RC2, MD5, Triple Des, Byte, 128 Cryptography functions

Security is a key in Information Technologies. From banking to insurance domain every where we want our data to be safe. In web technologies security is a major factor. Looking into this Microsoft provides Cryptography in their advanced programming languages .NET Framework. There are several types of encryption & decryption available in the chapter of Cryptography. The most common used Cryptography functions are RC2, MD5, Triple Des, 128 Encryption & Byte.

In the area of day today developments programmers are looking for a ready-made function to encrypt or decrypt their strings. To start with Cryptography Microsoft provides the base Class System.Security.Cryptography. You need to import this System Class before using the following functions. Here with I am sharing the functions how to encrypt or decrypt a string using vb.net. With these functions you can easily encrypt or decrypt a string to RC2, MD5, 128 & Byte format. Enjoy the programming.

Cryptography function to encrypt a String

Public Function EncodeString(ByVal value As String, ByVal key As String) As String
Dim crdes As New System.Security.Cryptography.TripleDESCryptoServiceProvider
crdes.IV = New Byte(7) {}
Dim pdb As New System.Security.Cryptography.PasswordDeriveBytes(key, New Byte(-1) {})
crdes.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
Dim ms As New IO.MemoryStream((value.Length * 2) - 1)
Dim encStream As New System.Security.Cryptography.CryptoStream(ms, crdes.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
Dim plainBytes As Byte() = Text.Encoding.UTF8.GetBytes(value)
encStream.Write(plainBytes, 0, plainBytes.Length)
encStream.FlushFinalBlock()
Dim encryptedBytes(CInt(ms.Length - 1)) As Byte
ms.Position = 0
ms.Read(encryptedBytes, 0, CInt(ms.Length))
encStream.Close()
Return Convert.ToBase64String(encryptedBytes)
End Function

Cryptography function to decrypt a String

Public Function DecodeString(ByVal value As String, ByVal key As String) As String
Dim crdes As New System.Security.Cryptography.TripleDESCryptoServiceProvider
crdes.IV = New Byte(7) {}
Dim pdb As New System.Security.Cryptography.PasswordDeriveBytes(key, New Byte(-1) {})
crdes.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
Dim encryptedBytes As Byte() = Convert.FromBase64String(value)
Dim ms As New IO.MemoryStream(value.Length)
Dim decStream As New System.Security.Cryptography.CryptoStream(ms, crdes.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
Dim plainBytes(CInt(ms.Length - 1)) As Byte
ms.Position = 0
ms.Read(plainBytes, 0, CInt(ms.Length))
decStream.Close()
Return Text.Encoding.UTF8.GetString(plainBytes)
End Function