DES Encryption VB.NET

By ecliptic

For those who want to use a simple encryption scheme that compiles and does not eat up your processor, I have provided a snippet. Enjoy. It is single pass DES encryption using the .NET framework.


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

'''
''' Encrypts the given input using single pass DES encryption.
'''
''' Input string to be encrypted.
''' Encrypted string.
'''
Public Function Encrypt(ByVal input As String) As String
Dim encryptedString As String = Nothing
Dim desKey() As Byte = {&H1, &H23, &H45, &H67, &H89, &HAB, &HCD, &HEF}
Dim desIV() As Byte = {&H1, &H12, &H23, &H34, &H45, &H56, &H67, &H78}

' assign the byte array
Dim b_inputArray() As Byte = Encoding.UTF8.GetBytes(input)

'more efficient to use DES encryption, but the keys are easier to break since
'limited to only 64 bit encryption
Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
Dim cs As CryptoStream = Nothing

Dim ms As MemoryStream = New MemoryStream()

Try
' write out the string to the memorystream for further use and easy
' conversion to a string value
cs = New CryptoStream(ms, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write)
cs.Write(b_inputArray, 0, b_inputArray.Length)
cs.FlushFinalBlock()

' build the string into a readable format excluding non-generic ascii values
encryptedString = Convert.ToBase64String(ms.ToArray())

Catch ex As Exception
'error handling here
Finally
' clear out the memory
desKey = Nothing
desIV = Nothing
b_inputArray = Nothing
des.Clear()
des = Nothing
cs.Clear()
cs = Nothing
ms.Close()
ms = Nothing
End Try

Return encryptedString

End Function

'''
''' Decrypts the given input using single pass DES decryption.
'''
''' Input string to be decrypted.
''' Decrypted string.
'''
Public Function Decrypt(ByVal input As String) As String
Dim decryptedString As String = Nothing
Dim desKey() As Byte = {&H1, &H23, &H45, &H67, &H89, &HAB, &HCD, &HEF}
Dim desIV() As Byte = {&H1, &H12, &H23, &H34, &H45, &H56, &H67, &H78}

' assign the byte array
Dim b_inputArray() As Byte = Convert.FromBase64String(input)

'more efficient to use DES encryption, but the keys are easier to break since
'limited to only 64 bit encryption
Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider()
Dim cs As CryptoStream = Nothing

Dim ms As MemoryStream = New MemoryStream()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8

Try
' write out the string to the memorystream for further use and easy
' conversion to a string value
cs = New CryptoStream(ms, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write)
cs.Write(b_inputArray, 0, b_inputArray.Length)
cs.FlushFinalBlock()

' build the string into a readable format excluding non-generic ascii values
decryptedString = encoding.GetString(ms.ToArray())

Catch ex As Exception
'error handling here
Finally
' clear out the memory
desKey = Nothing
desIV = Nothing
b_inputArray = Nothing
des.Clear()
des = Nothing
cs.Clear()
cs = Nothing
ms.Close()
ms = Nothing
encoding = Nothing
End Try

Return decryptedString

End Function

One Response to “DES Encryption VB.NET”

  1. patrick o Says:

    Thanks, I was needing simple functions like this.

Leave a Reply