Skip to content

Instantly share code, notes, and snippets.

@suchithm
Created February 4, 2022 13:25
Show Gist options
  • Save suchithm/c3a358585a4fef785bcc4041aa2c788f to your computer and use it in GitHub Desktop.
Save suchithm/c3a358585a4fef785bcc4041aa2c788f to your computer and use it in GitHub Desktop.
Encrypt decrypt string
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