Problem with decrypting data in .NET
I have data which was encrypted with WIndows Crypto API, code written in VB6.
Now trying to convert the decrypt code to .NET, and i cannot decrypt. I
cannot seem to generate the same key.
THis is a code sample from VB
CryptAcquireContext(hcsp, "strAppName", MS_DEF_PROV, PROV_RSA_FULL, 0)
CryptCreateHash(hcsp, CALG_MD5, 0, 0, hhash)
CryptHashDataString(hhash, pwd,Len(pwd), 0)
CryptDeriveKey(hcsp, CALG_RC2, hhash, CRYPT_EXPORTABLE, hkey)
CryptDestroyHash(hhash)
CryptDecrypt(hkey, 0, 1, 0, VarPtr(vbyteData(0)), lngLenDataBuf)
CryptDestroyKey hkey
Here it is in .NET
'setup CSP
Dim csp As New
System.Security.Cryptography.CspParameters(Providertype.PROV_RSA_FULL,
MS_DEF_PROV, "strAppName")
'Generate the Key
Dim zeroSalt() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} 'see
http://msdn2.microsoft.com/en-US/library/aa387695.aspx
Dim ivNull() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim cdk As New
System.Security.Cryptography.PasswordDeriveBytes(mstrPwd, zeroSalt, csp)
cdk.HashName = "MD5"
Dim key As Byte()
key = cdk.CryptDeriveKey("RC2", "MD5", 0, ivNull)
'Decrypt
Dim rc2CSP As New
System.Security.Cryptography.RC2CryptoServiceProvider
Dim transform As System.Security.Cryptography.ICryptoTransform
transform = rc2CSP.CreateDecryptor(key, ivNull)
Dim vbytDataOut(vintLen - 1) As Byte
vbytDataOut = transform.TransformFinalBlock(vbytData, 0, vintLen)
I did an export on the key in VB6 and tried to compare to the .NET key, and
they do not match. The only other thing i can think of is the password...
The password we are using is 128 abnormal characters, so i was thinking maybe
the way VB6 and .NET handle them going into the API calls is different.
Also, i was trying to find something about the number of iterations on the
derivekey, which i can't determine. The .NET default seems to be 1000, but i
dont see any mention of iteration in the APIs.
Date:Mon, 6 Aug 2007 14:30:03 -0700
Author:
|