2009-09-25 107 views
1

我想使用TripleDES在我的ASP.NET應用程序和另一個開發人員的CF應用程序之間交換加密的數據。TripleDES加密 - .NET和ColdFusion打得不好

這是他的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)代碼,使這項工作?或者失敗了,在兩種環境之間可以更好地工作的另一種方法?

+0

@赫伯 - 只是一個猜測,但也許他們的第四被忽略。無論是否有IV,我都會得到相同的結果。該文件說「[IV] ..該算法必須包含除ECB以外的反饋模式。」這似乎表明,除非您提供不同的反饋模式,否則不會使用IV。 – Leigh 2009-09-25 18:41:41

+0

我曾經和客戶有過同樣的問題(我在CF上,他在.NET上)。我們最終放棄了並使用了AES,併爲我們工作。 – ryber 2009-10-02 12:48:21

回答

0

嘗試使用UTF8編碼進行密碼驗證。

Private IV() As Byte = Encoding.UTF8.GetBytes("password") 
+0

沒有骰子。無論如何,密碼都是ASCII,所以Encoding.UTF8.GetBytes產生與Encoding.Ascii.GetBytes相同的結果。 – 2009-09-25 13:32:40

+0

當你在沒有IV的情況下從VB.NET嘗試它時會發生什麼? – Leigh 2009-09-25 22:16:52

1

默認爲CF似乎是 「DESede/ECB/PKCS5Padding」,但在VB.NET他們是 「DESede/CBC/PKCS7」。我不知道爲什麼,但是兩個填充(PKCS5和PKCS7)似乎是兼容的。

爲了得到它的工作,您可能需要改變模式到VB.NET側「ECB」

Public Function EncryptString(ByVal Input As String) As String 
     Dim buffer() As Byte = Encoding.UTF8.GetBytes(Input) 
     Dim des As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider 
     des.Mode = CipherMode.ECB 
     des.Key = EncryptionKey 
     des.IV = IV 
     Return Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(buffer, 0, buffer.Length())) 
End Function 

.. 變化的CF模式和IV相匹配的VB.NET默認

<cfset variables.theKey = "rpaSPvIvVLlrcmtzPU9/c67Gkj7yL1S5" /> 
    <cfset variables.theIV = javacast("string", "password").getBytes() /> 
    <cfset variables.theAlgorithm = "DESede/CBC/PKCS5Padding"> 
    <cfset variables.theEncoding = "Base64"> 
+0

謝謝,我會試一試。 – 2009-12-01 13:48:54