简体   繁体   English

如何修复:rfc2898DeriveBytes 和 AES 加密出现“填充无效且无法删除”错误?

[英]How to fix : 'Padding is invalid and cannot be removed' error with rfc2898DeriveBytes and AES encryption?

I am trying to encrypt and decrypt text by using the rfc2898 Derive bytes method to generate an iv and key to use with AES encryption however I run in to padding is invalid and cannot be removed whenever I try to decrypt a text with the same password.我正在尝试使用 rfc2898 Derive bytes 方法来加密和解密文本,以生成用于 AES 加密的 iv 和密钥,但是每当我尝试使用相同的密码解密文本时,我遇到填充无效且无法删除的情况。 This will be used to encrypt a text and sent to another user and they will use the same password to decrypt the text.这将用于加密文本并发送给另一个用户,他们将使用相同的密码来解密文本。

Here is the line of code where the error occurs:这是发生错误的代码行:

plaintext = srDecrypt.ReadToEnd()

And here is the call stack:这是调用堆栈:

Internal.Cryptography.UniversalCryptoDecryptor.DepadBlock(byte[], int, int)
    Internal.Cryptography.UniversalCryptoDecryptor.UncheckedTransformFinalBlock(byte[], int, int)
    Internal.Cryptography.UniversalCryptoTransform.TransformFinalBlock(byte[], int, int)
    System.Security.Cryptography.CryptoStream.ReadAsyncCore(byte[], int, int, System.Threading.CancellationToken, bool)
    System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
    System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
    System.Runtime.CompilerServices.TaskAwaiter<TResult>.GetResult()
    System.Security.Cryptography.CryptoStream.Read(byte[], int, int)
    System.IO.StreamReader.ReadBuffer()

*edit Here is the decrypt function *编辑这是解密 function

dim salt(8) as byte
  Function Decrypt(ByVal password As String, ByVal ciphertext As String)
        'variable to hold the string
        Dim plaintext As String = ""

        'creates the encrypted byte
        Dim encrypted() As Byte = Convert.FromBase64String(ciphertext)

        'fills the array with random values
        Using rngCsp As New RNGCryptoServiceProvider()
            rngCsp.GetBytes(salt)
        End Using

        'number of iterations to use
        Dim iterations As Integer = 1000

        'turns the password into a key
        Dim crypt As New Rfc2898DeriveBytes(password, salt, iterations)

        Using aesencrypt As Aes = Aes.Create()

            'creates the key and the iv
            aesencrypt.Key = crypt.GetBytes(32)
            aesencrypt.IV = crypt.GetBytes(16)

            ' Create a decryptor to perform the stream transform.
            Dim decryptor As ICryptoTransform = aesencrypt.CreateDecryptor(aesencrypt.Key, aesencrypt.IV)

            ' Create the streams used for decryption.
            Using msDecrypt As New MemoryStream(encrypted)

                Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

                    Using srDecrypt As New StreamReader(csDecrypt)
                        ' Read the decrypted bytes from the decrypting stream
                        ' and place them in a string.
                        plaintext = srDecrypt.ReadToEnd()
                        srDecrypt.Close()
                    End Using
                End Using
            End Using
        End Using
        Return plaintext
    End Function

The encrypt function is mostly the same but with the encrypt methods instead of the decrypt methods.加密 function 大部分相同,但使用加密方法而不是解密方法。

Removing the following lines as it causes the program to use different salts:删除以下行,因为它会导致程序使用不同的盐:

'fills the array with random values
        Using rngCsp As New RNGCryptoServiceProvider()
            rngCsp.GetBytes(salt)
        End Using

This allows the same salt to be used when encrypting and decrypting.这允许在加密和解密时使用相同的盐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM