0

这是我的代码,首先是字符串:

byte[] concatBytes = Encoding.ASCII.GetBytes(key);
byte[] keyBytes = Encoding.ASCII.GetBytes(key);
for (int i = 0; i < 3; i++)
{
         concatBytes = Encrypt(Encoding.ASCII.GetString(concatBytes), keyBytes);
         //Console.WriteLine(Transform.Hexa(concatBytes));
} 

public byte[] Encrypt(string plainText, byte[] key)
    {
        byte[] encrypted;
        using (var rijndael = new RijndaelManaged())
        {
            rijndael.Mode = CipherMode.ECB;
            rijndael.KeySize = 128;
            rijndael.BlockSize = 128;
            rijndael.Padding = PaddingMode.Zeros;
            rijndael.Key = key;
            //rijndael.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            ICryptoTransform transform = rijndael.CreateEncryptor(rijndael.Key, rijndael.IV);

            using (var memoryStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
                {
                    using (var streamWriter = new StreamWriter(cryptoStream))
                    {
                        streamWriter.Write(plainText);
                    }
                    encrypted = memoryStream.ToArray();
                }
            }
        }
        return encrypted;
    }

如果我将方法参数更改plainTextbyte[]比我的结果完全不同。我在这里想念什么?此外,我们使用OpenSSLRijndael AES加密有不同的结果。有什么建议我可以解决这个问题吗?

4

1 回答 1

1

StreamWriter(Stream)

StreamWriter使用 UTF-8 编码和默认缓冲区大小为指定流初始化类的新实例。

由于您使用的是不同的编码(UTF-8 而不是 ASCII),因此得到不同的结果也就不足为奇了。

于 2013-01-22T11:35:06.767 回答