Padding is invalid and cannot be removed
-
Hi friends, We are getting the following error while trying to decrypt the string which is encrypted using the same class. "Padding is invalid and cannot be removed" following is the class: (VB.NET) Imports System Imports System.IO Imports System.Security.Cryptography Public Class EncryptDecrypt Dim key() As Byte Dim iv() As Byte Dim firstRJ As RijndaelManaged Sub EncryptDecrypt() Me.InitializeKeyIV() End Sub Sub InitializeKeyIV() key = New Byte() {229, 249, 126, 70, 196, 148, 231, 10, 130, 22, 65, 172, 216, 13, 68, 234, 46, 146, 31, 102, 228, 181, 212, 145} iv = New Byte() {8, 14, 130, 251, 155, 125, 219, 144, 103, 182, 95, 103, 58, 6, 205, 161} End Sub Public Function Encrypt(ByVal clearText) As String Dim retChiperText As String = "" If clearText <> "" Then Dim ms As MemoryStream = Nothing Dim cs As CryptoStream = Nothing Dim sw As StreamWriter = Nothing Try ms = New MemoryStream() firstRJ = New RijndaelManaged() Dim encryptor As ICryptoTransform = firstRJ.CreateEncryptor(key, iv) cs = New CryptoStream(ms, encryptor, CryptoStreamMode.Write) sw = New StreamWriter(cs) sw.Write(clearText) sw.Close() Dim chiperTextBytes As Byte() chiperTextBytes = ms.ToArray() retChiperText = Convert.ToBase64String(chiperTextBytes) Catch Ex As Exception Finally cs.Close() ms.Close() firstRJ.Clear() End Try End If Encrypt = retChiperText End Function Public Function Decrypt(ByVal chiperText) As String Dim retClearText As String = "" chiperText = Trim(chiperText) MessageBox.Show("Text: " + chiperText) If chiperText <> "" Then Dim cs2 As CryptoStream = Nothing Dim ms2 As MemoryStream = Nothing Dim sr As StreamReader = Nothing Try Dim chText As Byte() chText = Convert.FromBase64String(chiperText) firstRJ = New RijndaelManaged() ms2 = New MemoryStream(chText) Dim decryptor As ICryptoTransform = firstRJ.CreateDecryptor(key, iv) cs2 = New CryptoStream(ms2, decryptor, CryptoStreamMode.Read)
-
Hi friends, We are getting the following error while trying to decrypt the string which is encrypted using the same class. "Padding is invalid and cannot be removed" following is the class: (VB.NET) Imports System Imports System.IO Imports System.Security.Cryptography Public Class EncryptDecrypt Dim key() As Byte Dim iv() As Byte Dim firstRJ As RijndaelManaged Sub EncryptDecrypt() Me.InitializeKeyIV() End Sub Sub InitializeKeyIV() key = New Byte() {229, 249, 126, 70, 196, 148, 231, 10, 130, 22, 65, 172, 216, 13, 68, 234, 46, 146, 31, 102, 228, 181, 212, 145} iv = New Byte() {8, 14, 130, 251, 155, 125, 219, 144, 103, 182, 95, 103, 58, 6, 205, 161} End Sub Public Function Encrypt(ByVal clearText) As String Dim retChiperText As String = "" If clearText <> "" Then Dim ms As MemoryStream = Nothing Dim cs As CryptoStream = Nothing Dim sw As StreamWriter = Nothing Try ms = New MemoryStream() firstRJ = New RijndaelManaged() Dim encryptor As ICryptoTransform = firstRJ.CreateEncryptor(key, iv) cs = New CryptoStream(ms, encryptor, CryptoStreamMode.Write) sw = New StreamWriter(cs) sw.Write(clearText) sw.Close() Dim chiperTextBytes As Byte() chiperTextBytes = ms.ToArray() retChiperText = Convert.ToBase64String(chiperTextBytes) Catch Ex As Exception Finally cs.Close() ms.Close() firstRJ.Clear() End Try End If Encrypt = retChiperText End Function Public Function Decrypt(ByVal chiperText) As String Dim retClearText As String = "" chiperText = Trim(chiperText) MessageBox.Show("Text: " + chiperText) If chiperText <> "" Then Dim cs2 As CryptoStream = Nothing Dim ms2 As MemoryStream = Nothing Dim sr As StreamReader = Nothing Try Dim chText As Byte() chText = Convert.FromBase64String(chiperText) firstRJ = New RijndaelManaged() ms2 = New MemoryStream(chText) Dim decryptor As ICryptoTransform = firstRJ.CreateDecryptor(key, iv) cs2 = New CryptoStream(ms2, decryptor, CryptoStreamMode.Read)
It would appear that whatever you're doing to the crypted stream afterwards is altering the data in the stream, or adding something to it.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008