'TripleDES encrypter / Decrypter for files and strings.
'This example is part of Bens Jones encryption codes use as you like.
Option Explicit On
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class cDes
'Class variables.
Private m_Input As String = vbNullString
Private m_Output As String = vbNullString
Private m_Password As String = vbNullString
Public Property InputFile As String
Get
Return m_Input
End Get
Set(ByVal value As String)
m_Input = value
End Set
End Property
Public Property OutputFile As String
Get
Return m_Output
End Get
Set(ByVal value As String)
m_Output = value
End Set
End Property
Public Property Password As String
Get
Return m_Password
End Get
Set(ByVal value As String)
m_Password = value
End Set
End Property
Public Sub DecryptFile()
'Decrypt file.
DoAction(False)
End Sub
Public Sub EncryptFile()
'Encrypt file.
DoAction(True)
End Sub
Public Function EncryptString(ByVal source As String, ByVal Password As String) As String
Dim Bytes() As Byte
Dim Des As New TripleDESCryptoServiceProvider()
Dim Hash As New MD5CryptoServiceProvider()
Dim EnTransForm As ICryptoTransform = Nothing
Try
'Create encrypter.
Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
Des.Mode = CipherMode.ECB
EnTransForm = Des.CreateEncryptor()
'Get bytes from string.
Bytes = Encoding.ASCII.GetBytes(source)
'Return base64 string.
Return Convert.ToBase64String(EnTransForm.TransformFinalBlock(Bytes, 0, Bytes.Length))
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
End Function
Public Function DecryptString(ByVal source As String, ByVal Password As String) As String
Dim Bytes() As Byte
Dim Des As New TripleDESCryptoServiceProvider()
Dim Hash As New MD5CryptoServiceProvider()
Dim EnTransForm As ICryptoTransform = Nothing
Try
'Create decrypter.
Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
Des.Mode = CipherMode.ECB
EnTransForm = Des.CreateDecryptor()
'Get bytes from string.
Bytes = Convert.FromBase64String(source)
'Return string.
Return Encoding.ASCII.GetString(EnTransForm.TransformFinalBlock(Bytes,0,Bytes.Length))
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
End Function
Private Sub DoAction(ByVal Encrypt As Boolean)
Dim br As BinaryReader = Nothing
Dim bw As BinaryWriter = Nothing
Dim Bytes() As Byte
Dim Des As New TripleDESCryptoServiceProvider()
Dim Hash As New MD5CryptoServiceProvider()
Dim EnTransForm As ICryptoTransform = Nothing
'Check for password.
If String.IsNullOrWhiteSpace(Password) Then
Throw New ArgumentException("No Password Found")
End If
Try
br = New BinaryReader(File.OpenRead(InputFile))
'Create hash from password.
Des.Key = Hash.ComputeHash(Encoding.ASCII.GetBytes(Password))
'Set mode.
Des.Mode = CipherMode.ECB
If Encrypt Then
'Create encrypter.
EnTransForm = Des.CreateEncryptor()
Else
'Create decrypter.
EnTransForm = Des.CreateDecryptor()
End If
'Read bytes from input filename.
Bytes = br.ReadBytes(br.BaseStream.Length)
'Encrypt bytes.
Bytes = EnTransForm.TransformFinalBlock(Bytes, 0, Bytes.Length)
'Create output filename.
bw = New BinaryWriter(File.OpenWrite(OutputFile))
'Write bytes to output file.
bw.Write(Bytes)
'Close open files.
bw.Close()
br.Close()
Catch ex As Exception
Throw New ArgumentException(ex.Message)
End Try
End Sub
End Class