大家好,我正在尝试将 Java 代码迁移到 VB,现在我需要复制 DES 加密,但我在这部分遇到了问题。
我承认我从大学开始就没有做过加密。
这使用 MD5 加密密钥,并将其发送到 DES 加密函数,似乎我得到了错误的线索,密钥必须是 8 位密钥,而我正在发送 16 位长度的密钥。
Dim MD5 As New MD5CryptoServiceProvider()
Dim dataHash() As Byte = MD5.ComputeHash(Encoding.UTF8.GetBytes(challenge + password))
Dim sb As New StringBuilder
Dim b As Byte
For Each b In dataHash
sb.Append(b.ToString("x2").ToLower())
Next
Dim md5Key As String = sb.ToString
''Dim md5Key As String = digestUtils.md5Hex(challenge + password)
Dim geoEncrypt As New GeoEncriptamiento
Dim challengeAnswer As String = geoEncrypt.EncryptFile(challenge, md5Key)
这是进行加密的代码
Function EncryptFile(ByVal esquema As String, ByVal llave As String) As String
Dim DES As New DESCryptoServiceProvider()
'Establecer la clave secreta para el algoritmo DES.
'Se necesita una clave de 64 bits y IV para este proveedor
DES.Key = UTF8Encoding.UTF8.GetBytes(llave)
DES.IV = UTF8Encoding.UTF8.GetBytes(llave)
Try
Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(esquema)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(MS, DES.CreateEncryptor(DES.Key, DES.IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
Return "Error"
End Try
End Function
错误是当我尝试将 MD5 解析为 DES.Key