0
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 });
4

3 回答 3

4

您需要using在流周围使用块以确保它们被关闭和刷新。尝试这个:

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);

    using(MemoryStream ms = new MemoryStream())
    {
      using(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();

    using(MemoryStream ms = new MemoryStream())
    {
      using(CryptoStream cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
        cs.Write(data, 0, data.Length);

      return ms.ToArray();
    }
}
于 2013-06-06T18:31:23.287 回答
2

如果您对 s 使用语句using它将刷新并关闭,以便它实际更新您的 s。文档记录了这种行为:你应该总是在写完之后。看起来是这样的(加密和解密非常相似):CryptoStreamMemoryStreamCryptoStreamClose

using (CryptoStream cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
    cs.Write(data, 0, data.Length);
return ms.ToArray();
于 2013-06-06T18:34:34.850 回答
1

在将流转换为数组之前调用FlushFinalBlock

于 2013-06-06T18:32:55.543 回答