tags:

views:

565

answers:

1
DWORD nSize;
LPBYTE lpData;
HCRYPTKEY hPublicKey;


nSize = ReadFromFile(lpszUserPublicKey, NULL);

if(nSize == -1)
 return FALSE;

lpData = new BYTE[nSize];

ReadFromFile(lpszUserPublicKey, lpData);

if(!CryptImportKey(hProv, lpData, nSize, NULL, 0, &hPublicKey)) {
 delete lpData;
 return FALSE;
}

Erase(lpData, nSize);

// Get the data size(&nSize)
if(!CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, NULL, &nSize))
 return FALSE;

lpData = new BYTE[nSize];

CryptExportKey(hKey, hPublicKey, SIMPLEBLOB, 0, lpData, &nSize);

if(WriteToFile(lpszLicenseFile, lpData, nSize) == -1) {
 delete lpData;
 return FALSE;
}

delete lpData;

return CryptDestroyKey(hPublicKey);

How would the above code be written in C#. I am particularily interested in the Crypto API calls. Note, the encryption method that is used is RSA

+1  A: 

This codeproject article seems it fits your needs. As shown in the article, C# has a RSACryptoServiceProvider Class in System.Security.Cryptography to make things a little easier so you don't have to roll an entire solution and translate all of that code manually.

John T
Thanks for the comment. I know how to use the RSACryptoServiceProvider. But what are the equivalent methods on the RSACryptoServiceProvider class to the above Crypto API calls.
You can view all of that using .NET Reflector from http://www.red-gate.com/products/reflector/ :)
John T