napoleonjones
Member
- Joined
- Mar 24, 2007
- Messages
- 18
- Programming Experience
- Beginner
I am working on an encryption class to be used with Access/Excel around the office, but I keep running into problems. At first I kept getting an error while decrypting stating that the padding was invalid when I attempted to close the stream. I set the padding = none and now it seems to run correctly while encrypting the string, but decryption does not return the original string. Can somebody please help.
VB.NET:
Private Function EString(ByVal Text As String, ByVal Password As String) As String
Try
Dim RJDLM As New RijndaelManaged
Dim key As New Rfc2898DeriveBytes(Password, salt)
With RJDLM
.KeySize = 256
.Key = key.GetBytes(.KeySize / 8)
.IV = key.GetBytes(.BlockSize / 8)
.Padding = PaddingMode.None
End With
Dim BTText As Byte() = ASCII.GetBytes(Text)
Dim Encryptor As ICryptoTransform = RJDLM.CreateEncryptor
Dim MemStream As New MemoryStream
Dim EncryptStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
EncryptStream.Write(BTText, 0, BTText.Length)
EncryptStream.Flush()
EncryptStream.Close()
Dim BTOutput As Byte() = MemStream.ToArray
Dim Output As String = ASCII.GetString(BTOutput)
MemStream.Close()
Return Output
Catch exCrypto As CryptographicException
MsgBox(exCrypto.Message & vbNewLine & exCrypto.StackTrace & vbNewLine & exCrypto.Source, MsgBoxStyle.Critical, "Cryptographic Exception")
Catch ex As Exception
MsgBox(ex.Message & vbNewLine & ex.StackTrace & vbNewLine & ex.Source, MsgBoxStyle.Critical, "Unhandled Exception")
End Try
End Function
Private Function DString(ByVal Text As String, ByVal Password As String) As String
Try
Dim RJDLM As New RijndaelManaged
Dim key As New Rfc2898DeriveBytes(Password, salt)
With RJDLM
.KeySize = 256
.Key = key.GetBytes(.KeySize / 8)
.IV = key.GetBytes(.BlockSize / 8)
.Padding = PaddingMode.None
End With
Dim BTText As Byte() = ASCII.GetBytes(Text)
Dim Decryptor As ICryptoTransform = RJDLM.CreateDecryptor
Dim MemStream As New MemoryStream
Dim DecryptStream As New CryptoStream(MemStream, Decryptor, CryptoStreamMode.Write)
DecryptStream.Write(BTText, 0, BTText.Length)
DecryptStream.Flush()
DecryptStream.Close()
Dim BTOutput As Byte() = MemStream.ToArray
Dim Output As String = ASCII.GetString(BTOutput)
MemStream.Close()
Return Output
Catch exCrypto As CryptographicException
MsgBox(exCrypto.Message & vbNewLine & exCrypto.StackTrace & vbNewLine & exCrypto.Source, MsgBoxStyle.Critical, "Cryptographic Exception")
Catch ex As Exception
MsgBox(ex.Message & vbNewLine & ex.StackTrace & vbNewLine & ex.Source, MsgBoxStyle.Critical, "Unhandled Exception")
End Try
End Function