StrataFrame Forum

How to use the TripleDesStream class?

http://forum.strataframe.net/Topic6468.aspx

By Greg McGuffey - 1/31/2007

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?
By StrataFrame Team - 2/1/2007

Ah, the TripleDesStream should be marked as obsolete.  It was intended to be used as a replacement for the TripleDesWrapper, but it was never completed.  So, you will need to use the TripleDesWrapper instead.  There is a sample of how to encrypt fields within the CRM sample distributed with the framework.
By StrataFrame Team - 2/1/2007

I believe it's the cust_CreditCard field that is encrypted within the CustomersBO business object in the CRM sample.
By Greg McGuffey - 2/1/2007

You should probably update the xml comments for both the TripleDesStream and TripleDesWrapper then. TripleDesWrapper indicates it is marked as obsolete (if you look in object browser) and says to use TripleDesStream instead...hence why I used it Ermm



I'm switching to TripleDesWrapper now though! Wink
By Greg McGuffey - 2/1/2007

That was easy...done!