public static byte[] AES_Encrypt(byte[] data, string[] aes_key)
{
var aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Encoding.Default.GetBytes(aes_key[0]);
aes.IV = Encoding.Default.GetBytes(aes_key[1]);
var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
return ms.ToArray();
}
public static byte[] AES_Decrypt(byte[] data, string[] aes_key)
{
RijndaelManaged aes = new RijndaelManaged();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Encoding.Default.GetBytes(aes_key[0]);
aes.IV = Encoding.Default.GetBytes(aes_key[1]);
var decrypt = aes.CreateDecryptor();
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write);
cs.Write(data, 0, data.Length);
return ms.ToArray();
}
代码中的某处:
string key = "788b0adbcf8b9211282fe613b18630d2";
string iv = "7fbb16b806fcc24396653b3218552d39";
byte[] test_byte = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] enc_byte = AES_Encrypt(test_byte, new string[] { key, iv }); //getting zero array
byte[] dec_byte = AES_Decrypt(enc_byte, new string[] { key, iv });