Imports System.IO Imports System.Text Imports System.Security.Cryptography
Public Class DES '--加密函数-- 'strEncrKey必须是8位或以上 Public Function Encrypt(ByVal data As String, ByVal strEncrKey As String) As String Try Dim bytes() As Byte = Encoding.Unicode.GetBytes(data) Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8)) Dim iv() As Byte = {1, 2, 3, 4, 5, 6, 7, 8} Dim des As New DESCryptoServiceProvider Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(data) Dim ms As New MemoryStream Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, iv), CryptoStreamMode.Write) cs.Write(inputByteArray, 0, inputByteArray.Length) cs.FlushFinalBlock() Return Convert.ToBase64String(ms.ToArray()) Catch MsgBox(Err.Description) Return "" End Try End Function
'--解密函数-- 'strEncrKey必须是8位或以上 Public Function Decrypt(ByVal data As String, ByVal strEncrKey As String) As String Try Dim bytes() As Byte = Encoding.Unicode.GetBytes(data) Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8)) Dim iv() As Byte = {1, 2, 3, 4, 5, 6, 7, 8} Dim des As New DESCryptoServiceProvider Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(data) Dim ms As New MemoryStream inputByteArray = Convert.FromBase64String(data) Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, iv), CryptoStreamMode.Write) cs.Write(inputByteArray, 0, inputByteArray.Length) cs.FlushFinalBlock() Return System.Text.Encoding.UTF8.GetString(ms.ToArray()) Catch MsgBox(Err.Description) Return "" End Try End Function End Class