I've spent the last two days learning and trying to write a c# class to
reflect what I already have in C++. The code below shows the method I use in
C++ (VS 6) to decrypt our encrypted values.
Expand|Select|Wrap|Line Numbers
- BOOL bResult = TRUE;
- HCRYPTPROV hProv = NULL;
- HCRYPTKEY hKey = NULL;
- HCRYPTKEY hXchgKey = NULL;
- HCRYPTHASH hHash = NULL;
- DWORD dwLength;
- // Get handle to user default provider.
- if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
- CRYPT_VERIFYCONTEXT))
- {
- // Create hash object.
- if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
- {
- // Hash password string.
- dwLength = sizeof(TCHAR)*_tcslen(g_szEncryptionData);
- if (CryptHashData(hHash, (BYTE *)g_szEncryptionData, dwLength, 0))
- {
- // Create block cipher session key based on hash of the password.
- if (CryptDeriveKey(hProv, CALG_RC4, hHash, CRYPT_EXPORTABLE, &hKey))
- {
- // Get the encrypted value from the registry
- dwLength = 32*sizeof(TCHAR);
- DWORD dwType = REG_BINARY;
- if (RegQueryValueEx(m_hKey, lpszValueName, NULL, &dwType,
- (BYTE*)szValue, &dwLength)==ERROR_SUCCESS)
- {
- if (!CryptDecrypt(hKey, 0, TRUE, 0, (BYTE *)szValue, &dwLength))
- bResult = FALSE;
- }
- else
- {
- bResult = FALSE;
- }
- CryptDestroyKey(hKey); // Release provider handle.
- }
- else {
- // Error during CryptDeriveKey!
- bResult = FALSE;
- }
- }
- else
- {
- // Error during CryptHashData!
- bResult = FALSE; }
- CryptDestroyHash(hHash); // Destroy session key.
- }
- else
- {
- // Error during CryptCreateHash!
- bResult = FALSE;
- }
- CryptReleaseContext(hProv, 0);
- }
- return bResult;
the documentation (or lack of) is making it very difficult for me to mirror
the info above. The key is to be able to write the same type of class the can
decrypt the info using a perdetermined key.
I would greatly appreciate it if anyone who has expertise in this area would
help me out here. Please don't hesitate to ask for more info from me.
Where I seem to be stuck is figuring out the parallel process for the
CryptDeriveKey method and how to tell it to use CALG_RC4. I think I have the
previous steps down, i.e., create an object and the md5 hash. Here's a c#
snippet:
Expand|Select|Wrap|Line Numbers
- byte[] md5Hash = new MD5CryptoServiceProvider().ComputeHash(keyHash);
- CspParameters csp = new CspParameters();
- csp.Flags = CspProviderFlags.UseMachineKeyStore;
- csp.ProviderType = 1; // PROV_RSA_FULL
- RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
- byte[] btSignedData = rsa.SignData(keyHash, new MD5CryptoServiceProvider());
- byte[] btEncoded = rsa.Encrypt(btToEnc, false);
using the platform sdk cryptography.
Any ideas or help in resolving this would be greatly appreciated. I've
spent the last two days searching and searching and just can't find anything
to help in this area. I'm starting to glaze over. Thanks again.