程序 是 asp.net技术内幕中的。是最近一段时间唯一顺畅的程序。没调几次就通了(原来照着打代码都能打错,要不一次就通了),觉得事例代码不错(文件的差不多就不贴了),特贴如下:Imports System.Security.Cryptography
Imports System.Text
Imports System.IOPublic Module myCryptoLib
Public Sub GenerateKey(ByVal Password As String, ByVal blocksize As Integer, ByRef key() As Byte, ByRef IV() As Byte)
Dim i As Integer
Dim data() As Byte = Encoding.ASCII.GetBytes(Password)
Dim hash() As Byte
Dim sha As New SHA1CryptoServiceProvider
Dim len As Integer = blocksize / 8 ReDim key(len - 1)
ReDim IV(len - 1) hash = sha.ComputeHash(data) For i = 0 To len - 1
key(i) = hash(i)
Next For i = len To (2 * len) - 1
IV(i - len) = hash(i)
Next
End Sub
Public Function Encrypt(ByVal s As String, ByVal Password As String) As String
Dim data() As Byte = Encoding.UTF8.GetBytes(s)
Dim des As New DESCryptoServiceProvider
Dim key() As Byte
Dim IV() As Byte GenerateKey(Password, des.BlockSize, key, IV)
des.Key = key
des.IV = IV Dim mout As New MemoryStream
Dim encStream As New CryptoStream(mout, des.CreateEncryptor(), CryptoStreamMode.Write) Dim i As Long For i = 0 To data.Length - 1 Step 4096
encStream.Write(data, i * 4096, data.Length - (i * 4096)) Next encStream.FlushFinalBlock() Dim result(mout.Length - 1) As Byte
mout.Seek(0, SeekOrigin.Begin) mout.Read(result, 0, result.Length) encStream.Close()
mout.Close() Return Convert.ToBase64String(result)
End Function
Public Function Decrypt(ByVal s As String, ByVal Password As String) As String
Dim data() As Byte = Convert.FromBase64String(s)
Dim des As New DESCryptoServiceProvider
Dim key() As Byte
Dim IV() As Byte GenerateKey(Password, des.BlockSize, key, IV)
des.Key = key
des.IV = IV
Dim mout As New MemoryStream
Dim decStream As New CryptoStream(mout, des.CreateDecryptor(), CryptoStreamMode.Write) Dim i As Long For i = 0 To data.Length - 1 Step 4096
decStream.Write(data, i * 4096, data.Length - (i * 4096))
Next decStream.FlushFinalBlock() Dim result(mout.Length - 1) As Byte mout.Seek(0, SeekOrigin.Begin)
mout.Read(result, 0, result.Length) mout.Close() Return Encoding.UTF8.GetString(result)
End Function
End Module使用
"加密后的内容"= Encrypt("要加密的内容", "密钥")
"解密后的内容"= Decrypt("加密后的内容", "密钥")
Imports System.Text
Imports System.IOPublic Module myCryptoLib
Public Sub GenerateKey(ByVal Password As String, ByVal blocksize As Integer, ByRef key() As Byte, ByRef IV() As Byte)
Dim i As Integer
Dim data() As Byte = Encoding.ASCII.GetBytes(Password)
Dim hash() As Byte
Dim sha As New SHA1CryptoServiceProvider
Dim len As Integer = blocksize / 8 ReDim key(len - 1)
ReDim IV(len - 1) hash = sha.ComputeHash(data) For i = 0 To len - 1
key(i) = hash(i)
Next For i = len To (2 * len) - 1
IV(i - len) = hash(i)
Next
End Sub
Public Function Encrypt(ByVal s As String, ByVal Password As String) As String
Dim data() As Byte = Encoding.UTF8.GetBytes(s)
Dim des As New DESCryptoServiceProvider
Dim key() As Byte
Dim IV() As Byte GenerateKey(Password, des.BlockSize, key, IV)
des.Key = key
des.IV = IV Dim mout As New MemoryStream
Dim encStream As New CryptoStream(mout, des.CreateEncryptor(), CryptoStreamMode.Write) Dim i As Long For i = 0 To data.Length - 1 Step 4096
encStream.Write(data, i * 4096, data.Length - (i * 4096)) Next encStream.FlushFinalBlock() Dim result(mout.Length - 1) As Byte
mout.Seek(0, SeekOrigin.Begin) mout.Read(result, 0, result.Length) encStream.Close()
mout.Close() Return Convert.ToBase64String(result)
End Function
Public Function Decrypt(ByVal s As String, ByVal Password As String) As String
Dim data() As Byte = Convert.FromBase64String(s)
Dim des As New DESCryptoServiceProvider
Dim key() As Byte
Dim IV() As Byte GenerateKey(Password, des.BlockSize, key, IV)
des.Key = key
des.IV = IV
Dim mout As New MemoryStream
Dim decStream As New CryptoStream(mout, des.CreateDecryptor(), CryptoStreamMode.Write) Dim i As Long For i = 0 To data.Length - 1 Step 4096
decStream.Write(data, i * 4096, data.Length - (i * 4096))
Next decStream.FlushFinalBlock() Dim result(mout.Length - 1) As Byte mout.Seek(0, SeekOrigin.Begin)
mout.Read(result, 0, result.Length) mout.Close() Return Encoding.UTF8.GetString(result)
End Function
End Module使用
"加密后的内容"= Encrypt("要加密的内容", "密钥")
"解密后的内容"= Decrypt("加密后的内容", "密钥")
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货