MD5加密算法(c语言实现)


md5.h文件

/*
md5.h
author mwater.
www.epopsoft.com 
*/
#ifndef _MD5_H_
#define _MD5_H_
#include <memory.h>




#define R_memset(x, y, z) memset(x, y, z)
#define R_memcpy(x, y, z) memcpy(x, y, z)
#define R_memcmp(x, y, z) memcmp(x, y, z)

typedef unsigned long UINT4;
typedef unsigned char *POINTER;

/* MD5 context. */
typedef struct {
	/* state (ABCD) */   
	/*四个32bits数,用于存放最终计算得到的消息摘要。当消息长度〉512bits时,也用于存放每个512bits的中间结果*/ 
	UINT4 state[4];   

	/* number of bits, modulo 2^64 (lsb first) */    
	/*存储原始信息的bits数长度,不包括填充的bits,最长为 2^64 bits,因为2^64是一个64位数的最大值*/
	UINT4 count[2];

	/* input buffer */ 
	/*存放输入的信息的缓冲区,512bits*/
	unsigned char buffer[64];  
} MD5_CTX;


unsigned char * mi_md5(unsigned char * content ,int len ,unsigned char  * md5_res/*the length of md5_res must be more than 16 Bytes*/ );


#endif /* _MD5_H_ */


md5.cpp文件

/*    md5.cpp   */
#include "md5.h"

/* Constants for MD5Transform routine. */
/*md5转换用到的常量,算法本身规定的*/
#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(UINT4 [4], unsigned char [64]);
static void Encode(unsigned char *, UINT4 *, unsigned int);
static void Decode(UINT4 *, unsigned char *, unsigned int);

/*
用于bits填充的缓冲区,为什么要64个字节呢?因为当欲加密的信息的bits数被512除其余数为448时,
需要填充的bits的最大值为512=64*8 。
*/
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
};

/*
接下来的这几个宏定义是md5算法规定的,就是对信息进行md5加密都要做的运算。
据说有经验的高手跟踪程序时根据这几个特殊的操作就可以断定是不是用的md5
*/
/* 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))))

/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
Rotation is separate from addition to prevent recomputation.
*/
#define FF(a, b, c, d, x, s, ac) { \
	(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
	(a) = ROTATE_LEFT ((a), (s)); \
	(a) += (b); \
	}
#define GG(a, b, c, d, x, s, ac) { \
	(a) += G ((b),
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个用C语言实现MD5加密算法的示例: ```c #include <stdio.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) { uint32_t h0, h1, h2, h3; uint8_t *msg = NULL; uint32_t *w = NULL; uint32_t a, b, c, d, i, f, g, temp; size_t new_len, offset; // 初始化变量 h0 = 0x67452301; h1 = 0xEFCDAB89; h2 = 0x98BADCFE; h3 = 0x10325476; // 对消息进行预处理 new_len = ((((initial_len + 8) / 64) + 1) * 64); msg = (uint8_t *)malloc(new_len); memcpy(msg, initial_msg, initial_len); msg[initial_len] = 128; offset = initial_len + 1; while (offset < new_len - 8) { msg[offset] = 0; offset++; } uint64_t bits_len = (uint64_t)initial_len * 8; memcpy(msg + new_len - 8, &bits_len, 8); // 处理消息的每个块 for (offset = 0; offset < new_len; offset += 64) { w = (uint32_t *)(msg + offset); // 初始化散列值 a = h0; b = h1; c = h2; d = h3; // 主循环 for (i = 0; i < 64; i++) { 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; } temp = d; d = c; c = b; b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]); a = temp; } // 更新散列值 h0 += a; h1 += b; h2 += c; h3 += d; } // 释放内存 free(msg); // 输出散列值 printf("MD5 Hash: %08x%08x%08x%08x\n", h0, h1, h2, h3); } int main() { char *msg = "Hello, world!"; size_t len = strlen(msg); md5((uint8_t *)msg, len); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值