Rijndael Encryption in VB.NET

Category:
C#, VB.NET, ASP.NET
Type:
Snippets
Difficulty:
Intermediate

Author: Intelligent Solutions Inc.

Version Compatibility: Visual Basic.NET

More information:
The EncryptString128Bit and DecryptString128Bit functions provide full encrytption and decryption capabilities for any string. The EncryptString128Bit will strip any nulls from the string to be encrypted. Likewise the DecryptString128Bit function will strip any nulls from the decrypted string.

Demo Usage:

Dim sEnc As String

sEnc = EncryptText("This is a test value", _
    "This is my secret key")
Debug.WriteLine(sEnc)
Debug.WriteLine(DecryptText(sEnc, _
        "This is my secret key"))

Thanks to Ron Beauchemin for updating this code (22 May 2003) to correct a bug whereby extraneous null characters were added at the end of the decrypted string. This created problems if you decrypted a string from an encrypted row in a database, displayed the string on the form and later reencrypted the string from the form and used it to access the original row in the database. In this case, the row was not found. Ron tracked this down to the fact the original decryption function did not strip off trailing nulls created by the CryptoStream object.

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: