I've built a little utility to see how the TripleDesStream class works, so I can encrypt sensitive strings in source code.
I have a form with:
- textbox to get seed string
- textbox for text to encrypt
- button to encrypt
- textbox with encrypted text
- button to decrypt
- text box with decrypted text
The encrypt code is:
Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
Dim seed As String = String.Empty
Dim text As String = String.Empty
' Must have text
If Me.txtTextToEncrypt.Text Is Nothing OrElse Me.txtTextToEncrypt.Text.Trim().Length() = 0 Then
MessageBox.Show("You must provide some text to encrypt!")
Return
End If
' Get text to encrypt
text = Me.txtTextToEncrypt.Text
' Get byte arrays for key and initialization vector
If Not Me.txtSecuritySeed.Text Is Nothing Then
seed = Me.txtSecuritySeed.Text
End If
Dim securityKey As Byte() = SecurityBasics.GetSecurityKey(seed, 24)
Dim initVector As Byte() = SecurityBasics.GetSecurityVector(seed, 8)
' Encrypt
Dim crypto As New TripleDesStream(securityKey, initVector)
Me.txtEncryptedText.Text = crypto.Encrypt(text)
End Sub
This seems to work fine. However, I get an error when I decrypt the text:
Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click
Dim seed As String = String.Empty
Dim text As String = String.Empty
' Must have text
If Me.txtEncryptedText.Text Is Nothing OrElse Me.txtEncryptedText.Text.Trim().Length() = 0 Then
MessageBox.Show("You must provide some text to decrypt!")
Return
End If
' Get text to encrypt
text = Me.txtEncryptedText.Text
' Get byte arrays for key and initialization vector
If Not Me.txtSecuritySeed.Text Is Nothing Then
seed = Me.txtSecuritySeed.Text
End If
Dim securityKey As Byte() = SecurityBasics.GetSecurityKey(seed, 24)
Dim initVector As Byte() = SecurityBasics.GetSecurityVector(seed, 8)
' Encrypt
Dim crypto As New TripleDesStream(securityKey, initVector)
Me.txtDecryptedText.Text = crypto.Decrypt(text)
End Sub
The error is "Invalid character in Base-64 string.
What am I doing wrong?