C++实现md5加密(兼容中文)

这篇博客详细介绍了如何在C++环境下实现MD5加密,特别强调了对中文字符的支持,利用Windows API进行处理,但目前该实现仅适用于Windows操作系统。
摘要由CSDN通过智能技术生成

说明:由于调用了windows api来对中文进行了处理,所以暂仅支持windows

#include <iostream>
#include <windows.h>
using namespace std;
typedef unsigned char uchar;
typedef unsigned long ulong;

//步函数
#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 ROL(x, n) (((x) << (n)) | ((x) >> (32-(n))))
//  四轮操作
#define FF(a, b, c, d, mj, s, ti) { (a) += F ((b), (c), (d)) + (mj) + ti; (a) = ROL ((a), (s)); (a) += (b);}
#define GG(a, b, c, d, mj, s, ti) { (a) += G ((b), (c), (d)) + (mj) + ti; (a) = ROL ((a), (s)); (a) += (b);}
#define HH(a, b, c, d, mj, s, ti) { (a) += H ((b), (c), (d)) + (mj) + ti; (a) = ROL ((a), (s)); (a) += (b);}
#define II(a, b, c, d, mj, s, ti) { (a) += I ((b), (c), (d)) + (mj) + ti; (a) = ROL ((a), (s)); (a) += (b);}

class MD5{
public:
	MD5(const string &str);
	const uchar* getDigest();	// 获取生成的摘要 
	string outstr(int);			// 返回字符串 
private:
	void generate(const uchar *input, int length);				 
	void change(const uchar block[64]);							// 四轮操作 
	void encode(const ulong *input, uchar *output, int length);	// uchar to ulong 
	void decode(const uchar *input, ulong *output, int length); // ulong to uchar
	
	ulong reg[4];		// ABCD	
	ulong count[2];	    // 长度扩充 
	uchar buffer[64];	// 输入buffer 
	uchar digest[16];	// 生成的摘要 
	bool end_flag;		// 结束标志 
	static const uchar padding[64];
	static const char hex[16];
};

const uchar MD5::padding[64] = { 0x80 };	// 初始化附加填充 1000 0000, 设置最高位为1 
const char MD5::hex[16] = {'0', '1', '2', '3
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值