【发布时间】:2009-12-01 19:08:54
【问题描述】:
我正在尝试使用 TripleDES 在我的 ASP.NET 应用程序和另一个开发人员的 CF 应用程序之间交换加密数据。
这是他的 CF 代码(当然是虚构的密钥和 IV):
<cfset variables.theKey = "rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5">
<cfset variables.theIV = BinaryDecode("password","Base64")>
<cfset variables.theAlgorithm = "DESEDE">
<cfset variables.theEncoding = "Base64">
<cfif IsDefined("form.string") and IsDefined("form.method")>
<cfif form.method is "encrypt">
<cfset variables.theString = encrypt(form.string, variables.theKey, variables.theAlgorithm, variables.theEncoding, variables.theIV)>
</cfif>
<cfif form.method is "decrypt">
<cfset variables.theString = decrypt(form.string, variables.theKey, variables.theAlgorithm, variables.theEncoding, variables.theIV)>
</cfif>
<cfoutput><p>Output: #variables.theString#</cfoutput>
</cfif>
这是我的 VB.NET(我省略了异常处理等):
Private IV() As Byte = ASCIIEncoding.ASCII.GetBytes("password")
Private EncryptionKey() As Byte = Convert.FromBase64String("rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5")
Public Function EncryptString(ByVal Input As String) As String
Dim buffer() As Byte = Encoding.UTF8.GetBytes(Input)
Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
des.Key = EncryptionKey
des.IV = IV
Return Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length()))
End Function
Public Function DecryptString(ByVal Input As String) As String
Dim buffer() As Byte = Convert.FromBase64String(Input)
Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider
des.Key = EncryptionKey
des.IV = IV
Return Encoding.UTF8.GetString(des.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length()))
End Function
我们得到了不同的结果。
想到的显而易见的事情是他使用 Base64 从密码创建 IV,而我使用的是 ASCII - 但如果我这样做
Private IV() As Byte = Convert.FromBase64String("password")
然后 .NET 不高兴,因为它为 IV 获取了一个 6 字节的数组,并且它需要 8 个字节。
任何想法我们做错了什么 - 最好是我可以对我的 (VB.NET) 代码进行更改以使其工作?或者如果做不到这一点,另一种方法可以更好地在两种环境之间运行?
【问题讨论】:
-
@Herb - 只是猜测,但也许他们的 IV 被忽略了。在有或没有 IV 的情况下,我在 CF 中得到相同的结果。文档说“[IV] ..算法必须包含 ECB 以外的反馈模式。”这似乎表明除非您提供不同的反馈模式,否则不使用 IV。
-
我曾经在一个客户端上遇到过同样的问题(我在 CF 上,他在 .NET 上)。我们最终放弃并改用 AES,这对我们有用。
标签: vb.net encryption coldfusion 3des