Converting CryptAcquireContext -> System.Security.Cryptography.SymmetricAlgorithm

From: HaukiDog (HaukiDog_at_hotmail.com)
Date: 04/25/03


Date: 25 Apr 2003 14:31:25 -0700


I am in the process of mapping a old school asp application over to a
asp.net world. I would like to do this cleanly and not use any calls
into COM+ dlls, or Dll imports.

Since I have many users with encrypted passwords used on there system
I need to have the code use the exact same algorithm. The system uses
a simple Symetric algorithm, with the private key stored in the db, of
course. I am familiar with the
System.Security.Cryptography.SymmetricAlgorithm class, but am not sure
how to map my exisiting algorithm to one of the readily available
derived algorithms: DES, RC2, Rijndael, or TripleDES.

In my current code I am using CryptAcquireContext from the
advapi32.dll. In a nutshell (in VB).

Here are the function declarations:
  Private Declare Function CryptAcquireContext Lib "advapi32.dll"
Alias "CryptAcquireContextA" ( ByRef phProv As Integer, ByVal
pszContainer As Integer, ByVal pszProvider As String, ByVal dwProvType
As Integer, ByVal dwFlags As Integer) As Integer

  Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal
hProv As Integer, ByVal Algid As Integer, ByVal hKey As Integer, ByVal
dwFlags As
Integer, ByRef phHash As Integer) As Integer

  Private Declare Function CryptHashData Lib "advapi32.dll" (ByVal
hHash As Integer, ByVal pbData As String, ByVal dwDataLen As Integer,
ByVal dwFlags
As Integer) As Integer

  Private Declare Function CryptDeriveKey Lib "advapi32.dll" ( ByVal
hProv As Integer, ByVal Algid As Integer, ByVal hBaseData As Integer,
ByVal dwFlags As Integer, ByRef phKey As Integer) As Integer

  Private Declare Function CryptDestroyHash Lib "advapi32.dll" ( ByVal
hHash As Integer) As Integer

  Private Declare Function CryptDestroyKey Lib "advapi32.dll" (ByVal
hKey As Integer) As Integer

  Private Declare Function CryptEncrypt Lib "advapi32.dll" (ByVal hKey
As Integer, ByVal hHash As Integer, ByVal Final As Integer, ByVal
dwFlags As Integer, ByVal pbData As String, ByRef pdwDataLen As
Integer, ByVal dwBufLen As Integer) As Integer

  Private Declare Function CryptDecrypt Lib "advapi32.dll" (ByVal hKey
As Integer, ByVal hHash As Integer, ByVal Final As Integer, ByVal
dwFlags As
Integer, ByVal pbData As String, ByRef pdwDataLen As Integer) As
Integer

  Private Declare Function CryptReleaseContext Lib "advapi32.dll"
(ByVal hProv As Integer, ByVal dwFlags As Integer) As Integer

Here are the defines used to set up the algorithm:

  Private Const PROV_RSA_FULL = 1
  Private Const ALG_CLASS_DATA_ENCRYPT = 24576
  Private Const ALG_CLASS_HASH = 32768

  Private Const ALG_TYPE_ANY = 0
  Private Const ALG_TYPE_BLOCK = 1536
  Private Const ALG_TYPE_STREAM = 2048

  Private Const ALG_SID_RC2 = 2

  Private Const ALG_SID_RC4 = 1
  Private Const ALG_SID_MD5 = 3
  Private Const CALG_MD5 = ((ALG_CLASS_HASH Or ALG_TYPE_ANY) Or
ALG_SID_MD5)
  Private Const CALG_RC2 = ((ALG_CLASS_DATA_ENCRYPT Or ALG_TYPE_BLOCK)
Or ALG_SID_RC2)
  Private Const CALG_RC4 = ((ALG_CLASS_DATA_ENCRYPT Or
ALG_TYPE_STREAM) Or ALG_SID_RC4)

  Private Const ENCRYPT_ALGORITHM = CALG_RC4
  Private Const ENCRYPT_BLOCK_SIZE = 1

And here is the series of commands (not full code):
  CryptAcquireContext(lngHCryptprov, kContainerName, MS_DEF_PROV,
PROV_RSA_FULL, CRYPT_MACHINE_KEYSET Or CRYPT_NEWKEYSET))
  CryptCreateHash(lngHCryptprov, CALG_MD5, 0, 0, lngHHash)
  CryptHashData(lngHHash, Key, Key.Length, 0)
  CryptDeriveKey(lngHCryptprov, ENCRYPT_ALGORITHM, lngHHash, 0,
lngHkey)
  CryptEncrypt(lngHkey, 0, 1, 0, strCryptBuffer, lngCryptLength,
lngCryptBufLen)



Relevant Pages

  • Re: Netzlaufwerk verbinden und trennen
    ... #Region " Network via API " ... Private Const CONNECT_INTERACTIVE As Long = &H8 ... As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, _ ... ByVal dwFlags As _ ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: using PlaySound within a dll
    ... ByVal hModule As Long, _ ... ByVal dwFlags As Long) As Long ... Private Const SND_RESOURCE = &H40004 ... Private Sub Command1_Click ...
    (microsoft.public.vb.winapi)
  • Re: Saving Userform as Gif
    ... bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) ... Private Const KEYEVENTF_KEYUP = &H2 ... > Or use image editing software to take screenshots. ... I use Paint Shop> Pro a lot for taking screenshots of forms, ...
    (microsoft.public.excel.programming)