The following is a complete class, you can set any password 'DES and md5 encryption and decryption----add a reference to system.web in Add Reference.
Imports?System.Security.Cryptography
Imports?System
Imports?System.Text
Imports?System.Web
p>'''?lt;summarygt;
'''?DES encryption class
'''?lt;/summarygt;
'''?lt;remarksgt;lt;/remarksgt;
Public?Class?DESEncrypt
Public?Sub?DESEncrypt()
End?Sub
Public?Shared?Function?Encrypt(ByVal?Text?As?String)?As?String
Return?Encrypt(Text,?"12345678")
End?Function
Public?Shared?Function?Encrypt(ByVal?Text?As?String,?ByVal?sKey?As?String)?As?String
Dim?des ?As?New?DESCryptoServiceProvider()
Dim?inputByteArray?As?Byte()
inputByteArray?=?Encoding.Default.GetBytes(Text)
des.Key?=?ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,?"md5").Substring(0,?8))
des.IV?= ?ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, ?"md5").Substring(0, ?8))
Dim?ms?As?New?System. IO.MemoryStream()
Dim?cs?As?New?CryptoStream(ms,?des.CreateEncryptor(),?CryptoStreamMode.Write)
cs.Write(inputByteArray,? 0,?inputByteArray.Length)
cs.FlushFinalBlock()
Dim?ret?As?New?StringBuilder()
Dim?b?As? Byte
For?Each?b?In?ms.ToArray()
ret.AppendFormat("{0:X2}",?b)
Next
Return?ret.ToString()
End?Function
Public?Shared?Function?Decrypt(ByVal?Text?As?Str
ing)?As?String
Return?Decrypt(Text,?"12345678")
End?Function
Public?Shared?Function?Decrypt(ByVal ?Text?As?String,?ByVal?sKey?As?String)?As?String
Dim?des?As?New?DESCryptoServiceProvider()
Dim?len?As ?Integer
len?=?Text.Length?/?2
Dim?inputByteArray(len?-?1)?As?Byte
Dim? x,?i?As?Integer
For?x?=?0?To?len?-?1
i?=?Convert.ToInt32(Text.Substring(x ?*?2,?2),?16)
inputByteArray(x)?=?CType(i,?Byte)
Next
des. Key?=?ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,?"md5").Substring(0,?8))
des.IV?=?ASCIIEncoding .ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey,?"md5").Substring(0,?8))
Dim?ms?As?New?System.IO. MemoryStream()
Dim?cs?As?New?CryptoStream(ms,?des.CreateDecryptor(),?CryptoStreamMode.Write)
cs.Write(inputByteArray,?0, ?inputByteArray.Length)
cs.FlushFinalBlock()
Return?Encoding.Default.GetString(ms.ToArray())
End?Function p>
End?Class
'The following is the calling method
Public?Class?Form1
Private?Sub?Button1_Click(ByVal?sender?As ?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click?'Encrypt
Dim?str_Encrypt?As?String?=?DESEncrypt.Encrypt("You want to encrypt The text can be of any length. ",?" The password can be very long. If this parameter is omitted, the default is 12345678")
End?Sub
Private?Sub?Button2_Click( ByVal?sender?As?System
.Object,?ByVal?e?As?System.EventArgs)?Handles?Button2.Click?'Decrypt
Dim?str_Decrypt?As?String?=?DESEncrypt.Decrypt("The text you want to decrypt ,? can be any length ",?" The password used for encryption. If this parameter is omitted, the default is 12345678")
End?Sub