Monday, December 13, 2010

TripleDES Encryption using MD5 Hashing

Example in VB.Net
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
 
Public NotInheritable Class Utility

 Private Sub New()
End Sub


Private
Shared Function CreateDES(ByVal key As String) As TripleDESDim md5 As MD5 = New MD5CryptoServiceProvider()
Dim des As TripleDES = New TripleDESCryptoServiceProvider()
des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
des.IV = New Byte(des.BlockSize \ 8 - 1) {}
Return des
End Function


Public
Shared Function Encryption(ByVal PlainText As String, ByVal key As String) As String
Dim
des As TripleDES = CreateDES(key)
Dim ct As ICryptoTransform = des.CreateEncryptor()
Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
Return Convert.ToBase64String(ct.TransformFinalBlock(input, 0, input.Length))
End Function


Public
Shared Function Decryption(ByVal CypherText As String, ByVal key As String) As String
Dim
b As Byte() = Convert.FromBase64String(CypherText)
Dim des As TripleDES = CreateDES(key)
Dim ct As ICryptoTransform = des.CreateDecryptor()
Dim output As Byte() = ct.TransformFinalBlock(b, 0, b.Length)
Return Encoding.Unicode.GetString(output)
End Function


End
Class

 
Example:
Dim strEncrypted As String = Utility.Encryption("Hello", "TESTKEY3327ABC09876433")
Dim strDecrypted As String =
Utility.Decryption(strEncrypted,TESTKEY3327ABC09876433")MessageBox.Show(strDecrypted)

No comments:

Post a Comment