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

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

I have error from CryptoStream:

Padding is invalid and cannot be removed.

Code

public MemoryStream Enc         


        
相关标签:
4条回答
  • 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.

    0 讨论(0)
  • 2020-12-07 02:06

    It's work

    using (FileStream fs = new FileStream( absolute, FileMode.Open )) {
        // create a CryptoStream in read mode
        using (CryptoStream cryptoStream = new CryptoStream( fs, decryptor, CryptoStreamMode.Read )) {
            int readLength = ( int )fs.Length;
            byte[] buffer = new byte[readLength];
            cryptoStream.Read( buffer, 0, readLength );
            using (MemoryStream ms = new MemoryStream( buffer )) {
                BinaryFormatter bf = new BinaryFormatter( );
                settings = ( SettingsJson )bf.Deserialize( ms );// Deserialize SettingsJson array
            }
        }
        fs.Close( );
    }
    
    0 讨论(0)
  • 2020-12-07 02:12

    My problem was I was taking the encryped output in bytes and converting it to a string. The string back to byte array (for decrypting) was not the same. I was using UTF8Encoding, then I tried ASCIIEnconding. Neither worked.

    Convert.FromBase64String/ToBase64String worked fine, got rid of the padding issues and actually decrypted the data.

    0 讨论(0)
  • 2020-12-07 02:16

    Please check your pass phrase - it should be same in both methods EncrypteBytes and DecrypteBytes. If both are not same, then it will generate the error.

    0 讨论(0)
提交回复
热议问题