MD5加密C代码

#ifndef _MD5_H_
#define _MD5_H_
#include <string.h>
#include <time.h>
#include "stdio.h"

#ifdef WIN32
#pragma pack( push )
#pragma pack ( 1 )
#endif

#if defined(WIN32)
    #define PACKED
#else
    #define PACKED __attribute__((__packed__))	// 取消编译器的优化对齐
#endif

#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
/* F, G, H and I are basic MD5 functions. */
#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)))
/* ROTATE_LEFT rotates x left n bits. */
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
#define FF(a, b, c, d, x, s, ac) { \
 (a) += F ((b), (c), (d)) + (x) + (unsigned int)(ac); \
 (a) = ROTATE_LEFT ((a), (s)); \
 (a) += (b);\
 } 
#define GG(a, b, c, d, x, s, ac) { \
 (a) += G ((b), (c), (d)) + (x) + (unsigned int)(ac); \
 (a) = ROTATE_LEFT ((a), (s)); \
 (a) += (b); \
  }
#define HH(a, b, c, d, x, s, ac) { \
 (a) += H ((b), (c), (d)) + (x) + (unsigned int)(ac); \
 (a) = ROTATE_LEFT ((a), (s)); \
 (a) += (b); \
  }
#define II(a, b, c, d, x, s, ac) { \
 (a) += I ((b), (c), (d)) + (x) + (unsigned int)(ac); \
 (a) = ROTATE_LEFT ((a), (s)); \
 (a) += (b); \
  }
static unsigned char PADDING[64] = { 
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
};

