md5.h文件:
typedef struct {
} MD5_CTX;
void MD5Init PROTO_LIST ((MD5_CTX *));
void MD5Update PROTO_LIST
void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
****************************************************************************
global.h文件:
#ifndef PROTOTYPES
#define PROTOTYPES 0
#endif
#define PROTOTYPES 0
#endif
typedef unsigned char *POINTER;
typedef unsigned short int UINT2;
typedef unsigned long int UINT4;
#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif
*************************************************************
md5.c文件
#include "global.h"
#include "md5.h"
#include <stdio.h>
#include <string.h>
#define S11 7
#define S12 12
#define S13 17
#define S14 22
#define S21 5
#define S22 9
#define S23 14
#define S24 20
#define S31 4
#define S32 11
#define S33 16
#define S34 23
#define S41 6
#define S42 10
#define S43 15
#define S44 21
static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64]));
static void Encode PROTO_LIST
static void Decode PROTO_LIST
static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int));
static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int));
static unsigned char PADDING[64] = {
};
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define FF(a, b, c, d, x, s, ac) {
#define GG(a, b, c, d, x, s, ac) {
#define HH(a, b, c, d, x, s, ac) {
#define II(a, b, c, d, x, s, ac) {
void MD5Init (MD5_CTX *context)
{
}
void MD5Update (MD5_CTX *context,unsigned char *input,unsigned int inputLen)
{
}
void MD5Final (unsigned char digest[16], MD5_CTX *context)
{
}
void MD5Transform (UINT4 state[4],unsigned char block[64])
{
}
static void Encode (unsigned char *output,UINT4 *input,unsigned int len)
{
}
void Decode (UINT4 *output,unsigned char *input,unsigned int len)
{
}
void MD5_memcpy (POINTER output,POINTER input,unsigned int len)
{
}
void MD5_memset (POINTER output,int value,unsigned int len)
{
}
void main()
{
int i;
unsigned char encrypt[200];
unsigned char decrypt[17];
MD5_CTX md5;
MD5Init(&md5);
scanf("%s",encrypt);
MD5Update(&md5,encrypt,strlen((char*)encrypt));
MD5Final(decrypt,&md5);
for(i=0;i<16;i++)
printf("x",decrypt[i]);
printf("n");
}
******************************************************************
实现将D:\test.txt文件用MD5算法加密,加密后的结果输出到md5.out.txt中去,修改main函数
void main()
{
int i,num;
FILE *fp,*code;
unsigned char *encrypt;
unsigned char decrypt[17];
MD5_CTX md5;
if((fp=fopen("D:\test.txt","r"))==NULL)
{
printf("Open file error!n");
exit(1);
}
if((code=fopen("D:\md5.out.txt","wb"))==NULL)
{
printf("open file error");
getchar();
exit(1);
}
num=0;
while(!feof(fp))
//求出文件的字符个数,动态分配内存
{
fgetc(fp);
num++;
}
printf("文件中的字符个数为:%dn",num-1);
encrypt=(char*)malloc(num);
fscanf(fp,"%s",encrypt);
encrypt[num-1]='';
MD5Init(&md5);
MD5Update(&md5,encrypt,num-1);
MD5Final(decrypt,&md5);
for(i=0;i<16;i++)
printf("x",decrypt[i]);
printf("n");
fwrite(decrypt,16,1,code);
fclose(code);
fclose(fp);
}