Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

c# - Error RijndaelManaged, "Padding is invalid and cannot be removed"

I have error from CryptoStream:

Padding is invalid and cannot be removed.

Code

public MemoryStream EncrypteBytes(Stream inputStream, string passPhrase, string saltValue)
{
    RijndaelManaged RijndaelCipher = new RijndaelManaged();
    RijndaelCipher.Padding = PaddingMode.PKCS7;
    RijndaelCipher.Mode = CipherMode.CBC;
    byte[] salt = Encoding.ASCII.GetBytes(saltValue);
    PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);

    ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(password.GetBytes(32), password.GetBytes(16));
    MemoryStream memoryStream = new MemoryStream();
    CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
    var buffer = new byte[1024];
    var read = inputStream.Read(buffer, 0, buffer.Length);
    while (read > 0)
    {
        cryptoStream.Write(buffer, 0, read);
        read = inputStream.Read(buffer, 0, buffer.Length);
    }

    cryptoStream.FlushFinalBlock();
    memoryStream.Position = 0;
    return memoryStream;
}

// Example usage: DecryptBytes(encryptedBytes, "SensitivePhrase", "SodiumChloride");
public byte[] DecrypteBytes(MemoryStream memoryStream, string passPhrase, string saltValue)
{
    RijndaelManaged RijndaelCipher = new RijndaelManaged();
    RijndaelCipher.Padding = PaddingMode.PKCS7;

    RijndaelCipher.Mode = CipherMode.CBC;
    byte[] salt = Encoding.ASCII.GetBytes(saltValue);
    PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);

    ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(password.GetBytes(32), password.GetBytes(16));


    CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
    byte[] plainBytes = new byte[memoryStream.Length];

    int DecryptedCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);

    return plainBytes;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use PaddingMode.Zeros to fix problem , Following code:

   public byte[] EncryptFile(Stream input, string password, string salt)
            {

                // Essentially, if you want to use RijndaelManaged as AES you need to make sure that:
                // 1.The block size is set to 128 bits
                // 2.You are not using CFB mode, or if you are the feedback size is also 128 bits

                var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 };
                var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(salt));

                algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
                algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
                algorithm.Padding = PaddingMode.Zeros;

                using (Stream cryptoStream = new MemoryStream())
                using (var encryptedStream = new CryptoStream(cryptoStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    CopyStream(input, encryptedStream);

                    return ReadToEnd(cryptoStream);
                }
            }

            public byte[] DecryptFile(Stream input, Stream output, string password, string salt)
            {
                // Essentially, if you want to use RijndaelManaged as AES you need to make sure that:
                // 1.The block size is set to 128 bits
                // 2.You are not using CFB mode, or if you are the feedback size is also 128 bits
                var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 };
                var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(salt));

                algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
                algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
                algorithm.Padding = PaddingMode.Zeros;

                try
                {
                    using (var decryptedStream = new CryptoStream(output, algorithm.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        CopyStream(input, decryptedStream);
                        return ReadToEnd(output);
                    }
                }
                catch (CryptographicException ex)
                {
                    throw new InvalidDataException("Please supply a correct password");
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

I hop help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...