// size of unsigned long int get 4 bytes for x86 and get 8 bytes for x64, which caused md5 enc differ ! keep 4 bytes enc for x64 platform !
class MD5_CTX
{ 
public:
	unsigned int state[4];                                   /* state (ABCD) */
	unsigned int count[2];        /* number of bits, modulo 2^64 (lsb first) */
	unsigned char buffer[64];                         /* input buffer */
public:
  MD5_CTX()
  {
  	Init();
  };
  ~MD5_CTX(){};
  void Init()
  {
  	int i;
    for (i = 0 ; i < 4 ; i++)
  		state[i] = 0;
  	for ( i = 0 ; i < 2 ; i++)
  	count[i] = 0;
  	memset(buffer,0,sizeof(buffer));
  	return ;
  };
}
PACKED
;
class CMD5Encrypt
{
public:
	CMD5Encrypt(){};
	~CMD5Encrypt(){};
	MD5_CTX context;
private:
	/* Note: Replace "for loop" with standard memcpy if possible. */
	void MD5memcpy (unsigned char * output, unsigned char * input, unsigned int len) 
	{
  		unsigned int i;
  		for (i = 0; i < len; i++)
      			output[i] = input[i];
	} 
	/* Note: Replace "for loop" with standard memset if possible. */
	void MD5memset (unsigned char * output, int value, unsigned int len) 
	{
  		unsigned int i;
		 for (i = 0; i < len; i++)
 			((char *)output)[i] = (char)value;
	}
	/* Decodes input (unsigned char) into output (unsigned int). Assumes len is a multiple of 4. */
	void Decode (unsigned int *output, unsigned char *input, unsigned int len)
	{
  		unsigned int i, j;
  		for (i = 0, j = 0; j < len; i++, j += 4)
 			output[i] = ((unsigned int)input[j]) | (((unsigned int)input[j+1]) << 8) |
   				(((unsigned int)input[j+2]) << 16) | (((unsigned int)input[j+3]) << 24);
	}
	/* Encodes input (unsigned int) into output (unsigned char). Assumes len is a multiple of 4.*/
	void Encode (unsigned char *output, unsigned int *input, unsigned int len)
	{
  		unsigned int i, j;
  		for (i = 0, j = 0; j < len; i++, j += 4) 
  		{
 			output[j] = (unsigned char)(input[i] & 0xff);
 			output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
 			output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
 			output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  		}
	}
	/* MD5 basic transformation. Transforms state based on block. */
	void MD5Transform (unsigned int state[4], unsigned char block[64])
	{
		unsigned int a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  		Decode (x, block, 64); 
  		/* Round 1 */
  		FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  		FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  		FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  		FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  		FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  		FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  		FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  		FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  		FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  		FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  		FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  		FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  		FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  		FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  		FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  		FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
 		/* Round 2 */
  		GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  		GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  		GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  		GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  		GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  		GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
  		GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  		GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  		GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  		GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  		GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  		GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ 
  		GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ 
  		GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ 
  		GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ 
  		GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ 
  		/* Round 3 */
  		HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  		HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  		HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  		HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  		HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  		HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  		HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  		HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  		HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  		HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  		HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  		HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
  		HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  		HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  		HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  		HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
		/* Round 4 */
  		II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  		II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  		II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  		II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  		II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  		II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  		II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  		II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  		II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  		II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  		II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  		II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  		II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  		II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  		II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  		II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  		state[0] += a; 
  		state[1] += b; 
  		state[2] += c; 
  		state[3] += d; 
  		/* Zeroize sensitive information.*/
  		MD5memset ((unsigned char *)x, 0, sizeof (x));
	}
	/* MD5 initialization. Begins an MD5 operation, writing a new context.*/
	void MD5Init (MD5_CTX *context) 
	{
 		context->count[0] = context->count[1] = 0;
  		context->state[0] = 0x67452301;
  		context->state[1] = 0xefcdab89;
  		context->state[2] = 0x98badcfe;
  		context->state[3] = 0x10325476;
		memset(context->buffer,0,64);
	} 
	/* MD5 block update operation. Continues an MD5 message-digest
  	operation, processing another message block, and updating the
  	context. */
	void MD5Update (MD5_CTX *context,unsigned char *input, unsigned int inputLen) 
	{
  		unsigned int i, index, partLen;
  		/* Compute number of bytes mod 64 */
  		index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  		/* Update number of bits */
  		if ((context->count[0] += ((unsigned int)inputLen << 3)) < ((unsigned int)inputLen << 3))
 				context->count[1]++;
  		context->count[1] += ((unsigned int)inputLen >> 29);
  		partLen = 64 - index;
		/* Transform as many times as possible.*/
  		if (inputLen >= partLen) 
  		{
 			MD5memcpy((unsigned char *)&context->buffer[index], (unsigned char *)input, partLen);
 			MD5Transform (context->state, context->buffer);
 			for (i = partLen; i + 63 < inputLen; i += 64)
   				MD5Transform (context->state, &input[i]);
 			index = 0;
  		}
  		else
 			i = 0;
 		 /* Buffer remaining input */
  		MD5memcpy((unsigned char *)&context->buffer[index], (unsigned char *)&input[i],inputLen-i);
	}
	/* MD5 finalization. Ends an MD5 message-digest operation, writing the
  	  the message digest and zeroizing the context. */
	void MD5Final (unsigned char digest[16], MD5_CTX *context) 
	{
  		unsigned char bits[8];
  		unsigned int index, padLen;
  		/* Save number of bits */
  		Encode (bits, context->count, 8);
  		/* Pad out to 56 mod 64.*/
  		index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  		padLen = (index < 56) ? (56 - index) : (120 - index);
  		MD5Update (context, PADDING, padLen);
  		/* Append length (before padding) */
  		MD5Update (context, bits, 8);
   		/* Store state in digest */
  		Encode (digest, context->state, 16);
  		/* Zeroize sensitive information.*/
  		MD5memset ((unsigned char *)context, 0, sizeof (*context));
	} 	     
public:
	void GetEncrypted(char* buf,char out[32])
	{
		MD5_CTX context;
		unsigned char temp[16];
		unsigned int len = strlen (buf);
		MD5Init(&context); 
		MD5Update(&context, (unsigned char *)buf, len); 
		MD5Final(temp, &context);
		for (int i = 0; i < 16; i++)
		{
			sprintf (out+2*i,"%x", (temp[i]&0xf0)>>4);
			sprintf (out+2*i+1,"%x", temp[i]&0x0f);	
		}
		return ;
	}
	
