Public Sub MD5Encrypt(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
Dim fsInput As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'读取文件到数组
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
fsInput.Close()
fsInput.Dispose()
Dim fsEncrypted As New FileStream(sOutputFilename, FileMode.Create, FileAccess.Write)
'创建DES加密码实例
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
'
Dim cryptostream As New CryptoStream(fsEncrypted, _
desencrypt, _
CryptoStreamMode.Write)
Public Sub MD5Decrypt(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
Dim str As String = ""
Dim DES As New DESCryptoServiceProvider()
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'读取加密文件
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
Str = New StreamReader(cryptostreamDecr).ReadToEnd
cryptostreamDecr.Close()
cryptostreamDecr.Dispose()
'输出到解密文件
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(str)
fsDecrypted.Flush()
fsDecrypted.Close()
Public Shared Function GetDecryptString(ByVal input As String, ByVal pass As String, ByRef output As String) As Boolean
Dim mHelper As New Helper(pass)
Try
output = mHelper.DecryptData(input)
Return True
Catch ex As Exception
Return False
End Try
End Function
相应的,使用代码修改如下:
Dim nowString As String = Nothing
Dim isOk As Boolean = String3Des.GetDecryptString(encryptString, pass, nowString)
If isOk Then
print("正确解密后的连接字串是:", nowString)
Else
print("不能正确解密.错误密码是:", pass)
End If
Public Shared Function GetEncryptString(ByVal input As String, ByVal pass As String) As String
Dim mHelper As New Helper(pass)
Return mHelper.EncryptData(input)
End Function
Public Shared Function GetDecryptString(ByVal input As String, ByVal pass As String) As String
Dim mHelper As New Helper(pass)
Return mHelper.DecryptData(input)
End Function
Private Class Helper
Private TripleDes As New TripleDESCryptoServiceProvider
Sub New(ByVal key As String)
' Initialize the crypto provider.
TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
End Sub
Public Function EncryptData( _
ByVal plaintext As String) _
As String
' Convert the plaintext string to a byte array.
Dim plaintextBytes() As Byte = _
System.Text.Encoding.Unicode.GetBytes(plaintext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the encoder to write to the stream.
Dim encStream As New CryptoStream(ms, _
TripleDes.CreateEncryptor(), _
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
encStream.FlushFinalBlock()
' Convert the encrypted stream to a printable string.
Return Convert.ToBase64String(ms.ToArray)
End Function
Private Function TruncateHash( _
ByVal key As String, _
ByVal length As Integer) _
As Byte()
Dim sha1 As New SHA1CryptoServiceProvider
' Hash the key.
Dim keyBytes() As Byte = _
System.Text.Encoding.Unicode.GetBytes(key)
Dim hash() As Byte = sha1.ComputeHash(keyBytes)
' Truncate or pad the hash.
ReDim Preserve hash(length - 1)
Return hash
End Function
Public Function DecryptData( _
ByVal encryptedtext As String) _
As String
' Convert the encrypted text string to a byte array.
Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
' Create the stream.
Dim ms As New System.IO.MemoryStream
' Create the decoder to write to the stream.
Dim decStream As New CryptoStream(ms, _
TripleDes.CreateDecryptor(), _
System.Security.Cryptography.CryptoStreamMode.Write)
' Use the crypto stream to write the byte array to the stream.
decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
decStream.FlushFinalBlock()
' Convert the plaintext stream to a string.
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Test.Run()
End Sub
End Class
Public Class Test
Public Shared Sub Run()
Dim connectionString As String = _
"Data Source=192.168.0.1\SQLExpress;Initial Catalog=Wmp;" & _
"Persist Security Info=True;User ID=Admin88;Password=htwosdi $ksdfdf1242343"
'***************************************************
' Encipher the text using the pasword.
' 此函数的作用为加密字符串
' from_text:欲加密的字符串
' PassWord:密匙
'***************************************************
Public Function strEncrypt(ByVal from_text As String, ByVal password As String) As String
Dim offset As Long
Dim str_len As Integer
Dim i As Integer
Dim ch As Integer
str_len = Len(from_text)
For i = 1 To str_len
ch = Asc(Mid$(from_text, i, 1))
If ch >= MIN_ASC And ch <= MAX_ASC Then
ch = ch - MIN_ASC
offset = Int((NUM_ASC + 1) * Rnd)
ch = ((ch + offset) Mod NUM_ASC)
ch = ch + MIN_ASC
strEncrypt = strEncrypt & Chr$(ch)
End If
Next i
End Function
Public Function DeStrEncrypt(ByVal from_text As String, ByVal password As String) As String
Dim offset As Long
Dim str_len As Integer
Dim i As Integer
Dim ch As Integer
str_len = Len(from_text)
For i = 1 To str_len
ch = Asc(Mid$(from_text, i, 1))
If ch >= MIN_ASC And ch <= MAX_ASC Then
ch = ch - MIN_ASC
offset = Int((NUM_ASC + 1) * Rnd)
ch = ((ch - offset) Mod NUM_ASC)
If ch < 0 Then ch = ch + NUM_ASC
ch = ch + MIN_ASC
DeStrEncrypt = DeStrEncrypt & Chr$(ch)
End If
Next i
End Function
Public Function NumericPassword(ByVal password As String) As Long
Dim value As Long
Dim ch As Long
Dim shift1 As Long
Dim shift2 As Long
Dim i As Integer
Dim str_len As Integer
str_len = Len(password)
For i = 1 To str_len
ch = Asc(Mid$(password, i, 1))
value = value Xor (ch * 2 ^ shift1)
value = value Xor (ch * 2 ^ shift2)
shift1 = (shift1 + 7) Mod 19
shift2 = (shift2 + 13) Mod 23
Next i
NumericPassword = value
End Function
Private Sub Command1_Click()
Text2.Text = strEncrypt(Text1.Text, "123456")
End Sub
Private Sub Command2_Click()
Text3.Text = DeStrEncrypt(Text2.Text, "123456")
End Sub