Error RijndaelManaged, “Padding is invalid and cannot be removed”

前端 未结 4 754
萌比男神i
萌比男神i 2020-12-07 01:27

I have error from CryptoStream:

Padding is invalid and cannot be removed.

Code

public MemoryStream Enc         


        
4条回答
  •  萌比男神i
    2020-12-07 01:59

    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.

提交回复
热议问题