RijdaelManaged 加密字节 [] 与使用相同 key/iv 的解密字节 [] 不同

RijdaelManaged Encrypted byte[] different than Decrypted byte[] using same key/iv

好的,所以我正在尝试在传输之前编码一个字节[],然后再对其进行解码。我首先将 "This is a super secret message" 转换为 byte[],然后使用 RijdaelManaged 进行加密。它在加密时从 byte[30](未加密)变为 byte[16],但是当我尝试解密它时,它变成 byte[13] 并且在使用字符串生成器翻译成字符串时仅呈现 "System.Byte[]"。

编辑:我已经三次检查 keyIV.Key 和 keyIV.IV 在 encrypting/decrypting

时是否匹配

调用方式:

    static void Main(string[] args)
    {
        KeyIV keyIV = Encryption.GenerateKeyIV();
        int keyLen = keyIV.Key.Length;
        int ivLen = keyIV.IV.Length;

        string plain = "This is a super secret message";

        byte[] plainArray = Encoding.ASCII.GetBytes(plain);

        byte[] encryptedArray = Encryption.EncryptBytes(ref plainArray, keyIV.Key, keyIV.IV);

        byte[] decryptedArray = Encryption.DecryptBytes(ref encryptedArray, keyIV.Key, keyIV.IV);

        Console.WriteLine("Original Message: {0}\n", plain);
        Console.WriteLine("Byte[{0}] Converted Message: {1}\n", plainArray.Length, BitConverter.ToString(plainArray));
        Console.WriteLine("Byte[{0}] Encrypted Message: {1}\n", encryptedArray.Length, BitConverter.ToString(encryptedArray));
        Console.WriteLine("Byte[{0}] Decrypted Message: {1}\n", decryptedArray.Length, BitConverter.ToString(decryptedArray));

        Console.ReadLine();
    }

加密方法:

        public static byte[] EncryptBytes(ref byte[] input, byte[] key, byte[] iv)
    {
        if (input.Length > 0 && key != null && iv != null)
        {
            using (RijndaelManaged rm = new RijndaelManaged() { Key = key, IV = iv })
            {
                rm.Padding = PaddingMode.PKCS7;

                ICryptoTransform encryptor = rm.CreateEncryptor(rm.Key, rm.IV);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(cs)) { sw.Write(input); }
                    }

                    return ms.ToArray();
                }
            }
        }

        return null;
    }

解密方法:

        public static byte[] DecryptBytes(ref byte[] input, byte[] key, byte[] iv)
    {
        if (input != null && key != null && iv != null)
        {
            using (RijndaelManaged rm = new RijndaelManaged() { Key = key, IV = iv })
            {
                ICryptoTransform decryptor = rm.CreateDecryptor(rm.Key, rm.IV);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write)) { cs.Write(input, 0, input.Length); }

                    return ms.ToArray();
                }
            }
        }

        return null;
    }

结果输出:

原消息:这是一条超级机密消息

字节[30] 转换后的消息:54-68-69-73-20-69-73-20-61-20-73-75-70-65-72-20-73-65-63- 72-65-74-20-6D-65-73-73-61-67-65

字节[16]加密信息:DD-85-D4-1E-E6-40-AA-44-DB-1A-17-33-A7-73-70-34

字节[13] 解密消息:53-79-73-74-65-6D-2E-42-79-74-65-5B-5D

问题是您正在使用 StreamWriter(即 TextWriter)写入您的加密流。它正在做的是选择 .Write 的重载,它接受一个对象,并且你正在传递一个数组......当你这样做时,TextWriter 假设你传递的对象有某种智能 .ToString() 重载将 return 您要写入目标流的字符串值。在这种情况下,您的数组没有,因此您正在加密值 "System.Byte[]".

事实证明,您并不是真的想要 StreamWriter。您只想将您的加密方法提供给 CryptoStream 的数组的字节写入。幸运的是,流有一个内置的 Write 方法。在加密方法的主体中试试这个:

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
                    {
                        cs.Write(input, 0, input.Length);
                        cs.Flush();
                    }

                    return ms.ToArray();
                }

当我比较你数组的文本输出而不是字节表示时,我得到了错误提示:

        Console.WriteLine("Original Message: {0}\n", plain);
        Console.WriteLine("Byte[{0}] Converted Message: {1}\n", plainArray.Length, Encoding.ASCII.GetString(plainArray));
        Console.WriteLine("Byte[{0}] Encrypted Message: {1}\n", encryptedArray.Length, Encoding.ASCII.GetString(encryptedArray));
        Console.WriteLine("Byte[{0}] Decrypted Message: {1}\n", decryptedArray.Length, Encoding.ASCII.GetString(decryptedArray));