Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Created August 24, 2018 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save heiswayi/e98b492413f1458947973442ee0b72ce to your computer and use it in GitHub Desktop.
Save heiswayi/e98b492413f1458947973442ee0b72ce to your computer and use it in GitHub Desktop.
Encryption & Decryption method for C#
public static string Encrypt(string data, string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 0x80;
rijndaelCipher.BlockSize = 0x80;
byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] plainText = Encoding.UTF8.GetBytes(data);
return Convert.ToBase64String
(transform.TransformFinalBlock(plainText, 0, plainText.Length));
}
public static string Decrypt(string data, string key)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 0x80;
rijndaelCipher.BlockSize = 0x80;
byte[] encryptedData = Convert.FromBase64String(data);
byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
byte[] keyBytes = new byte[0x10];
int len = pwdBytes.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
rijndaelCipher.IV = keyBytes;
byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock
(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}
@bhavin1994
Copy link

I need to decrypt from dart as android side . How to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment