C#CryptoStream结果与预期不符

时间:2009-09-18 22:14:12

标签: c# .net cryptography cryptostream

我试图理解为什么以下代码导致加密字节数组为16个字节,如果plainText长度为8个字节。我期望结果也是8个字节的长度?

private static byte[] encrypt(byte[] key, byte[] plainText)
{
    try
    {
        using (MemoryStream ms = new MemoryStream())
        {
            DES des = new DESCryptoServiceProvider() { Key = key, IV = key };

            using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
            {
                using(BinaryWriter bw = new BinaryWriter(cs))
                {
                    bw.Write(plainText);
                }
            }

            return ms.ToArray();
        }
    }
    catch (Exception e)
    {
        Logger.LogWarning(e);
        throw e;
    }
}

1 个答案:

答案 0 :(得分:3)