MD5算法简介及两种实现方法(使用WindowsAPI函数).doc_第1页
MD5算法简介及两种实现方法(使用WindowsAPI函数).doc_第2页
MD5算法简介及两种实现方法(使用WindowsAPI函数).doc_第3页
MD5算法简介及两种实现方法(使用WindowsAPI函数).doc_第4页
MD5算法简介及两种实现方法(使用WindowsAPI函数).doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MD5简介MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由MIT Laboratory for Computer Science和RSA Data Security Inc的Ronald L. Rivest开发出来,经MD2、MD3和MD4发展而来。它的作用是让大容量信息在用数字签名软件签署私人密匙前被压缩成一种保密的格式。MD5算法的描述和C语言源代码在Internet RFCs 1321中有详细的描述(/rfc/rfc1321.txt),这是一份最权威的文档,由Ronald L. Rivest在1992年8月向IEFT提交。我们先来看看RFC文档中对MD5的描述:This document describes the MD5 message-digest algorithm(摘要). The algorithm takes as input a message of arbitrary length(任意长度) and produces as output a 128-bit fingerprint or message digest of the input. It is conjectured that it is computationally infeasible(不可实行的) to produce two messages having the same message digest, or to produce any message having a given prespecified target message digest. The MD5 algorithm is intended for digital signature applications, where a large file must be compressed in a secure manner before being encrypted with a private (secret) key under a public-key cryptosystem such as RSA.从上面的RFC描述中,可以总结出MD5摘要算法两个最重要的特性:1、 不可逆,无法由结果退出输入2、 two messages不可能拥有相同的摘要信息,保证一对一正是这两个特性保证了MD5在数字签名中的应用,将一个需要签名的大文件转换为等价的128bit的摘要信息,再用私钥对128bit的摘要信息进行加密。摘要的结果是128bit的,所以网上经常看到的32位/16位MD5中的位对应的是16进制的32和16,128bit刚好是32字节。MD5只是一个摘要算法,由于它具有上面说的两个特性,在实际中(很多网站)也被用来对密码进行摘要,起到加密的作用。算法实现关于MD5的算法实现,当然可以参考RFC文档中的源代码,不过如果只在Windows平台应用的话,完全可以偷懒,调用Windows现有的API函数,只需三个API就可以搞定,很简单,呵呵!,分别是MD5Init、MD5Update、MD5Final,只是这三个函数没有在windows的.h头文件中有定义,也没有相应的import library,需要自己声明它们的函数原型,这三个函数的具体实现在Cryptdll.dll中,需要动态加载!实现一:下面是具体的源代码,用Cryptdll.dll中的三个api实现:用法:MD5.exe空格input_message#include stdafx.h#include #include #include using namespace std;typedef struct ULONG i2; ULONG buf4; unsigned char in64; unsigned char digest16; MD5_CTX;typedef void (CALLBACK* MD5Init_Tpye)(MD5_CTX* context); typedef void (CALLBACK* MD5Update_Tpye)(MD5_CTX* context, unsigned char* input, unsigned int inlen); typedef void (CALLBACK* MD5Final_Tpye)(MD5_CTX* context);int main(int argc, char *argv) if (argc != 2)cout args count err! endl;return 0;HINSTANCE hDLL = LoadLibrary(LCryptdll.dll);if (hDLL = NULL)return -1;MD5Init_Tpye MD5Init;MD5Update_Tpye MD5Update; MD5Final_Tpye MD5Final;MD5Init = (MD5Init_Tpye)GetProcAddress(hDLL, MD5Init);MD5Update = (MD5Update_Tpye)GetProcAddress(hDLL, MD5Update);MD5Final = (MD5Final_Tpye)GetProcAddress(hDLL, MD5Final);if (MD5Init = NULL | MD5Update = NULL | MD5Final = NULL)FreeLibrary(hDLL);return -1;MD5_CTX md5_context; MD5Init(&md5_context); unsigned char src100;unsigned length = strlen(argv1);memcpy(src, argv1, length);MD5Update(&md5_context, src, length); MD5Final(&md5_context); char dest100 = 0 ;char *p = dest;for(int i = 0; i 16; +i)sprintf_s(p, 3, %02x, md5_context.digesti);p += 2;cout dest 1000#pragma once#endif / _MSC_VER 1000#include / Cryptographic API Prototypes and Definitionsclass Cmd5Capi public:CString &Digest(CString & csBuffer);CString &GetDigest(void);Cmd5Capi(CString & csBuffer);Cmd5Capi();virtual Cmd5Capi();CStringcsDigest;#endif/ 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_FILEstatic 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 bHash0x7f; 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.h0, 0, &hHash) if(CryptHashData(hHash, pbContent, cbContent, 0)if(CryptGetHashParam(hHash, HP_HASHVAL, bHash, &dwHashLen, 0) / Make a string version of the numeric digest valuecsDigest.Empty();CString tmp; for (int i = 0; i16; i+) tmp.Format(%02x, bHashi);csDigest+=tmp; else csDigest=_T(Error getting hash param); else csDi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论