老师们好,在vb.net中如何写出和加密过程,使其加密的字符串和foxtable加密的字符串一致
以下运行正常,但加密后的结果和foxtable的不一样,自无法解密还原,该如何修改哪里。我想用其做一个生成配置信息的小程序
Module Program
Sub Main()
' 您提供的密钥和IV
Dim Key As String = "a23" ' 密钥长度需要是16、24或32字节
Dim IV As String = "op#" ' IV长度必须是16字节
' 确保Key和IV的长度正确
Dim keyBytes As Byte() = New Byte(15) {}
Dim ivBytes As Byte() = New Byte(15) {}
' 填充Key和IV数组
'Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(16, " ")), 0, keyBytes, 0, Math.Min(Key.Length, 16))
'Array.Copy(Encoding.UTF8.GetBytes(IV.PadRight(16, " ")), 0, ivBytes, 0, Math.Min(IV.Length, 16))
Dim Val1 As String = "龙的传人"
Dim Val2 As String = EncryptText(Val1, keyBytes, ivBytes) '加密
Dim Val3 As String = DecryptText(Val2, keyBytes, ivBytes) '解密,两个密钥必须和加密的时候相同
Console.WriteLine("加密后:" & Val2)
Console.WriteLine("解密后:" & Val3)
End Sub
'加密函数
Function EncryptText(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As String
Dim encryptProvider As New AesCryptoServiceProvider()
encryptProvider.Key = Key
encryptProvider.IV = IV
Dim inputBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
Using encryptor As ICryptoTransform = encryptProvider.CreateEncryptor()
Dim encryptedBytes() As Byte = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length)
Return Convert.ToBase64String(encryptedBytes)
End Using
End Function
'解密函数
Function DecryptText(ByVal cipherText As String, ByVal Key() As Byte, ByVal IV() As Byte) As String
Dim decryptProvider As New AesCryptoServiceProvider()
decryptProvider.Key = Key
decryptProvider.IV = IV
Dim inputBytes As Byte() = Convert.FromBase64String(cipherText)
Using decryptor As ICryptoTransform = decryptProvider.CreateDecryptor()
Dim decryptedBytes() As Byte = decryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length)
Return Encoding.UTF8.GetString(decryptedBytes)
End Using
End Function
End Module