Build an XMLDOM - Safe DES CryptoStream Class Library in VB.NET

By Peter A. Bromberg, Ph.D.

Peter Bromberg  

One of the common things we need to do when passing our XML documents around is to be able to encrypt key data elements so that they are kept from prying eyes. Since I'm working on a lot of .NET stuff now, I thought I'd take the time to practice my VB.NET to write a little DES CryptoStream class library. The neat thing about this class is that I do a final encoding of the encrypted input string as Base64 so that it can be inserted in an XML document for streaming over http without fear about those nasty little illegal characters that make XML parsers puke. And, when you pass in an encrypted element to be decrypted, your base64 gets unwound automatically so the original encrypted string can be decrypted.

And finally, most people who need to encrypt some text usually want to call a function that accepts text and returns text, so I've set up my library to handle these little houskeeping matters internally. The code I present below is well - commented so you should be able to follow the track of this very simple yet useful class


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

' Namespace: YourCompany.Utils.Encryption
' Uses DES private key and vector to provide HTTP / XMLDOM - safe base64 string encryption
' Encrypted string such as account info, passwords, etc can be safely placed in XML element
' for transmission over the wire without any illegal characters
' Author: Peter Bromberg
' Date:   3/12/02
' Last Modified: 3/12/02

Public Class Encryption64

    ' Use DES CryptoService with Private key pair
    Private key() As Byte = {} ' we are going to pass in the key portion in our method calls
    Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

    Public Function DecryptFromBase64String(ByVal stringToDecrypt As String, ByVal sEncryptionKey As String) As String
        Dim inputByteArray(stringToDecrypt.Length) As Byte
        ' Note: The DES CryptoService only accepts certain key byte lengths
        ' We are going to make things easy by insisting on an 8 byte legal key length

            key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider()
            ' we have a base 64 encoded string so first must decode to regular unencoded (encrypted) string
            inputByteArray = Convert.FromBase64String(stringToDecrypt)
            ' now decrypt the regular string
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            Return encoding.GetString(ms.ToArray())
        Catch e As Exception
            Return e.Message
        End Try
    End Function

    Public Function EncryptToBase64String(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String
            key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider()
            ' convert our input string to a byte array
            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(stringToEncrypt)
            'now encrypt the bytearray
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            ' now return the byte array as a "safe for XMLDOM" Base64 String
            Return Convert.ToBase64String(ms.ToArray())
        Catch e As Exception
            Return e.Message
        End Try
    End Function
End Class

And just in case you're curious but would like to save time, I've created a sample WebForm Application on our server that you can try out HERE.

The CryptoStream classes in the .NET Framework are very powerful and easy to use. They are also very fast. The download ZIP file below contains an entire solution with a "Test Harness" Winforms application, as well as a full copy of the WebForm application project, that will let you try out the class library and put it through its paces. Enjoy!

Download the code that accompanies this article

Peter Bromberg is an independent consultant specializing in distributed .NET solutions Inc. in Orlando and a co-developer of the developer website. He can be reached at