R
Random
I'm creating a simple RinjdaelManaged class for some simple
encryption/decryption. I am not using a password to generate the Key and
IV, but am just hard-coding those values into the class. Yes, I know, but I
intend to build on it later for better security.
Problem right now is in the decryption, I am getting a "PKCS7 padding is
invalid and cannot be removed." error. What is the reason for this?
Public Class MySecurity
Private Shared KEY_128() As Byte = {--byte array--}
Private Shared IV_128() As Byte = {--byte array--}
Public cryptoObj As New RijndaelManaged
Public Shared Function Encrypt(ByVal value As String) As String
If value <> "" Then
Dim ms As MemoryStream = New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, cryptoObj
..CreateEncryptor(KEY_128, IV_128), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
'convert back to a string
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
Else
Return ""
End If
End Function
Public Shared Function Decrypt(ByVal value As String) As String
If value <> "" Then
'convert from string to byte array
Dim buffer As Byte() = Convert.FromBase64String(value)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = New CryptoStream(ms,
cryptoObj.CreateDecryptor(KEY_128, IV_128), CryptoStreamMode.Read)
Dim sr As StreamReader = New StreamReader(cs)
Return sr.ReadToEnd()
Else
Return ""
End If
End Function
End Class
encryption/decryption. I am not using a password to generate the Key and
IV, but am just hard-coding those values into the class. Yes, I know, but I
intend to build on it later for better security.
Problem right now is in the decryption, I am getting a "PKCS7 padding is
invalid and cannot be removed." error. What is the reason for this?
Public Class MySecurity
Private Shared KEY_128() As Byte = {--byte array--}
Private Shared IV_128() As Byte = {--byte array--}
Public cryptoObj As New RijndaelManaged
Public Shared Function Encrypt(ByVal value As String) As String
If value <> "" Then
Dim ms As MemoryStream = New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, cryptoObj
..CreateEncryptor(KEY_128, IV_128), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
'convert back to a string
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
Else
Return ""
End If
End Function
Public Shared Function Decrypt(ByVal value As String) As String
If value <> "" Then
'convert from string to byte array
Dim buffer As Byte() = Convert.FromBase64String(value)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = New CryptoStream(ms,
cryptoObj.CreateDecryptor(KEY_128, IV_128), CryptoStreamMode.Read)
Dim sr As StreamReader = New StreamReader(cs)
Return sr.ReadToEnd()
Else
Return ""
End If
End Function
End Class