解密返回16个字节而不是32个,为什么?

时间:2010-12-15 19:00:07

标签: c# aes encryption

我有以下解密方法似乎工作正常,除了它只返回16个字节而不是32它应该我已经盯着它太长时间并且无法弄清楚我做错了什么。我希望不同的一双眼睛会有所帮助。感谢。

private string Decrypt(string dataToDecrypt, string password, string salt)
    {
        //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator 
        Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt));

        //Create AES algorithm with 256 bit key and 128-bit block size 
        using (var aes = new AesManaged())
        {
            // These two values are hard coded in Silverlight
            //aes.Mode = CipherMode.CBC;
            //aes.Padding = PaddingMode.PKCS7;
            aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
            aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);

            //Create Memory and Crypto Streams 
            using (var memoryStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))
                {

                    //Decrypt Data 
                    byte[] data = Convert.FromBase64String(dataToDecrypt);
                    cryptoStream.Write(data, 0, data.Length);
                    // cryptoStream.FlushFinalBlock();

                    //Return Decrypted String 
                    byte[] decryptBytes = memoryStream.ToArray();
                    string decrypted = Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
                    return decrypted;

                }       // using (cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write))

            }       // using (memoryStream = new MemoryStream())

        }       // using (aes = new AesManaged())

    }       // string Decrypt(string dataToDecrypt, string password, string salt)

1 个答案:

答案 0 :(得分:0)

您的块大小为128位。如果您只解码一个块,则将获得16个字节。