Tuesday, July 13, 2010

Encrypt / Decrypt Strings using Visual Basic and C# .Net

I have created a few handy methods for encrypting and decrypting a string using the .net frameworks Tripple DES Crypto Service Provider.

I have posted the Encryption / Decryption code below in both VB and C# .Net.


Visual Basic Version:

Imports System
Imports System.IO
Imports System.Security.Cryptography

' Triple DES String Encryptor / Decryptor
' Austin Scott
' June 17, 2010
Public Class FormTestEncryptDecrypt

' Key For Triple DES
Private key() As Byte = {34, 12, 93, 23, 7, 53, 2, 99, 19, 25, 27, 200, 192, 32, 60, 57, 87, 102, 183, 123, 34, 22, 65, 42}
' IV for Triple DES
Private iv() As Byte = {69, 110, 68, 26, 69, 178, 200, 219}

Private Sub ButtonEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEncrypt.Click
TextBoxEncrypt.Text = Encrypt(TextBoxEncrypt.Text)
End Sub


Private Sub ButtonDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDecrypt.Click
TextBoxDecrypt.Text = Decrypt(TextBoxDecrypt.Text)
End Sub

' =-=-=-=-=-=-=-=-=-=-=---=-=-
' Encrypt a string value (to a Comma Separated byte array) using triple DES
' =-=-=-=-=-=-=-=-=-=-=---=-=-
Public Function Encrypt(ByVal plainText As String) As String

Dim utf8encoder As Text.UTF8Encoding = New Text.UTF8Encoding()
Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)

Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)

Dim encryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write)

cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
encryptedStream.Position = 0

Dim result(encryptedStream.Length - 1) As Byte
encryptedStream.Read(result, 0, encryptedStream.Length)
cryptStream.Close()

Dim sbResult As Text.StringBuilder = New Text.StringBuilder()

For iPos As Int16 = 0 To result.Length - 1
If (iPos = result.Length - 1) Then
sbResult.Append(result(iPos))
Else
sbResult.Append(result(iPos))
sbResult.Append(",")
End If
Next iPos

Return sbResult.ToString

End Function


' =-=-=-=-=-=-=-=-=-=-=---=-=-
'Decrypt a string value (Comma Separated byte array) using triple DES
' =-=-=-=-=-=-=-=-=-=-=---=-=-
Public Function Decrypt(ByVal stringToDecrypt As String) As String


Dim byteArrayString() As String = stringToDecrypt.Split(",")
Dim inputInBytes(byteArrayString.Length - 1) As Byte
For iPos As Int16 = 0 To byteArrayString.Length - 1
inputInBytes(iPos) = Convert.ToByte(byteArrayString(iPos))
Next iPos

Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateDecryptor(Me.key, Me.iv)

Dim decryptedStream As MemoryStream = New MemoryStream()
Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write)
cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
cryptStream.FlushFinalBlock()
decryptedStream.Position = 0

Dim result(decryptedStream.Length - 1) As Byte
decryptedStream.Read(result, 0, decryptedStream.Length)
cryptStream.Close()
Dim myutf As Text.UTF8Encoding = New Text.UTF8Encoding()
Return myutf.GetString(result)
End Function


C# Version:


// Key For Triple DES - Modify these
private byte[] key = { 34, 12, 93, 23, 7, 53, 2, 99, 19, 25, 27, 200, 192, 32, 60, 57, 87, 102, 183, 123, 34, 22, 65, 42 };
// IV for Triple DES - Modify these
private byte[] iv = { 69, 110, 68, 26, 69, 178, 200, 219 };

public Form1()
{
InitializeComponent();
}

private void ButtonEncrypt_Click(object sender, EventArgs e)
{
TextBoxEncrypt.Text = Encrypt(TextBoxEncrypt.Text);
}


private void ButtonDecrypt_Click(object sender, EventArgs e)
{
TextBoxDecrypt.Text = Decrypt(TextBoxDecrypt.Text);
}


// =-=-=-=-=-=-=-=-=-=-=---=-=-
// Encrypt a string value (to a Comma Separated byte array) using triple DES
// =-=-=-=-=-=-=-=-=-=-=---=-=-
private String Encrypt(string plainText)
{

UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputInBytes = utf8encoder.GetBytes(plainText);
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform = tdesProvider.CreateEncryptor(key, iv);

MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(encryptedStream, cryptoTransform, CryptoStreamMode.Write);

cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
cryptStream.FlushFinalBlock();
encryptedStream.Position = 0;

byte[] result = new byte[encryptedStream.Length];
encryptedStream.Read(result, 0, Convert.ToInt32(encryptedStream.Length));
cryptStream.Close();

// Create a comma separated string of the encrypted bytes values
StringBuilder sbResult = new StringBuilder();

for (Int16 iPos = 0; iPos <= result.Length - 1; iPos++)
{
if (iPos == result.Length - 1)
{
sbResult.Append(result[iPos]);
}
else
{
sbResult.Append(result[iPos]);
sbResult.Append(",");
}

}

return sbResult.ToString();
}


// =-=-=-=-=-=-=-=-=-=-=---=-=-
// Decrypt a string value (Comma Separated byte array) using triple DES
// =-=-=-=-=-=-=-=-=-=-=---=-=-
private String Decrypt(string stringToDecrypt)
{
string[] byteArrayString = stringToDecrypt.Split(',');
byte[] inputInBytes = new byte[byteArrayString.Length];
for (Int16 iPos = 0; iPos <= byteArrayString.Length - 1; iPos++)
{
inputInBytes[iPos] = Convert.ToByte(byteArrayString[iPos]);
}

TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform = tdesProvider.CreateDecryptor(key, iv);

MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(decryptedStream, cryptoTransform, CryptoStreamMode.Write);

cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
cryptStream.FlushFinalBlock();
decryptedStream.Position = 0;

byte[] result = new byte[decryptedStream.Length];
decryptedStream.Read(result, 0, Convert.ToInt32(decryptedStream.Length));
cryptStream.Close();
UTF8Encoding myutf = new UTF8Encoding();
return myutf.GetString(result);
}