How Encryt string with MD5

  • Thread starter Thread starter ralvaradot
  • Start date Start date
Can you give us the details of what you are trying to accomplish? MD5 is
not actually an encryption algorithm, but is instead a hashing algorithm.
So I am guessing that you want to use this as part of communicating with
some other piece of software, that wants an MD5 hash of some data?

That said, you could use the Crypto API (CAPI) to accomplish this. The
basic steps are...

HCRYPTPROV prov;
HCRYPTHASH hash;
BOOL bSuccess;
BYTE rgbResult[16];
DWORD cbResult = sizeof(rgbResult);
bSuccess = CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
bSuccess = CryptCreateHash(prov, CALG_MD5, NULL, 0, &hash);
bSuccess = CryptHashData(hash, pbData, dwDataLen, 0); // pass the
data to be hashed to this call
bSuccess = CryptGetHashParam(hash, HP_HASHVAL, rgbResult, &cbResult, 0);
// rgbResult has the resulting hash value
CryptDestroyHash(hash);
CryptDestroyContext(prov);

This is not intended to be shipping code. It is just intended to give you
the functions you will call, along with the appropriate parameters, to do
MD5. Some bad things about the code above are: Doesn't actually check the
error returns from the functions. Has a hardcoded length of 16 bytes for
the output of the MD5 hash. Hasn't been run through a compiler, so there
might be typo's and such.

--Don
 
I just noticed that this is in the .Net Compact Framework newsgroup, so
presumably you wanted a solution using the .Net CF. The message I posted
was using CAPI, which is a native API.

I know that the normal .Net Framework has the
System.Security.Cryptography.MD5CryptoServiceProvider class, and the MSDN
doc's for that class have a simple example of how to use it. I don't know
if the .Net CF has an implementation of this class.

--Don


Don Dumitru said:
Can you give us the details of what you are trying to accomplish? MD5 is
not actually an encryption algorithm, but is instead a hashing algorithm.
So I am guessing that you want to use this as part of communicating with
some other piece of software, that wants an MD5 hash of some data?

That said, you could use the Crypto API (CAPI) to accomplish this. The
basic steps are...

HCRYPTPROV prov;
HCRYPTHASH hash;
BOOL bSuccess;
BYTE rgbResult[16];
DWORD cbResult = sizeof(rgbResult);
bSuccess = CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
bSuccess = CryptCreateHash(prov, CALG_MD5, NULL, 0, &hash);
bSuccess = CryptHashData(hash, pbData, dwDataLen, 0); // pass the
data to be hashed to this call
bSuccess = CryptGetHashParam(hash, HP_HASHVAL, rgbResult, &cbResult,
0);
// rgbResult has the resulting hash value
CryptDestroyHash(hash);
CryptDestroyContext(prov);

This is not intended to be shipping code. It is just intended to give you
the functions you will call, along with the appropriate parameters, to do
MD5. Some bad things about the code above are: Doesn't actually check
the error returns from the functions. Has a hardcoded length of 16 bytes
for the output of the MD5 hash. Hasn't been run through a compiler, so
there might be typo's and such.

--Don


ralvaradot said:
Hi for All!

How can i do encryp a string with MD5?

Thanks in advance!

RA
 
Back
Top