	void GetEncryptedFromFile(const char* filepath,char out[32])
	{
		MD5_CTX context;
		unsigned char temp[16];
		unsigned int length = 0;
		const int size = 1024;
		char buffer[size] = {0};
		FILE* stream = fopen(filepath,"rb");
		if (NULL == stream)
		{
			return ;
		}
		MD5Init(&context);
		while((length = fread(buffer,1,size,stream))>0)
		{
			MD5Update(&context,(unsigned char*)buffer,length);
		}
		MD5Final(temp, &context);
		fclose(stream);
		for (int i = 0; i < 16; i++)
		{
			sprintf (out+2*i,"%x", (temp[i]&0xf0)>>4);
			sprintf (out+2*i+1,"%x", temp[i]&0x0f);	
		}
		return ;
	}
}
PACKED
;

#ifdef WIN32
#pragma pack( pop )
#endif

#endif

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MD5(Message Digest Algorithm 5),即消息摘要算法的第五个版本,是一种广泛使用的哈希函数。C语言中实现MD5加密算法代码如下: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdint.h> #define ROTLEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define INIT_A 0x67452301 #define INIT_B 0xEFCDAB89 #define INIT_C 0x98BADCFE #define INIT_D 0x10325476 void md5(uint8_t *initial_msg, size_t initial_len) { uint32_t h0, h1, h2, h3; uint8_t *msg = NULL; uint32_t *w = NULL; size_t new_len, offset; uint32_t a, b, c, d, i; h0 = INIT_A; h1 = INIT_B; h2 = INIT_C; h3 = INIT_D; for (new_len = initial_len*8 + 1; new_len%512!=448; new_len++); new_len /= 8; msg = (uint8_t*)calloc(new_len + 64, 1); memcpy(msg, initial_msg, initial_len); msg[initial_len] = 128; offset = new_len - 8; w = (uint32_t*)(msg + new_len); w[0] = initial_len*8; for (i=0; i<new_len/64; i++) { uint32_t *chunk = (uint32_t*)(msg + i*64); a = h0; b = h1; c = h2; d = h3; uint32_t *x = w; uint32_t olda, oldb, oldc, oldd; for (uint8_t j=0; j<64; j++, x++) { if (j < 16) *x = chunk[j]; else *x = ROTLEFT(*(x-3) ^ *(x-8) ^ *(x-14) ^ *(x-16), 1); olda = a; oldb = b; oldc = c; oldd = d; #define MD5_F(x, y, z) (((x) & (y)) | ((~x) & (z))) #define MD5_G(x, y, z) (((x)&(z)) | ((y)&(~z))) #define MD5_H(x, y, z) ((x) ^ (y) ^ (z)) #define MD5_I(x, y, z) ((y) ^ ((x) | (~z))) #define MD5_FF(a, b, c, d, x, s, ac) { \ (a) += MD5_F((b), (c), (d)) + (x) + (uint32_t)(ac); \ (a) = ROTLEFT((a), (s)); \ (a) += (b); \ } #define MD5_GG(a, b, c, d, x, s, ac) { \ (a) += MD5_G((b), (c), (d)) + (x) + (uint32_t)(ac); \ (a) = ROTLEFT((a), (s)); \ (a) += (b); \ } #define MD5_HH(a, b, c, d, x, s, ac) { \ (a) += MD5_H((b), (c), (d)) + (x) + (uint32_t)(ac); \ (a) = ROTLEFT((a), (s)); \ (a) += (b); \ } #define MD5_II(a, b, c, d, x, s, ac) { \ (a) += MD5_I((b), (c), (d)) + (x) + (uint32_t)(ac); \ (a) = ROTLEFT((a), (s)); \ (a) += (b); \ } MD5_FF(a, b, c, d, x[j], 7, 0xd76aa478); MD5_FF(d, a, b, c, x[j+1], 12, 0xe8c7b756); MD5_FF(c, d, a, b, x[j+2], 17, 0x242070db); MD5_FF(b, c, d, a, x[j+3], 22, 0xc1bdceee); MD5_FF(a, b, c, d, x[j+4], 7, 0xf57c0faf); MD5_FF(d, a, b, c, x[j+5], 12, 0x4787c62a); MD5_FF(c, d, a, b, x[j+6], 17, 0xa8304613); MD5_FF(b, c, d, a, x[j+7], 22, 0xfd469501); MD5_FF(a, b, c, d, x[j+8], 7, 0x698098d8); MD5_FF(d, a, b, c, x[j+9], 12, 0x8b44f7af); MD5_FF(c, d, a, b, x[j+10], 17, 0xffff5bb1); MD5_FF(b, c, d, a, x[j+11], 22, 0x895cd7be); MD5_FF(a, b, c, d, x[j+12], 7, 0x6b901122); MD5_FF(d, a, b, c, x[j+13], 12, 0xfd987193); MD5_FF(c, d, a, b, x[j+14], 17, 0xa679438e); MD5_FF(b, c, d, a, x[j+15], 22, 0x49b40821); MD5_GG(a, b, c, d, x[j+1], 5, 0xf61e2562); MD5_GG(d, a, b, c, x[j+6], 9, 0xc040b340); MD5_GG(c, d, a, b, x[j+11], 14, 0x265e5a51); MD5_GG(b, c, d, a, x[j], 20, 0xe9b6c7aa); MD5_GG(a, b, c, d, x[j+5], 5, 0xd62f105d); MD5_GG(d, a, b, c, x[j+10], 9, 0x2441453); MD5_GG(c, d, a, b, x[j+15], 14, 0xd8a1e681); MD5_GG(b, c, d, a, x[j+4], 20, 0xe7d3fbc8); MD5_GG(a, b, c, d, x[j+9], 5, 0x21e1cde6); MD5_GG(d, a, b, c, x[j+14], 9, 0xc33707d6); MD5_GG(c, d, a, b, x[j+3], 14, 0xf4d50d87); MD5_GG(b, c, d, a, x[j+8], 20, 0x455a14ed); MD5_GG(a, b, c, d, x[j+13], 5, 0xa9e3e905); MD5_GG(d, a, b, c, x[j+2], 9, 0xfcefa3f8); MD5_GG(c, d, a, b, x[j+7], 14, 0x676f02d9); MD5_GG(b, c, d, a, x[j+12], 20, 0x8d2a4c8a); MD5_HH(a, b, c, d, x[j+5], 4, 0xfffa3942); MD5_HH(d, a, b, c, x[j+8], 11, 0x8771f681); MD5_HH(c, d, a, b, x[j+11], 16, 0x6d9d6122); MD5_HH(b, c, d, a, x[j+14], 23, 0xfde5380c); MD5_HH(a, b, c, d, x[j+1], 4, 0xa4beea44); MD5_HH(d, a, b, c, x[j+4], 11, 0x4bdecfa9); MD5_HH(c, d, a, b, x[j+7], 16, 0xf6bb4b60); MD5_HH(b, c, d, a, x[j+10], 23, 0xbebfbc70); MD5_HH(a, b, c, d, x[j+13], 4, 0x289b7ec6); MD5_HH(d, a, b, c, x[j], 11, 0xeaa127fa); MD5_HH(c, d, a, b, x[j+3], 16, 0xd4ef3085); MD5_HH(b, c, d, a, x[j+6], 23, 0x4881d05); MD5_HH(a, b, c, d, x[j+9], 4, 0xd9d4d039); MD5_HH(d, a, b, c, x[j+12], 11, 0xe6db99e5); MD5_HH(c, d, a, b, x[j+15], 16, 0x1fa27cf8); MD5_HH(b, c, d, a, x[j+2], 23, 0xc4ac5665); MD5_II(a, b, c, d, x[j], 6, 0xf4292244); MD5_II(d, a, b, c, x[j+7], 10, 0x432aff97); MD5_II(c, d, a, b, x[j+14], 15, 0xab9423a7); MD5_II(b, c, d, a, x[j+5], 21, 0xfc93a039); MD5_II(a, b, c, d, x[j+12], 6, 0x655b59c3); MD5_II(d, a, b, c, x[j+3], 10, 0x8f0ccc92); MD5_II(c, d, a, b, x[j+10], 15, 0xffeff47d); MD5_II(b, c, d, a, x[j+1], 21, 0x85845dd1); MD5_II(a, b, c, d, x[j+8], 6, 0x6fa87e4f); MD5_II(d, a, b, c, x[j+15], 10, 0xfe2ce6e0); MD5_II(c, d, a, b, x[j+6], 15, 0xa3014314); MD5_II(b, c, d, a, x[j+13], 21, 0x4e0811a1); MD5_II(a, b, c, d, x[j+4], 6, 0xf7537e82); MD5_II(d, a, b, c, x[j+11], 10, 0xbd3af235); MD5_II(c, d, a, b, x[j+2], 15, 0x2ad7d2bb); MD5_II(b, c, d, a, x[j+9], 21, 0xeb86d391); a += olda; b += oldb; c += oldc; d += oldd; } h0 += a; h1 += b; h2 += c; h3 += d; } free(msg); printf("%08x%08x%08x%08x", h0, h1, h2, h3); } int main() { uint8_t input[] = "hello world"; size_t len = strlen((char*)input); md5(input, len); return 0; } 上述代码中,我们定义了以下宏: #define ROTLEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) #define INIT_A 0x67452301 #define INIT_B 0xEFCDAB89 #define INIT_C 0x98BADCFE #define INIT_D 0x10325476 宏 ROTLEFT 实现循环左移,INIT_A、 INIT_B、 INIT_C 和 INIT_D 则为 MD5 压缩函数中的四个常数。在函数 md5 中,我们依次计算了 initial_msg 中的 512 位数据分组,针对每个数据分组进行流程处理,更新生成的MD5值 h0,h1,h2 和 h3。最终,通过 printf函数输出计算得到的 128 位(即32个十六进制数)的 MD5 值。 总体上,这段代码实现了 MD5 算法,可将任意长度消息的数字摘要压缩成 16 字节(即128位) 的二进制数。 ### 回答2: MD5(Message-Digest Algorithm 5)是一种常用的哈希函数,可将任意长度的消息压缩成一个128位的消息摘要,通常用于验证文件和密码的完整性。 以下是用C语言编写的MD5加密代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c)))) void md5(uint8_t* initial_msg, size_t initial_len, uint8_t* digest) { //初始化MD缓冲区 uint32_t h0, h1, h2, h3; h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE; h3 = 0x10325476; //预处理 size_t new_len; for (new_len = initial_len * 8 + 1; new_len % 512 != 448; new_len++); new_len /= 8; uint8_t* msg = (uint8_t*)calloc(new_len + 64, 1); memcpy(msg, initial_msg, initial_len); msg[initial_len] = 128; uint32_t bit_len = 8 * initial_len; memcpy(msg + new_len, &bit_len, 4); //循环计算每一个分块的MD值 for (size_t offset = 0; offset < new_len; offset += (512 / 8)) { uint32_t* w = (uint32_t*)(msg + offset); uint32_t a = h0; uint32_t b = h1; uint32_t c = h2; uint32_t d = h3; for (size_t i = 0; i < 64; i++) { uint32_t f, g; if (i < 16) { f = (b & c) | ((~b) & d); g = i; } else if (i < 32) { f = (d & b) | ((~d) & c); g = (5 * i + 1) % 16; } else if (i < 48) { f = b ^ c ^ d; g = (3 * i + 5) % 16; } else { f = c ^ (b | (~d)); g = (7 * i) % 16; } uint32_t temp = d; d = c; c = b; b = b + LEFTROTATE((a + f + ((uint32_t*)w)[g] + 0x5A827999), 7); a = temp; } h0 += a; h1 += b; h2 += c; h3 += d; } free(msg); //将最终的MD值存储到摘要 uint32_t* output = (uint32_t*)digest; output[0] = h0; output[1] = h1; output[2] = h2; output[3] = h3; } int main(int argc, char* argv[]) { char* msg = "hello world"; uint8_t digest[16]; md5((uint8_t*)msg, strlen(msg), digest); for (size_t i = 0; i < 16; i++) { printf("%02x", digest[i]); } return 0; } ``` 以上代码实现了基本的MD5加密功能,能够接受任意长度的消息,并返回一个16字节的消息摘要。为了让代码更加健壮和高效,还可以进行优化和改进。 ### 回答3: MD5是一种常用的哈希函数,用于确保数据完整性和验证文件的一致性。将C语言编写的MD5加密算法实现如下: 1. 定义4个32位常量K[0…63],用于辅助计算。 2. 定义一个512位缓冲区block[0…15],用于存储需要加密的信息,将其初始化填充为0。 3. 定义4个32位变量A、B、C、D,表示MD5算法的4个字节的寄存器。初始化为如下值: A=0x67452301 B=0xefcdab89 C=0x98badcfe D=0x10325476 4. 定义一个循环变量i,进入循环,依次取出四个字符进行处理。 5. 定义16个32位变量F[0…15],用于表示MD5算法中的非线性函数,每次循环都要重新计算。 6. 定义16个32位变量X[0…15],用于表示MD5算法中的消息块。 7. 将字符转化为32位整数X[i],存储在X[0…15]中。 8. 根据i的值计算F[0…15],将结果存储在16个32位变量F[0…15]中。 9. 定义4个32位变量tmp、g、k、s,用于计算。 10. 根据i的值计算tmp,将结果存储在tmp中。 11. 根据i的值计算g、k、s,将结果存储在变量g、k、s中。 12. 根据MD5算法,更新寄存器的值A、B、C、D。具体更新方式为: temp = D; D = C; C = B; B = B + rotate_left((A + F[i] + X[g] + k), s); A = temp; 13. 最后,将四个32位寄存器A、B、C、D连接起来,生成MD5加密值。 下面是MD5加密代码C语言实现的示例: #include <stdio.h> #include <string.h> #include <stdint.h> uint32_t buffer[16] = {0x00}; uint32_t k[64] = {0x00}; uint32_t a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476; void calc_k(void) { int i; for(i = 0; i < 64; ++i) { k[i] = 0x100000000 * fabs(sin(i + 1)); } } void calc_md5(char* str) { int i, j, p; uint32_t s, g, f, temp; uint32_t x[16]; calc_k(); int len = strlen(str); for(i = 0; i < len; i += 64) { for(j = 0; j < 16 && i+j < len; j++) { p = i + j*4; buffer[j] = (str[p] & 0xff) | ((str[p+1] & 0xff) << 8) | ((str[p+2] & 0xff) << 16) | ((str[p+3] & 0xff) << 24); } for(j = 16; j < 64; j++) { s = ((j - 3) % 32) ^ ((j - 8) % 32) ^ ((j - 14) % 32) ^ ((j - 16) % 32); buffer[j%16] = buffer[(j - 3) % 16] ^ buffer[(j - 8) % 16] ^ buffer[(j - 14) % 16] ^ buffer[(j - 16) % 16]; buffer[j%16] = buffer[j%16] << s | buffer[j%16] >> (32 - s); } uint32_t aa = a, bb = b, cc = c, dd = d; for(j = 0; j < 64; j++) { if (j < 16) { s = (7 * j) % 32; g = j; f = (b & c) | ((~b) & d); } else if (j < 32) { s = (7 * j) % 32; g = (5 * j + 1) % 16; f = (b & d) | (c & (~d)); } else if (j < 48) { s = (7 * j) % 32; g = (3 * j + 5) % 16; f = b ^ c ^ d; } else if (j < 64) { s = (7 * j) % 32; g = (7 * j) % 16; f = c ^ (b | (~d)); } temp = d; d = c; c = b; b = b + ((a + f + k[j] + buffer[g]) << s | (a + f + k[j] + buffer[g]) >> (32 - s)); a = temp; } a += aa; b += bb; c += cc; d += dd; } printf("%08X%08X%08X%08X\n", a, b, c, d); } int main() { char str[1024] = "hello world"; calc_md5(str); return 0; } 在这个示例中,我们定义了一个512位的缓冲区block[0...15],用于存储需要加密的信息,128位的k[0...63]用于辅助计算,四个32位的变量A、B、C、D用于表示MD5算法的四个字节的寄存器。随后定义了一个计算K数组的函数calc_k,和主函数calc_md5,其中主要实现了如上面所述的MD5加密流程。最终输出32位的MD5加密值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值