我正在研究用于加密和解密字符串的标准 3DES 128 位。它在两种不同的环境中实现;经典的 ASP (VB6) 和 .Net。
我认为 3DES 是标准的,但是,我无法使其在这两者(capicom 和 .Net)之间兼容。谷歌搜索后,我意识到 capicom 有一些自己定制的魔法。
据我了解,3DES 需要两个指定的 128 位(8 字节)密钥(密码)和8 个字节的 "IV"。所以,我找不到任何可以在 capicom 中指定“IV”的 8 个字节的地方,因此,我怀疑这会导致差异。
.Net 系统.安全.密码学
Dim m_des As New TripleDESCryptoServiceProvider
...
Private ReadOnly str_key As String = "MyPwd1234567890p"
Private ReadOnly iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}
Private m_utf8 As New UTF8Encoding
Private m_key() As Byte
Private m_iv() As Byte
Public Sub New(ByVal key() As Byte, ByVal iv() As Byte)
Me.m_key = key
Me.m_iv = iv
End Sub
' VB.NET to convert a string to a byte array
Public Shared Function StrToByteArray(ByVal str As String) As Byte()
Dim encoding As New System.Text.UTF8Encoding()
Return encoding.GetBytes(str)
End Function
Public Function Encrypt(ByVal input() As Byte) As Byte()
Return Transform(input, m_des.CreateEncryptor(StrToByteArray(str_key), m_iv))
End Function
Capicom 在 VB6 环境中:
Dim DecryptData
Set DecryptData = CreateObject("CAPICOM.EncryptedData")
Dim sDAta
sData = "Hello World"
DecryptData.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS
DecryptData.Algorithm.Name = CAPICOM_ENCRYPTION_ALGORITHM_3DES
DecryptData.SetSecret("MyPwd1234567890p")
result = DecryptData.Decrypt(sData)
希望您能帮忙提供一些想法。