参考微软?RC2CryptoServiceProvider构造函数中示例,写了个加密解密函数,可以用来进行一般的加解密用。
#Region " rc2Encode 、rc2Decode 加解密 "
'ANSIMode = False '可对任何Unicode字符串加解密
'ANSIMode = True '只对 ANSI 字符串加解密,对英文字符串的加密串长度较短,中文和False时一样
'因为经GetBytes转换时,英文经“系统的当前 ANSI 代码页的编码”编码只有1byte,中文为2byte,
'而经Unicode编码不管英文中文都是2byte。
Public Function rc2Encode(ByVal InStr1 As String, Optional ByVal ANSIMode As Boolean = True) As String
Dim rc2CSP As New RC2CryptoServiceProvider()
' Get the key and IV.
Dim key As Byte() = Encoding.ASCII.GetBytes("1234567890123456")
Dim IV As Byte() = Encoding.ASCII.GetBytes("12345678")
' Get an encryptor.
Dim encryptor As ICryptoTransform = rc2CSP.CreateEncryptor(key, IV)
' Encrypt the data as an array of encrypted bytes in memory.
Dim msEncrypt As New MemoryStream()
Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
' Convert the data to a byte array.
Dim toEncrypt As Byte()
If ANSIMode Then
toEncrypt = Encoding.ASCII.GetBytes(InStr1)
Else
toEncrypt = Encoding.Unicode.GetBytes(InStr1)
End If
' Write all data to the crypto stream and flush it.
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
csEncrypt.FlushFinalBlock()
' Get the encrypted array of bytes.
Dim encrypted As Byte() = msEncrypt.ToArray()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This is where the data could be transmitted or saved.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'将加密的结果以16进制字符保存
Dim tmpstr As New StringBuilder()
For i As Int32 = 0 To encrypted.Length - 1
tmpstr.Append(Microsoft.VisualBasic.Right("0" + Hex(encrypted(i)), 2))
Next
rc2Encode = tmpstr.ToString
End Function
Public Function rc2Decode(ByVal InStr1 As String, Optional ByVal ANSIMode As Boolean = True) As String
Try
'InStr1传入空值或非加密后的字符串,将引发错误。2019.8.16
Dim rc2CSP As New RC2CryptoServiceProvider()
' Get the key and IV.
Dim key As Byte() = Encoding.ASCII.GetBytes("1234567890123456")
Dim IV As Byte() = Encoding.ASCII.GetBytes("12345678")
Dim decryptor As ICryptoTransform = rc2CSP.CreateDecryptor(key, IV)
'将原来以16进制字符保存的加密结果,转换为byte数组,以便解码
Dim encrypted(InStr1.Length / 2 - 1) As Byte
For k As Int32 = 0 To InStr1.Length / 2 - 1
encrypted(k) = CByte(Convert.ToInt16(Mid(InStr1, k * 2 + 1, 2), 16))
Next
' Now decrypt the previously encrypted message using the decryptor
' obtained in the above step.
Dim msDecrypt As New MemoryStream(encrypted)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Dim fromEncrypt() As Byte
fromEncrypt = New Byte(encrypted.Length) {}
'Read the data out of the crypto stream.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
Dim RealLength As Integer
For RealLength = fromEncrypt.Length - 1 To 0 Step -1 '解密后数据为8位的倍数,后面为0的数据为无效数据 2024.1.5
If fromEncrypt(RealLength) <> 0 Then
RealLength += 1
Exit For
End If
Next
'Convert the byte array back into a string.
Dim outstr As String
If ANSIMode Then
Dim newArray(RealLength - 1) As Byte
Array.Copy(fromEncrypt, newArray, RealLength)
outstr = Encoding.ASCII.GetString(newArray)
Else
If RealLength Mod 2 = 1 Then
RealLength += 1
End If
Dim newArray(RealLength - 1) As Byte
Array.Copy(fromEncrypt, newArray, RealLength)
outstr = Encoding.Unicode.GetString(newArray)
End If
rc2Decode = outstr
Catch ex As Exception
rc2Decode = "Error input data"
End Try
End Function
#End Region