Created
August 24, 2018 22:14
-
-
Save heiswayi/e98b492413f1458947973442ee0b72ce to your computer and use it in GitHub Desktop.
Encryption & Decryption method for C#
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 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need to decrypt from dart as android side . How to do it?