2010-04-13 51 views
0

第一篇文章在這裏。簡單而安全的加密/解密asp到asp.net

我有一個asp/vb6網絡應用程序,登錄用戶我想加密用戶標識字段並將它傳遞(查詢字符串)到一個asp.net應用程序,然後解密它做一個數據庫查找。

我已經google'd它,當然,發現rot13,不夠安全。我還在MD5/RC4上發現了一些命中,但沒有找到任何加密/解密的好例子。

感謝,

邁克爾

+0

簡單策略:需要通過SSL進行登錄。 – Brian 2010-04-13 20:25:07

回答

1

人們普遍承認,你不應該這樣的解密信息,而是比較加密加密。例如,MD5可以用於這種「陷門」的時尚。編碼信息,然後存儲MD5散列。當你需要認證時,編碼新的信息並比較散列。未加密的信息從不公開或不可用。

如果這不適合您的情況,請查看Windows Crypto API,該API提供允許全週期加密/解密的備選方案。

1

我同意@布賴恩 - 不要去做自己的加密,加密很容易,直到你開始做密鑰管理。除非你有非常好的V.E.R.Y.否則請使用SSL/TLS。很好的理由不這樣做。

0

這是一個基本的加密示例。你會想找出自己的鑰匙。我這樣做是爲了增加一層複雜性(我希望)。正如Jim指出的那樣,您可以使用它來加密新密碼,然後存儲結果。在創建密碼而不是試圖解密該值(這恰恰相反)之後,您將加密輸入的密碼並將其與存儲的值進行比較。

'combine these constants to build the encryption key' 
Private Const KEY1 = "abcde" 
Private Const KEY2 = "fghij" 
Private Const KEY3 = "klmno" 
Private Const KEY4 = "pqrst" 
Private Const KEY5 = "uvwxy" 

Private Function Encrypt(ByVal s As String, ByVal EncryptionType As CAPICOM.CAPICOM_ENCODING_TYPE) As String 
    Dim oEN As New CAPICOM.EncryptedData 
    Dim intENCType As CAPICOM.CAPICOM_ENCRYPTION_ALGORITHM 
    Dim strSecret As String 
    Dim intTries As Integer 

    On Error GoTo errEncrypt 

    intENCType = CAPICOM_ENCRYPTION_ALGORITHM_AES ' try this first and fall back if not supported' 

    With oEN 
startEncryption: 
     .Algorithm = intENCType 
     strSecret = KEY2 & KEY5 & KEY4 & KEY1 & KEY3 
     .SetSecret strSecret 
     strSecret = "" 
     .Content = s 
     ' the first encryption type needs to be base64 as the .content property' 
     ' can loose information if I try to manipulate a binary string' 
     .Content = StrReverse(.Encrypt(CAPICOM_ENCODE_BASE64)) 
     strSecret = KEY1 & KEY4 & KEY3 & KEY2 & KEY5 
     .SetSecret strSecret 
     strSecret = "" 
     Encrypt = .Encrypt(EncryptionType) 
    End With 

    Set oEN = Nothing 

    Exit Function 

errEncrypt: 
    If Err.Number = -2138568448 Then 
     ' if this is the first time the step the encryption back and try again 
     If intTries < 1 Then 
     intTries = intTries + 1 
     intENCType = CAPICOM_ENCRYPTION_ALGORITHM_3DES 
     Resume startEncryption 
     End If 
    End If 

    Err.Raise Err.Number, Err.Source & ":Encrypt", Err.Description 
    strSecret = "" 
    Set oEN = Nothing 

End Function