为什么RijndaelManaged在加密后返回垃圾数据?

时间:2012-07-27 15:18:15

标签: c# rijndaelmanaged

我从http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptostream.aspx获取了解密代码,并按如下方式对其进行了修改。我有一个加密的例子,它在解码时工作得很好。但是当使用加密函数时,它会返回带有奇怪符号的垃圾字符串。以下是加密/解密的功能。

加密字符串“hey”的一个例子:“??? U?b ??? z?Y ???”
再次解码时:“ûc{ÁpÅ`ñ”“”

我正在使用此代码将字节数组转换为字符串:

private string ByteArrayToString(byte[] input)
    {
        ASCIIEncoding dec = new ASCIIEncoding();
        return dec.GetString(input);
    }

这里是加密/解密功能。解密功能正常。

private string DecryptStringFromBytesAes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments. 
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the string used to hold 
        // the decrypted text.
        string plaintext = null;

        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged aesAlg = new RijndaelManaged())
        {
            aesAlg.Key = Key;
            aesAlg.Padding = PaddingMode.Zeros;
            aesAlg.Mode = CipherMode.ECB;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream 
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

        }

        return plaintext;

    }




private byte[] EncryptStringToBytesAes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments. 
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged aesAlg = new RijndaelManaged())
        {
            aesAlg.Key = Key;
            aesAlg.Padding = PaddingMode.Zeros;
            aesAlg.Mode = CipherMode.ECB;
            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }
        return encrypted;

    }

3 个答案:

答案 0 :(得分:4)

您观察到的是将任意字节(在0-255范围内)映射到字符的问题。有意义的字符仅在32-255范围内,甚至只有32-127(ASCII)。低于32的值是所谓的不可打印字符,高于127的值取决于您使用的字符编码。这就是加密文本看起来像垃圾的原因。因此,桅杆加密系统将字节转换为合理的ASCII范围。一种这样的算法是BASE64。因此,通过BASE64对加密的字节进行修改可以提供可打印的字符,并且可以通过电子邮件顺利运行。在解密之前,您必须撤消BASE64编码。

使加密结果看起来更好的另一种方法是显示它的六进制十进制表示。例如,如果您的字节值为15,则会打印0F。您可以使用它来表示十六进制的字节数组:

private string ByteArrayToHexString(byte[] data)
{
    return String.Concat(data.Select(b => b.ToString("x2")));
}

答案 1 :(得分:1)

加密字符串看起来像乱码。测试加密是否正常工作的方法是通过解密传递您的字符串。如果它在解密时工作,那么你知道字符串是正确的,尽管看起来像垃圾一样。

答案 2 :(得分:1)

为了将输出作为数据的十六进制编码,请按照here找到的方法进行操作。我稍微修改了它们作为扩展方法:

    public static string ToHexString(this byte[] bytes)
    {
        return bytes == null ? string.Empty : BitConverter.ToString(bytes).Replace("-", string.Empty);
    }

    public static byte[] FromHexString(this string hexString)
    {
        if (hexString == null)
        {
            return new byte[0];
        }

        var numberChars = hexString.Length;
        var bytes = new byte[numberChars / 2];

        for (var i = 0; i < numberChars; i += 2)
        {
            bytes[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
        }

        return bytes;
    }