7,763
社区成员
发帖
与我相关
我的任务
分享
Public Function EncryptStringToBytes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
If plainText Is Nothing OrElse plainText.Length <= 0 Then
Throw New ArgumentNullException("plainText")
End If
If Key Is Nothing OrElse Key.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
If IV Is Nothing OrElse IV.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
Dim encrypted() As Byte
Using rijAlg As New RijndaelManaged()
rijAlg.Key = Key
rijAlg.IV = IV
Dim encryptor As ICryptoTransform = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV)
Using msEncrypt As New MemoryStream()
Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
Using swEncrypt As New StreamWriter(csEncrypt)
swEncrypt.Write(plainText)
End Using
encrypted = msEncrypt.ToArray()
End Using
End Using
End Using
Return encrypted
End Function
Public Function DecryptStringFromBytes(ByVal cipherText() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
Throw New ArgumentNullException("cipherText")
End If
If Key Is Nothing OrElse Key.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
If IV Is Nothing OrElse IV.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
Dim plaintext As String = Nothing
Using rijAlg As New RijndaelManaged
rijAlg.Key = Key
rijAlg.IV = IV
Dim decryptor As ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV)
Using msDecrypt As New MemoryStream(cipherText)
Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Using srDecrypt As New StreamReader(csDecrypt)
plaintext = srDecrypt.ReadToEnd()
End Using
End Using
End Using
End Using
Return plaintext
End Function
Private Const base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
''======================================================================
''##### 加密成可见字符 #####
Private Function Base64_Encode(DecryptedText As String) As String
Dim c1, c2, c3 As Integer
Dim w1 As Integer
Dim w2 As Integer
Dim w3 As Integer
Dim w4 As Integer
Dim N As Long
Dim retry As String
For N = 1 To Len(DecryptedText) Step 3
c1 = Asc(Mid$(DecryptedText, N, 1))
c2 = Asc(Mid$(DecryptedText, N + 1, 1) + Chr$(0))
c3 = Asc(Mid$(DecryptedText, N + 2, 1) + Chr$(0))
w1 = Int(c1 / 4)
w2 = (c1 And 3) * 16 + Int(c2 / 16)
If Len(DecryptedText) >= N + 1 Then w3 = (c2 And 15) * 4 + Int(c3 / 64) Else w3 = -1
If Len(DecryptedText) >= N + 2 Then w4 = c3 And 63 Else w4 = -1
retry = retry + mimeencode(w1) + mimeencode(w2) + mimeencode(w3) + mimeencode(w4)
Next
Base64_Encode = retry
End Function
''##### 解密为原来的数据 #####
Private Function Base64_Decode(A As String) As String
Dim w1 As Integer
Dim w2 As Integer
Dim w3 As Integer
Dim w4 As Integer
Dim N As Long
Dim retry As String
For N = 1 To Len(A) Step 4
w1 = mimedecode(Mid$(A, N, 1))
w2 = mimedecode(Mid$(A, N + 1, 1))
w3 = mimedecode(Mid$(A, N + 2, 1))
w4 = mimedecode(Mid$(A, N + 3, 1))
If w2 >= 0 Then retry = retry + Chr$(((w1 * 4 + Int(w2 / 16)) And 255))
If w3 >= 0 Then retry = retry + Chr$(((w2 * 16 + Int(w3 / 4)) And 255))
If w4 >= 0 Then retry = retry + Chr$(((w3 * 64 + w4) And 255))
Next
Base64_Decode = retry
End Function
Private Function mimedecode(A As String) As Integer
If Len(A) = 0 Then mimedecode = -1: Exit Function
mimedecode = InStr(base64, A) - 1
End Function
Private Function mimeencode(w As Integer) As String
If w >= 0 Then mimeencode = Mid$(base64, w + 1, 1) Else mimeencode = ""
End Function