MD5和VC ++ [英] MD5 and VC++
本文介绍了MD5和VC ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
VC ++中有一个可以进行MD5加密和解密的库吗?
谢谢,
Garyc
Hello,
Is there a library in VC++ that does MD5 encryption and decryption?
Thanks,
Garyc
推荐答案
^ ].
有Windows Cryptography
[
There isWindows Cryptography
[^], for instance.
:)
我有解密示例代码...
这是md5Capi.h代码..... >>>>
//md5Capi.h:Cmd5Capi类的接口.
//
///////////////////////////////////////////////////////////////////////
#if!defined(AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E7B6D1FD7E2__INCLUDED_)
#define AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E7B6D1FD7E2__INCLUDED_
#if _MSC_VER> 1000
#pragma一旦
#endif//_MSC_VER> 1000
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#include< wincrypt.h> //加密API原型和定义
#endif
//加密API原型和定义
Cmd5Capi类
{
公众:
CString& Digest(CString& csBuffer);
CString& GetDigest(void);
Cmd5Capi(CString& csBuffer);
Cmd5Capi();
虚拟〜Cmd5Capi();
CString csDigest;
};
#endif//!defined(AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E7B6D1FD7E2__INCLUDED_)
/////////////////////////////////
和md5Capi.cpp代码>>>>>>>
///////////////////////////////////////////////////////////////////////
//md5Capi.cpp:Cmd5Capi类的实现.
//使用WIN Crypto API计算MD5摘要.
//
///////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"md5Capi.h"
#ifdef _DEBUG
#undef THIS_FILE
静态字符THIS_FILE [] = __ FILE__;
#define new DEBUG_NEW
#endif
///////////////////////////////////////////////////////////////////////
//构造/破坏
///////////////////////////////////////////////////////////////////////
Cmd5Capi :: Cmd5Capi()
{
csDigest.Empty();
}
Cmd5Capi :: Cmd5Capi(CString& csBuffer)
{
摘要(csBuffer);
}
Cmd5Capi ::〜Cmd5Capi()
{
}
CString& Cmd5Capi :: GetDigest(void)
{
返回csDigest;
}
CString& Cmd5Capi :: Digest(CString& csBuffer)
{
HCRYPTPROV hCryptProv;
HCRYPTHASH哈希;
字节bHash [0x7f];
DWORD dwHashLen = 16; //MD5算法始终返回16个字节.
DWORD cbContent = csBuffer.GetLength();
BYTE * pbContent =(BYTE *)csBuffer.GetBuffer(cbContent);
if(CryptAcquireContext(& hCryptProv,
NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
if(CryptCreateHash(hCryptProv,
CALG_MD5,//算法标识符定义,请参见:wincrypt.h
0,0,& hHash))
{
if(CryptHashData(hHash,pbContent,cbContent,0))
{
if(CryptGetHashParam(hHash,HP_HASHVAL,bHash,& dwHashLen,0))
{
//制作数字摘要值的字符串版本
csDigest.Empty();
CString tmp;
for(int i = 0; i< 16; i ++)
{
tmp.Format(%02x",bHash [i]);
csDigest + = tmp;
}
}
else csDigest = _T(获取哈希参数出错");
}
else csDigest = _T(错误哈希数据");
}
else csDigest = _T(创建哈希时出错");
}
else csDigest = _T(错误获取上下文");
CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv,0);
csBuffer.ReleaseBuffer();
返回csDigest;
}
/////////////////////////////////
#include"md5Capi.h"
void EncriptFunction(CString content)
{
Cmd5Capi md5Capi;
CString enc(content);
CString md5coad = md5Capi.Digest(enc);
AfxMessageBox(md5coad);
}
hi i have decryption sample code...
Here is the md5Capi.h code.....>>>>
// md5Capi.h: interface for the Cmd5Capi class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E7B6D1FD7E2__INCLUDED_)
#define AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E7B6D1FD7E2__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#include <wincrypt.h> // Cryptographic API Prototypes and Definitions
#endif
// Cryptographic API Prototypes and Definitions
class Cmd5Capi
{
public:
CString &Digest(CString & csBuffer);
CString &GetDigest(void);
Cmd5Capi(CString & csBuffer);
Cmd5Capi();
virtual ~Cmd5Capi();
CString csDigest;
};
#endif // !defined(AFX_MD5CAPI_H__438D2BEF_6F1B_4C5C_830F_0E7B6D1FD7E2__INCLUDED_)
////////////////////////////////////////////////
AND md5Capi.cpp CODE>>>>>>>
//////////////////////////////////////////////////////////////////////
// md5Capi.cpp: implementation of the Cmd5Capi class.
// Calcule MD5 Digest using the WIN Crypto API.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "md5Capi.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Cmd5Capi::Cmd5Capi()
{
csDigest.Empty();
}
Cmd5Capi::Cmd5Capi(CString & csBuffer)
{
Digest(csBuffer);
}
Cmd5Capi::~Cmd5Capi()
{
}
CString &Cmd5Capi::GetDigest(void)
{
return csDigest;
}
CString &Cmd5Capi::Digest(CString & csBuffer)
{
HCRYPTPROV hCryptProv;
HCRYPTHASH hHash;
BYTE bHash[0x7f];
DWORD dwHashLen= 16; // The MD5 algorithm always returns 16 bytes.
DWORD cbContent= csBuffer.GetLength();
BYTE* pbContent= (BYTE*)csBuffer.GetBuffer(cbContent);
if(CryptAcquireContext(&hCryptProv,
NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
if(CryptCreateHash(hCryptProv,
CALG_MD5, // algorithm identifier definitions see: wincrypt.h
0, 0, &hHash))
{
if(CryptHashData(hHash, pbContent, cbContent, 0))
{
if(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0))
{
// Make a string version of the numeric digest value
csDigest.Empty();
CString tmp;
for (int i = 0; i<16; i++)
{
tmp.Format("%02x", bHash[i]);
csDigest+=tmp;
}
}
else csDigest=_T("Error getting hash param");
}
else csDigest=_T("Error hashing data");
}
else csDigest=_T("Error creating hash");
}
else csDigest=_T("Error acquiring context");
CryptDestroyHash(hHash);
CryptReleaseContext(hCryptProv, 0);
csBuffer.ReleaseBuffer();
return csDigest;
}
///////////////////////////////////////////////
#include "md5Capi.h"
void EncriptFunction(CString content)
{
Cmd5Capi md5Capi;
CString enc(content);
CString md5coad = md5Capi.Digest(enc);
AfxMessageBox(md5coad);
}
这篇关于MD5和VC ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文