Created
February 4, 2022 13:25
-
-
Save suchithm/c3a358585a4fef785bcc4041aa2c788f to your computer and use it in GitHub Desktop.
Encrypt decrypt string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static byte[] iv; | |
public byte[] DoEncryptionString(string strText) | |
{ | |
var secretKey = GetKeyFromKeyStore(); | |
Cipher cipher = Cipher.GetInstance("AES/GCM/NoPadding"); | |
cipher.Init(Javax.Crypto.CipherMode.EncryptMode, secretKey); | |
iv = cipher.GetIV(); | |
var encryption = cipher.DoFinal(System.Text.Encoding.UTF8.GetBytes(strText)); | |
return encryption; | |
} | |
public string DoDecryptionString(byte[] strEncrypted) | |
{ | |
var secretKey = GetKeyFromKeyStore(); | |
Cipher cipher = Cipher.GetInstance("AES/GCM/NoPadding"); | |
Javax.Crypto.Spec.GCMParameterSpec spec = new Javax.Crypto.Spec.GCMParameterSpec(128, iv); | |
cipher.Init(Javax.Crypto.CipherMode.DecryptMode, secretKey, spec); | |
byte[] decodedData = cipher.DoFinal(strEncrypted); | |
var data = System.Text.Encoding.UTF8.GetString(decodedData); | |
return data; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment