Convert from c++ to c#

P

PawelR

Hello Group,
I've keygenerator in C# with function:

int GetHash(CString data, CString& results)
{
USES_CONVERSION;
LPSTR aData=T2A((LPCTSTR)data);

HCRYPTPROV hCryptProv = NULL;
HCRYPTHASH hHash;
BYTE *pbHash;
DWORD pbHashSize;
DWORD dwHashLen = sizeof(DWORD);
int i;
pbHash=NULL;

if(!CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, 0))
{ return 1; }

if (!CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash))
{ return 1; }

if (!CryptHashData( hHash, (BYTE*)aData, strlen(aData), 0))
{ return 1; }

if(!CryptGetHashParam( hHash, HP_HASHSIZE, (BYTE*)&pbHashSize,
&dwHashLen, 0 ))
{ return 1; }

if(!CryptGetHashParam(hHash, HP_HASHVAL, NULL, &dwHashLen, 0))
{ return 1; }

if(pbHash = (BYTE*)malloc(dwHashLen))
{ }
else { return 1; }

CString buf=_T("");
results=_T("");

if(CryptGetHashParam( hHash, HP_HASHVAL, pbHash, &dwHashLen, 0))
{
for(i = 0 ; i < dwHashLen ; i++)
{
buf.Format(_T("%2.2x"), (short unsigned int)pbHash
);
results=results+buf;
}
}
results.MakeUpper();
return 0;
};

How inplement this in C#?
How use wincrypt.h from c++ in c#.

PawelR
 
I

irfan

Hi,

well, first of all, you could convert ur C++ into a COM
DLL and directly call it in C#.

Secondly, you could look on for the support for encryption
and related stuff in .NET's documentation. .NET has many
builtin classes ready to be used...
System.Security.Cryptography namespace..

thirdly, I'm not sure of, but may be PInvoke of .NET
 
B

Bret Mulvey [MS]

PawelR said:
Hello Group,
I've keygenerator in C# with function:

int GetHash(CString data, CString& results)
. . .
How inplement this in C#?

Something like this should work:

using System;
using System.Security.Cryptography;
using System.Text;

class Hash
{
public static string GetHash(string data)
{
MD5 md5 = MD5.Create();
byte[] bytes = Encoding.Unicode.GetBytes(data);
byte[] hash = md5.ComputeHash(bytes);
return BitConverter.ToString(hash).Replace("-", "");
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top