DES Encryption





10
Date Submitted Wed. Nov. 15th, 2006 12:59 PM
Revision 1 of 1
Beginner ecliptic
Tags "Email Address" | "VB.NET"
Comments 0 comments
A simple snippet for using single pass DES encryption. Uses the .NET framework functions. Enjoy!

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

''' <summary>
        '
'' Encrypts the given input using single pass DES encryption.
        ''' </summary>
        '
'' <param name="input">Input string to be encrypted.</param>
        ''' <returns>Encrypted string.</returns>
        '
'' <remarks></remarks>
        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


''' <summary>
        '
'' Decrypts the given input using single pass DES decryption.
        ''' </summary>
        '
'' <param name="input">Input string to be decrypted.</param>
        ''' <returns>Decrypted string.</returns>
        '
'' <remarks></remarks>
        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
 

Clay Zuvich

Comments

There are currently no comments for this snippet.

Voting