问题描述
wstringBinary2Str(PBYTEpbBuffer,DWORDdwLen,WCHARwSeparator){wstringrv;WCHARszItem[3]={0x0};for(DWORDd=0;d<dwLen;d++){swprintf(szItem,L"%02X",pbBuffer[d]);rv.push_back(szItem[0]);rv.push_back(szItem[1]);if(wSeparator&&(d<dwLen-1)){rv.push_back(wSeparator);}}returnrv;}BOOLCalcMD5(PBYTEpbData,DWORDdwDataLen,PBYTEpbBuffer,PDWORDpdwBufferLen){BOOLrv=FALSE;HCRYPTPROVhProv=NULL;HCRYPTHASHhHash=NULL;__try{//检查输入参数并确保输入的缓冲有Byteif(!pbData/*||!dwDataLen*/||!pbBuffer||!pdwBufferLen||*pdwBufferLen<16){__leave;}//获得CSP句柄if(!CryptAcquireContext(&hProv,NULL,MS_DEF_PROV,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT)){__leave;}//创建HASH对象if(!CryptCreateHash(hProv,CALG_MD5,0,0,&hHash)){__leave;}//计算HASH数据if(!CryptHashData(hHash,pbData,dwDataLen,0)){__leave;}//获得HASH值if(!CryptGetHashParam(hHash,HP_HASHVAL,pbBuffer,pdwBufferLen,0)){__leave;}//置成功标志rv=TRUE;}__finally{//关闭HASH对象if(hHash){CryptDestroyHash(hHash);}//关闭CSP句柄if(hProv){CryptReleaseContext(hProv,0);}}returnrv;}wstringCalcMD5(PBYTEpbData,DWORDdwDataLen){wstringrv;BYTEBuffer[16]={0x0};DWORDdwBufferLen=sizeof(Buffer);if(CalcMD5(pbData,dwDataLen,Buffer,&dwBufferLen)){rv=Binary2Str(Buffer,dwBufferLen,L'');}returnrv;}wstringCalcMD5(LPCWSTRlpszString){wstringrv;BYTEBuffer[16]={0x0};DWORDdwBufferLen=sizeof(Buffer);if(lpszString&&CalcMD5((PBYTE)lpszString,(DWORD)wcslen(lpszString)*sizeof(WCHAR),Buffer,&dwBufferLen)){rv=Binary2Str(Buffer,dwBufferLen,L'');}returnrv;}int_tmain(intargc,_TCHAR*argv[]){wstringResult=CalcMD5(L"1");return0;}
解决方案
解决方案二:
求大神转换成c#程序
解决方案三:
md5加密,不管哪种语言实现,原理都是一样的。你度娘找个C#的例子~
解决方案四:
生成文件的MD5,供参考,加密字节数组只需要改变一下参数publicbyte[]GetMD5(stringfileName){byte[]md5array=newbyte[16];if(!File.Exists(fileName))returnmd5array;using(MD5md5=MD5.Create()){using(FileStreamfs=File.OpenRead(fileName)){md5array=md5.ComputeHash(fs);}}returnmd5array;}
解决方案五:
publicstaticstringGetMd5(stringstrIn){using(MD5md5=MD5.Create()){byte[]byteHash=md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strIn));stringstrRes=BitConverter.ToString(byteHash).Replace("-","");returnstrRes.ToLower();}
如果结果不一样那就是你的源代码有特殊处理
解决方案六:
这个是c#md5加密