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

Public Class tripleDES

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

    Public Function TripleDESDecryptMessage(ByVal encryptedBase64 As String, ByVal password As String) As String
        Dim des As New TripleDESCryptoServiceProvider()
        des.IV = New Byte(7) {}
        Dim pdb As New PasswordDeriveBytes(password, New Byte(-1) {})
        des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
        Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedBase64)
        Dim ms As New MemoryStream(encryptedBase64.Length)
        Dim decStream As New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)
        decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
        decStream.FlushFinalBlock()
        Dim plainBytes As Byte() = New Byte(ms.Length - 1) {}
        ms.Position = 0
        ms.Read(plainBytes, 0, CInt(ms.Length))
        decStream.Close()
        Return (Encoding.UTF8.GetString(plainBytes))
    End Function

End Class