title: Convert Crypto API calls to .NET
body: <p>I have this code in C++ (AnsiString is a wrapper for char* buffer):</p>
  
  <pre><code>BOOL HashIt(AnsiString &amp;pw,BYTE *buf,BYTE &amp;len)
  {
     HCRYPTPROV cp;
     CryptAcquireContext(&amp;cp,NULL,NULL,PROV_RSA_AES,CRYPT_VERIFYCONTEXT);
  
     HCRYPTHASH ch;
     CryptCreateHash(cp,CALG_MD5,0,0,&amp;ch);
  
     CryptHashData(ch,(BYTE*)pw.c_str(),pw.Length(),0);
  
     HCRYPTKEY kc;
     CryptDeriveKey(cp,CALG_3DES,ch,CRYPT_EXPORTABLE,&amp;kc);
     CryptExportKey(kc,NULL,PLAINTEXTKEYBLOB,0,buf,&amp;dwLen);
  
     len = (BYTE) dwLen;
  }
  </code></pre>
  
  <p>So far I've got this far converting it to .NET:</p>
  
  <pre><code>    public static byte[] HashItB(string text)
      {
          byte[] textBytes = System.Text.Encoding.UTF8.GetBytes(text);
          System.Security.Cryptography.MD5CryptoServiceProvider sp = new System.Security.Cryptography.MD5CryptoServiceProvider();
  
          byte[] hashBytes = sp.ComputeHash(textBytes);
          System.Security.Cryptography.PasswordDeriveBytes pdb = new System.Security.Cryptography.PasswordDeriveBytes(hashBytes, new byte[0]);
          hashBytes = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, hashBytes);
  
          // Need to export key as plain text blob here, CryptExportKey
  
          return (hashBytes);
      }
  </code></pre>
  
  <p>Assuming the above code is correct, my last step is to translate the CryptExportKey function to .NET.  Can someone point me to the function or a sample?  I can't change the C++ method and I need my .NET code to match a legacy app.  Or should I give up on .NET methods and either create a C++ DLL and call that (not crazy about that due to extra size of shipping another DLL with my tiny app), or pinvoke all of the Crypto functions?</p>