调用AdvApi32.dll中MD5算法的函数。

类别:编程语言 点击:0 评论:0 推荐:

在Win2000/XP的AdvApi32.dll中实现了基本的Hash和RSA算法,但微软没有给出函数和数据的定义。

下面演示MD5算法的调用。

#include <windows.h>

/* Data structure for MD5 (Message-Digest) computation */
typedef struct {
 ULONG i[2];                          /* number of _bits_ handled mod 2^64 */
 ULONG buf[4];                                           /* scratch buffer */
 unsigned char in[64];                                     /* input buffer */
 unsigned char digest[16];            /* actual digest after MD5Final call */
} MD5_CTX;


#define MD5DIGESTLEN 16

#define PROTO_LIST(list)    list


/*
* MTS: Each of these assumes MD5_CTX is locked against simultaneous use.
*/
typedef void (WINAPI* PMD5Init) PROTO_LIST ((MD5_CTX *));
typedef void (WINAPI* PMD5Update) PROTO_LIST ((MD5_CTX *, const unsigned char *, unsigned int));
typedef void (WINAPI* PMD5Final )PROTO_LIST ((MD5_CTX *));

PMD5Init MD5Init = NULL;
PMD5Update MD5Update = NULL;
PMD5Final MD5Final = NULL;

const char *Hex2ASC(const BYTE *Hex, int Len)
{
 static char  ASC[4096 * 2];
 int    i; 
 
 for (i = 0; i < Len; i++)
 {
  ASC[i * 2] = "0123456789ABCDEF"[Hex[i] >> 4];
  ASC[i * 2 + 1] = "0123456789ABCDEF"[Hex[i] & 0x0F];
 }
 ASC[i * 2] = '\0';
 
 return ASC;
}

 

int main()
{
 MD5_CTX ctx;
 unsigned char buf[4] = "1233";
 HINSTANCE hDLL;

 if ( (hDLL = LoadLibrary("advapi32.dll")) > 0 )
 {
  MD5Init = (PMD5Init)GetProcAddress(hDLL,"MD5Init");
  MD5Update = (PMD5Update)GetProcAddress(hDLL,"MD5Update");
  MD5Final = (PMD5Final)GetProcAddress(hDLL,"MD5Final");

  MD5Init(&ctx);
  MD5Update(&ctx,buf,4);
  MD5Final(&ctx);

  printf(Hex2ASC(ctx.digest,16));

 

 }
 
 return 0;
}

 

是不是很简单呢。另外AdvApi32.Dll中还有其他的一些算法如RSA,
DSA,SHA等,

以上代码在WinXP SP1/vc6.0 下调试通过

本文地址:http://com.8s8s.com/it/it28583.htm