RijdaelManaged Encrypted byte []使用相同的密钥/ iv与Decrypted byte []不同

时间:2017-08-05 20:37:41

标签: c# encryption rijndael rijndaelmanaged

好的,所以我试图在传输之前编码一个byte []并在之后对其进行解码。我首先将“这是一个超级秘密消息”转换为byte [],然后使用RijdaelManaged加密。它在加密时从字节[30](未加密)转换为字节[16],但是当我尝试解密它时,它变为byte [13]并且在使用字符串构建器转换为字符串时仅呈现“System.Byte []”

编辑:我已经三次检查过keyIV.Key和keyIV.IV在加密/解密时是否匹配

通话方式:

    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

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

1 个答案:

答案 0 :(得分:2)

问题是您使用StreamWriterTextWriter)来写入加密流。这样做是选择带有Object的.Write的重载,并且你传递一个数组......当你这样做时,TextWriter假设你传递的对象有某种smart .ToString()重载,它将返回您要写入目标流的字符串值。在这种情况下,您的数组不会,因此您正在加密值" 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));