///'#include <afx.h>//n#include <windows.h>//n#include //n#include //n#include //n#include //n//n// 定义 MD5 相关数据结构//ntypedef unsigned int UINT4;//ntypedef unsigned char BYTE;//n//nstruct MD5_CTX {//n UINT4 state[4];//n UINT4 count[2];//n BYTE buffer[64];//n};//n//n// MD5 加密算法的函数声明//nvoid MD5Init(MD5_CTX* context);//nvoid MD5Update(MD5_CTX* context, BYTE* input, UINT4 inputLen);//nvoid MD5Final(BYTE digest[16], MD5_CTX* context);//nvoid MD5Transform(UINT4 state[4], BYTE block[64]);//nvoid MD5Encode(BYTE* output, UINT4* input, UINT4 len);//nvoid MD5Decode(UINT4* output, BYTE* input, UINT4 len);//n//n// MD5 加密算法的实现//nvoid MD5Init(MD5_CTX* context) {//n context->count[0] = context->count[1] = 0;//n context->state[0] = 0x67452301;//n context->state[1] = 0xEFCDAB89;//n context->state[2] = 0x98BADCFE;//n context->state[3] = 0x10325476;//n}//n//nvoid MD5Update(MD5_CTX* context, BYTE* input, UINT4 inputLen) {//n UINT4 i, index, partLen;//n//n index = (UINT4)((context->count[0] >> 3) & 0x3F);//n if ((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3))//n context->count[1]++;//n context->count[1] += ((UINT4)inputLen >> 29);//n//n partLen = 64 - index;//n//n if (inputLen >= partLen) {//n memcpy(&context->buffer[index], input, partLen);//n MD5Transform(context->state, context->buffer);//n//n for (i = partLen; i + 63 < inputLen; i += 64) {//n MD5Transform(context->state, &input[i]);//n }//n//n index = 0;//n } else {//n i = 0;//n }//n//n memcpy(&context->buffer[index], &input[i], inputLen - i);//n}//n//nvoid MD5Final(BYTE digest[16], MD5_CTX* context) {//n BYTE bits[8];//n UINT4 index, padLen;//n//n MD5Encode(bits, context->count, 8);//n//n index = (UINT4)((context->count[0] >> 3) & 0x3F);//n padLen = (index < 56) ? (56 - index) : (120 - index);//n MD5Update(context, (BYTE*)PADDING, padLen);//n//n MD5Update(context, bits, 8);//n//n MD5Encode(digest, context->state, 16);//n//n memset(context, 0, sizeof(context));//n}//n//nvoid MD5Encode(BYTE output, UINT4* input, UINT4 len) {//n UINT4 i, j;//n//n for (i = 0, j = 0; j < len; i++, j += 4) {//n output[j] = (BYTE)(input[i] & 0xFF);//n output[j + 1] = (BYTE)((input[i] >> 8) & 0xFF);//n output[j + 2] = (BYTE)((input[i] >> 16) & 0xFF);//n output[j + 3] = (BYTE)((input[i] >> 24) & 0xFF);//n }//n}//n//nvoid MD5Decode(UINT4* output, BYTE* input, UINT4 len) {//n UINT4 i, j;//n//n for (i = 0, j = 0; j < len; i++, j += 4) {//n output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) |//n (((UINT4)input[j + 2]) << 16) | (((UINT4)input[j + 3]) << 24);//n }//n}//n//nvoid MD5Transform(UINT4 state[4], BYTE block[64]) {//n UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];//n//n MD5Decode(x, block, 64);//n//n // Round 1//n FF(a, b, c, d, x[0], S11, 0xd76aa478); // 1//n FF(d, a, b, c, x[1], S12, 0xe8c7b756); // 2//n FF(c, d, a, b, x[2], S13, 0x242070db); // 3//n FF(b, c, d, a, x[3], S14, 0xc1bdceee); // 4//n FF(a, b, c, d, x[4], S11, 0xf57c0faf); // 5//n FF(d, a, b, c, x[5], S12, 0x4787c62a); // 6//n FF(c, d, a, b, x[6], S13, 0xa8304613); // 7//n FF(b, c, d, a, x[7], S14, 0xfd469501); // 8//n FF(a, b, c, d, x[8], S11, 0x698098d8); // 9//n FF(d, a, b, c, x[9], S12, 0x8b44f7af); // 10//n FF(c, d, a, b, x[10], S13, 0xffff5bb1); // 11//n FF(b, c, d, a, x[11], S14, 0x895cd7be); // 12//n FF(a, b, c, d, x[12], S11, 0x6b901122); // 13//n FF(d, a, b, c, x[13], S12, 0xfd987193); // 14//n FF(c, d, a, b, x[14], S13, 0xa679438e); // 15//n FF(b, c, d, a, x[15], S14, 0x49b40821); // 16//n//n // Round 2//n GG(a, b, c, d, x[1], S21, 0xf61e2562); // 17//n GG(d, a, b, c, x[6], S22, 0xc040b340); // 18//n GG(c, d, a, b, x[11], S23, 0x265e5a51); // 19//n GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); // 20//n GG(a, b, c, d, x[5], S21, 0xd62f105d); // 21//n GG(d, a, b, c, x[10], S22, 0x2441453); // 22//n GG(c, d, a, b, x[15], S23, 0xd8a1e681); // 23//n GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); // 24//n GG(a, b, c, d, x[9], S21, 0x21e1cde6); // 25//n GG(d, a, b, c, x[14], S22, 0xc33707d6); // 26//n GG(c, d, a, b, x[3], S23, 0xf4d50d87); // 27//n GG(b, c, d, a, x[8], S24, 0x455a14ed); // 28//n GG(a, b, c, d, x[13], S21, 0xa9e3e905); // 29//n GG(d, a, b, c, x[2], S22, 0xfcefa3f8); // 30//n GG(c, d, a, b, x[7], S23, 0x676f02d9); // 31//n GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); // 32//n//n // Round 3//n HH(a, b, c, d, x[5], S31, 0xfffa3942); // 33//n HH(d, a, b, c, x[8], S32, 0x8771f681); // 34//n HH(c, d, a, b, x[11], S33, 0x6d9d6122); // 35//n HH(b, c, d, a, x[14], S34, 0xfde5380c); // 36//n HH(a, b, c, d, x[1], S31, 0xa4beea44); // 37//n HH(d, a, b, c, x[4], S32, 0x4bdecfa9); // 38//n HH(c, d, a, b, x[7], S33, 0xf6bb4b60); // 39//n HH(b, c, d, a, x[10], S34, 0xbebfbc70); // 40//n HH(a, b, c, d, x[13], S31, 0x289b7ec6); // 41//n HH(d, a, b, c, x[0], S32, 0xeaa127fa); // 42//n HH(c, d, a, b, x[3], S33, 0xd4ef3085); // 43//n HH(b, c, d, a, x[6], S34, 0x4881d05); // 44//n HH(a, b, c, d, x[9], S31, 0xd9d4d039); // 45//n HH(d, a, b, c, x[12], S32, 0xe6db99e5); // 46//n HH(c, d, a, b, x[15], S33, 0x1fa27cf8); // 47//n HH(b, c, d, a, x[2], S34, 0xc4ac5665); // 48//n//n // Round 4//n II(a, b, c, d, x[0], S41, 0xf4292244); // 49//n II(d, a, b, c, x[7], S42, 0x432aff97); // 50//n II(c, d, a, b, x[14], S43, 0xab9423a7); // 51//n II(b, c, d, a, x[5], S44, 0xfc93a039); // 52//n II(a, b, c, d, x[12], S41, 0x655b59c3); // 53//n II(d, a, b, c, x[3], S42, 0x8f0ccc92); // 54//n II(c, d, a, b, x[10], S43, 0xffeff47d); // 55//n II(b, c, d, a, x[1], S44, 0x85845dd1); // 56//n II(a, b, c, d, x[8], S41, 0x6fa87e4f); // 57//n II(d, a, b, c, x[15], S42, 0xfe2ce6e0); // 58//n II(c, d, a, b, x[6], S43, 0xa3014314); // 59//n II(b, c, d, a, x[13], S44, 0x4e0811a1); // 60//n II(a, b, c, d, x[4], S41, 0xf7537e82); // 61//n II(d, a, b, c, x[11], S42, 0xbd3af235); // 62//n II(c, d, a, b, x[2], S43, 0x2ad7d2bb); // 63//n II(b, c, d, a, x[9], S44, 0xeb86d391); // 64//n//n state[0] += a;//n state[1] += b;//n state[2] += c;//n state[3] += d;//n//n memset((BYTE*)x, 0, sizeof(x));//n}//n//n// 将十六进制字符串转换为字节数组//nvoid HexStringToByteArray(const std::string& hexString, BYTE* byteArray, UINT4 byteArraySize) {//n std::istringstream hexStream(hexString);//n//n for (UINT4 i = 0; i < byteArraySize; ++i) {//n int byteValue;//n hexStream >> std::hex >> byteValue;//n//n byteArray[i] = static_cast(byteValue);//n }//n}//n//n// 将字节数组转换为十六进制字符串//nstd::string ByteArrayToHexString(const BYTE* byteArray, UINT4 byteArraySize) {//n std::ostringstream hexStream;//n//n hexStream << std::hex << std::uppercase << std::setfill('0');//n for (UINT4 i = 0; i < byteArraySize; ++i) {//n hexStream << std::setw(2) << static_cast(byteArray[i]);//n }//n//n return hexStream.str();//n}//n//n// MD5 加密函数//nstd::string MD5Encrypt(const std::string& input) {//n MD5_CTX md5Context;//n BYTE digest[16];//n//n MD5Init(&md5Context);//n MD5Update(&md5Context, (BYTE*)input.c_str(), input.length());//n MD5Final(digest, &md5Context);//n//n return ByteArrayToHexString(digest, sizeof(digest));//n}//n//nint main() {//n std::string input = ///'hello world///';//n std::string encrypted = MD5Encrypt(input);//n//n std::cout << ///'MD5 Encrypted: ///' << encrypted << std::endl;//n//n return 0;//n}//n/