Tuesday 11 August 2009

Simple but Powerfull Encryption/Decryption utility

You probably find yourself thinking about how to secure your data. It's especially useful if you stored some information in file(s) and/or sending information over the Internet, or just want to secure something. According what's your goal is, you can find hundred-of-thousands encryption utilities on the Internet.
I personally think that .NET has strong enough ability for encryption/decryption. Just have a look to System.Security.Cryptography namespace. I find myself comfortable with Advanced Encryption Standard (AES). The following code shows how easy is to used it and create a powerful encryption:

Imports System.Security.Cryptography
Imports System.Text.UTF8Encoding

Public Class crypt
Private _salt As String = "I am Salt-to mix output phase" 'some "salt" string to spice-up phase
Private _pass As String = "A1most_p0w3rful_passw0rd" 'password used to en/decrypt

Public Sub New()

End Sub

Public Function Encrypt(ByVal input As String) As String
Dim result As String
Dim aes As New AesManaged
Dim utfData As Byte() = UTF8.GetBytes(input)
Dim saltBytes As Byte() = UTF8.GetBytes(_salt)
Dim rfc As New Rfc2898DeriveBytes(_pass, saltBytes)
With aes
.BlockSize = .LegalBlockSizes(0).MaxSize
.KeySize = .LegalKeySizes(0).MaxSize
.Key = rfc.GetBytes(.KeySize / 8)
.IV = rfc.GetBytes(.BlockSize / 8)
Using encryptTransf As ICryptoTransform = .CreateEncryptor
Using encryptStream As New System.IO.MemoryStream
Using encryptor As New CryptoStream(encryptStream, _
encryptTransf, CryptoStreamMode.Write)
encryptor.Write(utfData, 0, utfData.Length)
encryptor.Flush()
encryptor.Close()
End Using
result = System.Convert.ToBase64String(encryptStream.ToArray)
End Using
End Using
End With
rfc = Nothing
aes = Nothing
Return result
End Function

Public Function Decrypt(ByVal input As String) As String
Dim result As String
Dim encryptedBytes As Byte() = System.Convert.FromBase64String(input)
Dim saltBytes As Byte() = UTF8.GetBytes(_salt)
Dim aes As New AesManaged
Dim rfc As New Rfc2898DeriveBytes(_pass, saltBytes)
With aes
.BlockSize = .LegalBlockSizes(0).MaxSize
.KeySize = .LegalKeySizes(0).MaxSize
.Key = rfc.GetBytes(.KeySize / 8)
.IV = rfc.GetBytes(.BlockSize / 8)
Using decryptTrans As ICryptoTransform = .CreateDecryptor
Using decryptStream As New System.IO.MemoryStream
Using decryptor As New CryptoStream(decryptStream, _
decryptTrans, CryptoStreamMode.Write)
decryptor.Write(encryptedBytes, 0, encryptedBytes.Length)
decryptor.Flush()
decryptor.Close()
End Using
Dim decryptBytes As Byte() = decryptStream.ToArray
result = UTF8.GetString(decryptBytes, 0, decryptBytes.Length)
End Using
End Using
End With
rfc = Nothing
aes = Nothing
Return result
End Function

Public Function toHex(ByVal input As String) As String
Dim result As New System.Text.StringBuilder
For Each c As Char In input
result.Append(AscW(c).ToString("X"))
Next
Return result.ToString
End Function

Public Function hexToString(ByVal hexInput As String) As String
Dim result As New System.Text.StringBuilder
For i = 1 To Len(hexInput) Step 2
result.Append(ChrW(Integer.Parse(Mid(hexInput, i, 2), _
System.Globalization.NumberStyles.HexNumber)))
Next
Return result.ToString
End Function

Sample of usage: The following code shows how to encrypt the string stored in txtString. I used extra function toHex to convert encryption result to Hex numbers. The encr contains encrypted string and decr contains decrypted string (the original string)

Dim txtString As String = "String To Be Encrypted!"
Dim crypto As New crypt
Dim encr As String = crypto.toHex(crypto.Encrypt(txtString))
Dim decr As String = crypto.Decrypt(crypto.hexToString(encr))
crypto = Nothing

Happy coding ;)

No comments: