Note that there are some explanatory texts on larger screens.

plurals
  1. POCryptography Padding is invalid exception
    primarykey
    data
    text
    <p>I am using Visual Basic.net and have the following System.Security.Cryptography.CryptographicException exception:</p> <blockquote> <p>Padding is invalid and cannot be removed.</p> </blockquote> <p>Here is my code:</p> <pre><code> Public Sub SaveEncryptedObjectToFile(FileName As String, Item As Object) Dim fs As FileStream Dim encryptor As CryptoStream Dim formatter As New BinaryFormatter Dim password As String = "MyPassword" Dim salt As String = "InitialVector123" Dim AES As AesManaged = New AesManaged AES.Padding = PaddingMode.PKCS7 AES.Mode = CipherMode.CBC Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5 Dim PasswordIterations As Integer = 2 Dim InitialVector As String = "InitialVector123" 'This should be a string of 16 ASCII characters. Dim KeySize As Integer = 256 'Can be 128, 192, or 256. Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector) Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt) Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(password, SaltValueBytes, HashAlgorithm, PasswordIterations) Dim KeyBytes As Byte() = DerivedPassword.GetBytes(CInt(KeySize / 8)) Dim encryptTransf As ICryptoTransform = AES.CreateEncryptor(KeyBytes, InitialVectorBytes) fs = New FileStream(FileName, FileMode.Create) encryptor = New CryptoStream(fs, encryptTransf, CryptoStreamMode.Write) Try formatter.Serialize(encryptor, Item) Catch e As SerializationException Console.WriteLine("Failed to serialize. Reason: " &amp; e.Message) Throw Finally fs.Close() End Try End Sub Public Function OpenEncryptedObjectFromFile(FileName As String) As Object Dim fs As New FileStream(FileName, FileMode.Open) Dim decryptor As CryptoStream Dim ItemToReturn As New Object Dim password As String = "MyPassword" Dim salt As String = "InitialVector123" Dim AES As AesManaged = New AesManaged AES.Padding = PaddingMode.PKCS7 AES.Mode = CipherMode.CBC Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5 Dim PasswordIterations As Integer = 2 Dim InitialVector As String = "InitialVector123" 'This should be a string of 16 ASCII characters. Dim KeySize As Integer = 256 'Can be 128, 192, or 256. Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector) Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt) Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(password, SaltValueBytes, HashAlgorithm, PasswordIterations) Dim KeyBytes As Byte() = DerivedPassword.GetBytes(CInt(KeySize / 8)) 'Dim KeyBytes As Byte() = Rfc2898DeriveBytes(CInt(KeySize / 8)) Dim decryptTrans As ICryptoTransform = AES.CreateDecryptor(KeyBytes, InitialVectorBytes) Try Dim formatter As New BinaryFormatter decryptor = New CryptoStream(fs, decryptTrans, CryptoStreamMode.Read) ItemToReturn = DirectCast(formatter.Deserialize(decryptor), Object) Return ItemToReturn Catch e As SerializationException Console.WriteLine("Failed to deserialize. Reason: " &amp; e.Message) Throw Finally fs.Close() End Try End Function </code></pre> <p>The exception occurs at the following line:</p> <pre><code>ItemToReturn = DirectCast(formatter.Deserialize(decryptor), Object) </code></pre> <p>I am not sure if this has any relation to the CryptographicException, but I am currently getting the following warning:</p> <blockquote> <p>'Public Overrides Function GetBytes(cb As Integer) As Byte()' is obsolete: 'Rfc2898DeriveBytes replaces PasswordDeriveBytes for deriving key material from a password and is preferred in new applications.'.</p> </blockquote> <p>At this line:</p> <pre><code>Dim KeyBytes As Byte() = DerivedPassword.GetBytes(CInt(KeySize / 8)) </code></pre> <p>I have had a look online and cannot find any working solution for this warning.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload