How to use the TripleDesStream class?


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
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?
StrataFrame Team
S
StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
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.
StrataFrame Team
S
StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)StrataFrame Developer (4.3K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
I believe it's the cust_CreditCard field that is encrypted within the CustomersBO business object in the CRM sample.
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
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
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.4K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
That was easy...done!
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search