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
Decrypt
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
[color green]
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
Try
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)
cs.FlushFinalBlock()
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
Try
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)
cs.FlushFinalBlock()
' 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
[/color]
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
[color green]
' 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 = {}
[/color]
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.
[color green]
dim myEncryptor as new Encryptor
dim EncryptedString as string
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.