Converting old C++ crypto code to .NET

From: Mark Frishman (mfrishman_nospam_at_hotmail.com)
Date: 02/05/04


Date: Thu, 5 Feb 2004 10:07:43 -0500

If I have the following C++ code:

HCRYPTPROV hCryptProv;
 HCRYPTHASH hHash;
 HCRYPTKEY hKey;
 CReadOnlyVariant bLocPassword(&bPassword);
 CReadOnlyVariant bLocPlainText(&bPlainText);

 *nRC = -1;

 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
  if(CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash)) {
   if(CryptHashData(hHash, bLocPassword.GetData(), bLocPassword.GetSize(),
0)) {
    if(CryptDeriveKey(hCryptProv, CALG_RC4, hHash, 0, &hKey)) {
     DWORD dwBufferLen = bLocPlainText.GetSize();
     DWORD dwDataLen = dwBufferLen;
     BYTE *pbBuffer = (BYTE *) malloc(dwBufferLen);

     memcpy(pbBuffer, bLocPlainText.GetData(), dwBufferLen);

     if(CryptEncrypt(hKey, 0, TRUE, 0, pbBuffer, &dwDataLen, dwBufferLen)) {

What would be the equivalent .NET objects/methods to do this. Obviously I
want my new encryption algorithm to produce the same results as the old one.