2009-06-05 56 views
1

我有以下本地代碼(Powerbuilder),使用Crypto API加密字符串。我需要C#代碼來解密加密的數據。有人可以給我一個提示或樣本嗎?使用加密API的本地代碼的.NET等效

感謝,夏侯

private function blob of_encryptdecrypt (blob ablob_data, string as_password, boolean ab_encrypt) 
// ----------------------------------------------------------------------------- 
// SCRIPT:  n_cryptoapi.of_EncryptDecrypt 
// 
// PURPOSE: This function will encrypt/decrypt the blob passed to it. Both 
//     encrypt/decrypt have the same api calls except one so they 
//     are combined to save coding. 
// 
// ARGUMENTS: ablob_data - The blob to be decrypted 
//     as_password - The secret password 
// 
// RETURN:  Blob containing the decrypted data. 
// 
// DATE  PROG/ID  DESCRIPTION OF CHANGE/REASON 
// ---------- --------  ----------------------------------------------------- 
// 12/26/2006 RolandS  Initial Coding 
// ----------------------------------------------------------------------------- 

ULong hCryptProv, hHash, hKey 
ULong lul_datalen, lul_buflen, lul_error 
Blob lblob_buffer, lblob_value 
String ls_msgtext 
string ls_password 

// Get handle to CSP 
If Not CryptAcquireContext(hCryptProv, KEY_CONTAINER, is_cryptoservice, PROV_RSA_FULL, 0) Then 
    If Not CryptAcquireContext(hCryptProv, KEY_CONTAINER, is_cryptoservice, PROV_RSA_FULL, CRYPT_NEWKEYSET) Then 
     of_GetLastError(lul_error, ls_msgtext) 
     SignalError(lul_error, "CryptAcquireContext:~r~n~r~n" + ls_msgtext) 
    End If 
End If 

// Create a hash object 
If Not CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, hHash) Then 
    of_GetLastError(lul_error, ls_msgtext) 
    SignalError(lul_error, "CryptCreateHash:~r~n~r~n" + ls_msgtext) 
End If 

// Hash the password 
If Not CryptHashData(hHash,as_password, Len(as_password), 0) Then 
    of_GetLastError(lul_error, ls_msgtext) 
    SignalError(lul_error, "CryptHashData:~r~n~r~n" + ls_msgtext) 
End If 

// Derive a session key from the hash object 
If Not CryptDeriveKey(hCryptProv, ENCRYPT_ALGORITHM, hHash, 0, hKey) Then 
    of_GetLastError(lul_error, ls_msgtext) 
    SignalError(lul_error, "CryptDeriveKey:~r~n~r~n" + ls_msgtext) 
End If 

// allocate buffer space 
lul_datalen = Len(ablob_data) 
lblob_buffer = ablob_data + Blob(Space(8)) 
lul_buflen = Len(lblob_buffer) 

If ab_encrypt Then 
    // Encrypt data 
    If CryptEncrypt(hKey, 0, True, 0, lblob_buffer, lul_datalen, lul_buflen) Then 
     lblob_value = BlobMid(lblob_buffer, 1, lul_datalen) 
    Else 
     of_GetLastError(lul_error, ls_msgtext) 
     SignalError(lul_error, "CryptEncrypt:~r~n~r~n" + ls_msgtext) 
    End If 
Else 
    // Decrypt data 
    If CryptDecrypt(hKey, 0, True, 0, lblob_buffer, lul_datalen) Then 
     lblob_value = BlobMid(lblob_buffer, 1, lul_datalen) 
    Else 
     of_GetLastError(lul_error, ls_msgtext) 
     SignalError(lul_error, "CryptDecrypt:~r~n~r~n" + ls_msgtext) 
    End If 
End If 

// Destroy session key 
If hKey > 0 Then 
    CryptDestroyKey(hKey) 
End If 

// Destroy hash object 
If hHash > 0 Then 
    CryptDestroyHash(hHash) 
End If 

// Release CSP handle 
If hCryptProv > 0 Then 
    CryptReleaseContext(hCryptProv, 0) 
End If 

Return lblob_value 
end function 

回答

2

您可以檢查此網站的PInvoke參考CryptoAPI函數:http://www.pinvoke.net

例如: [的DllImport( 「ADVAPI32.DLL」,SetLastError =真)] 公共靜態的extern BOOL CryptHashData(IntPtr的hHash, byte [] pbData,uint dataLen,uint標誌);

+0

爲什麼你會建議PInvoke的代碼時的.Net已經內置加密庫? – 2009-06-08 07:41:41

+1

是的,.NET有自己的加密庫。但是,這是一個如何從.NET調用相同的win32函數的例子(如果這就是他想要的第一個地方) – Sergiu 2009-06-08 15:47:42

3

你可以在C#中使用System.Security.Cryptography中的類來做同樣的事情。

您正在有效地執行密碼的MD5散列,然後使用全局常量ENCRYPT_ALGORITHM中定義的算法。您必須發佈該變量的值才能獲得更好的答案。但是,如果您使用的是任何常見的東西,那麼您可能會有一個* CryptoServiceProvider包裝器。例如,AesCryptoServiceProvider

順便說一下,使用密碼的直線(例如非鹽漬)MD5散列可能是不好的。有關更多信息,請參閱this article

+0

ENCRYPT_ALGORITHM實際上具有值CALG_RC4。問題是.NET中沒有RC4CrypteServiceProvicer。現在將其更改爲RC2,並通過鏈接提到的示例Tjipke的幫助使其工作。 – JaapM 2009-06-08 11:27:48