Your Ad Here

The .NET Framework also contains a TripleDES class, which is an improved version of
the old DES standard. TripleDES takes longer to encrypt and decrypt information,
but is much more secure than DES.
A Plain-Text Message Is Encrypted with the DES Algorithm Using a User-
Specified Password.

<%@ Import Namespace=”System.Security.Cryptography” %>

<%@ Import Namespace=”System.Text” %>

<script language=”VB” runat=”server”>

Dim aConstantIV() as Byte = _

{ &Haa, &Hbb, &Hcc, &Hdd, &Hee, &Hff, &H12, &H78 }

Sub btnSubmit_OnClick(source as Object, e as EventArgs)

Dim strEncryptedText as String = DESEncrypt(txtContents.Text)

lblResults.Text = “<b>Encrypted String:</b><xmp>” & _

strEncryptedText & “</xmp><p><i>” & _

“Copy this encrypted information to the clipboard! To decrypt“

& _

“it visit <a href=””Listing2.11.2.aspx””>Listing2.11.2.aspx</a>”

End Sub

Function GetKey(strPassword as String) as Byte()

‘Ensure that the strPassword string is at least 8 characters long

strPassword = strPassword.PadRight(9)

‘Now, convert the string into a byte array

Dim objUTF8 as New UTF8Encoding()

GetKey = objUTF8.GetBytes(strPassword.Substring(1, 8))

End Function

Function DESEncrypt(strEncrypt as String) as String

Dim aKey() as Byte

Dim objUTF8 as New UTF8Encoding()

‘Convert the password into a byte array

aKey = GetKey(txtPassword.Text)

‘Create an instance of the DES class

Dim objDES as DES = DES.Create()

objDES.Key = aKey

objDES.IV = aConstantIV

Convert the string into an array of bytes

Dim aByteData() as Byte = objUTF8.GetBytes(strEncrypt)

Dim objStreamEnc as SymmetricStreamEncryptor = objDES.CreateEncryptor()

Dim objCryptoStream as New CryptoMemoryStream()




Represent the byte array as a string

Dim i as Integer

Dim strTmp as String = “”

For i = 0 to objCryptoStream.Data.Length - 1

strTmp &= objCryptoStream.Data(i).ToString() & “ “

Next i

DESEncrypt = strTmp

End Function




<form runat=”server”>

<h1>Encrypt Information!</h1>

<b>Enter a Password:</b>

<asp:textbox id=”txtPassword” TextMode=”Password” runat=”server” />

<br><b>Enter text to encrypt:</b><br>

<asp:textbox id=”txtContents” TextMode=”MultiLine” runat=”server”

Columns=”50” Rows=”6” />

<br><asp:button id=”btnSubmit” runat=”server” Text=”Encrypt!”

OnClick=”btnSubmit_OnClick” />


<asp:label id=”lblResults” runat=”server” />




Recall that cryptography algorithms generally require two inputs: a key and the information to
be encrypted. The DES algorithm (as with many other symmetric algorithms) also require a
third input, an initialization vector (IV) that is used to get the encryption and decryption process started. This information, as well as the key, must be in the form of a Byte array.