String Encryption

John
02-22-2006, 07:51 PM
So I decided to play around with the encryption capabilities in the .NET framework this evening. The result is a little class library that will encrypt and decrypt a string. Optionally you can provide your own key and initialization vector, both in the format of a hex string. You don't have to provide them, but it would be much more secure if you don't use the defaults provided. You can use the library to generate new ones by calling the GenerateKey and GenerateIV respectively. Save the results in a secure place and use them when you want to encrypt or decrypt a string of text.

The way it works is this, you pass a string of text to the EncryptString function and it returns the encrypted result in the form of a hex string. You can then pass the hex string to the DecryptString function and get back the original string, provided the key and iv are set to the same values used during the encryption. Here is an example of using it:

Imports System

Public Class Tester
Public Shared Sub Main()
Dim txt As String
Dim helper As New StringEncryption.StringEncryption

helper.Key = helper.GenerateKey
helper.IV = helper.GenerateIV

Console.WriteLine("Key: " & helper.Key)
Console.WriteLine("IV: " & helper.IV)

txt = "encryption is cool"
Console.WriteLine("Before Encryption: " & txt)

txt = helper.EncryptString(txt)
Console.WriteLine("After Encryption: " & txt)

txt = helper.DecryptString(txt)
Console.WriteLine("After Decryption: " & txt)

Console.ReadLine()
End Sub
End Class

This would be handy in situations where you have login and passwords stored in your applications, or app.config files, but don't want them to be easily deciphered. A password manager would be a good example of something where this would also be useful.

If you find any problems or have suggestions for improvement feel free to PM me.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum