Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: ASP.NET FAQ


Encrypting your strings by Zarcom
Posted: 29 Aug 02 (Edited 29 Jan 03)

Although this question hasn't been asked as often as some others, it seems to take a while to explain so I am writing an FAQ about it.

.NET has provided some very useful classes to encrypt your data with.  I found that these classes are .. well rather confusing.  It takes a fair amount of time to set up an encryptor to encrypt a string if you haven't used them before.  So I wrote a Class of my own that has two functions.
Encrypt and

The encrypt function takes in a string and sends back the encrypted string.

The Decrypt function takes in a encypted string and sends back the orignal.  The decrypt function accepts only a string that has already been encrypted or it will send back an error message.

Here is the class

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Public Class Encryptor
    ' Use DES CryptoService with Private key pair
    Private mSEncryptionKey As String = "encryptstring"
    Private mIV() As Byte = {&H25, &H29, &H93, &H27, &H52, &HFD, &HAE, &HBC}
    Private mkey() As Byte = {}

#Region " Public Properties "

    Public WriteOnly Property SEncryptionKey() As String
        Set(ByVal Value As String)
            mSEncryptionKey = Value
        End Set
    End Property

    Public WriteOnly Property IV() As Byte()
        Set(ByVal Value As Byte())
            mIV = Value
        End Set
    End Property

#End Region

#Region " Public Functions "

    Public Sub New()

    End Sub

    Public Sub New(ByVal nEncryptionKey As String, ByVal nIV As Byte())
        SEncryptionKey = nEncryptionKey
        IV = nIV
    End Sub

    Public Function Decrypt(ByVal stringToDecrypt 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

            mkey = System.Text.Encoding.UTF8.GetBytes(Left(mSEncryptionKey, 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(mkey, mIV), 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 Encrypt(ByVal stringToEncrypt As String) As String
            mkey = System.Text.Encoding.UTF8.GetBytes(Left(mSEncryptionKey, 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(mkey, mIV), 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 Region

End Class

In this class I used the DES encryption algorithm.  Any algorithm worth anything doesn't use a static equation.  You or I could easily write an equation that turns normal text to something else.
Ex turn the text to assci and add 1.

The DES algorithmm takes two parameters.  The main encryption key and a second key.  The second key encrypts the first part of the encrypted string farther to prevent any ease of breaking the code.

If you use the Class as it is anyone that visits this forum can break your encryted data.  If you notice at the top of the class I included these lines

' Use DES CryptoService with Private key pair
    Private mSEncryptionKey As String = "encryptstring"
    Private mIV() As Byte = {&H25, &H29, &H93, &H27, &H52, &HFD, &HAE, &HBC}
    Private mkey() As Byte = {}

The mSEncryptionKey is any string you want to be your key.  Change it to your hearts content, I have put a default value into the class that will be used should you not set the key.

THE mIV array is an array of bytes.  The numbers you see there are in hexidecimal form.  I am assuming you know what hexadecimal is. This is the secondary key I was speaking of.  Change the array to anything you want as long as it remains a hex number.  Note leave the '&' signs

quick note about hex. It is a number system that runs from 0-9 and A-F.  F being 16 and A 11 so to speak.

To Call this class use the following code where it is needed.

dim myEncryptor as new Encryptor
dim EncryptedString as string

EncryptedString = myEncryptor.Encrypt(textbox1.text)

textbox2.text = myEncryptor.Decrypt(EncryptedString)

myEncryptor = nothing

To reiterate this uses the default key and IV which anyone reading this using this FAQ will know so I suggest changing them.

Hope everyone likes it.  

Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close