MD5源代码VC++

 

/

/

//将MD5封装成一个类文件,其头文件如下(直接拷贝即可)//

 

#pragma warning(disable : 4786)

#ifndef _LGY_MD5_H
#define _LGY_MD5_H

/* MD5 Class. */

class MD5
{
private:

// this value must initialize first
unsigned char PADDING[64];

/** Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4.
*/
void Encode (unsigned char *output, unsigned long *input, unsigned int len);

/** Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4.
*/
void Decode (unsigned long* output, unsigned char *input, unsigned int len);

/** Decodes input (unsigned char) into output (UINT4). Assumes len is
a multiple of 4.
*/
void MD5_memset (unsigned char* output, int value, unsigned int len);

// Note: Replace “for loop” with standard memcpy if possible.
void MD5_memcpy (unsigned char* output, unsigned char* input, unsigned int len);

public:

// MD5 context.
struct MD5_CTX
{
unsigned long state[4]; // state (ABCD)
unsigned long count[2]; // number of bits, modulo 2^64 (lsb first)
unsigned char buffer[64]; // input buffer
};

MD5();

virtual ~MD5();

/** step 1
MD5 initialization. Begins an MD5 operation, writing a new context.
*/
void MD5Init (struct MD5_CTX *context);

/** step 2
MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
void MD5Update (struct MD5_CTX *context /* context */,
unsigned char *input /* input block */,
unsigned int inputLen /* length of input block */);

/** step 3
MD5 basic transformation. Transforms state based on block.
*/
private : void MD5Transform (unsigned long state[4], unsigned char block[64]);

/** step 4
MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context.
*/
public : void MD5Final (unsigned char digest[16]/* message digest */, MD5_CTX *context /* context */);

CString MD5String(unsigned char *pData, unsigned long DataLen, bool bUppercase = true);

private:

CString m_strErr; // Error Description

// 将寄存器A, B, C, D的值转换为十六进制, 并以字符串方式返回
CString ResultToHex(unsigned char digest[16], bool bUppercase = true);

/** 获取字符串的MD5值
pData - 待计算MD5值的数据
DataLen - 数据长度(单位, 字节)
Uppercase - 结果是否转换为大写
*/

// 此函数可提前结束MD5File函数的执行
void StopCalculate();

// Get Error Description
CString GetError();

};

#endif

 

/

/

//将MD5封装成一个类文件,其头文件如上(直接拷贝即可)//

 

 

/

/

//将MD5封装成一个类文件,其cpp文件如下(直接拷贝即可)//

 

 

#include "stdafx.h"

#include "MD5.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#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))))

/** 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) + (unsigned long)(ac); /
(a) = ROTATE_LEFT ((a), (s)); /
(a) += (b); /
}
#define GG(a, b, c, d, x, s, ac) /
{ /
(a) += G ((b), (c), (d)) + (x) + (unsigned long)(ac); /
(a) = ROTATE_LEFT ((a), (s)); /
(a) += (b); /
}
#define HH(a, b, c, d, x, s, ac) /
{ /
(a) += H ((b), (c), (d)) + (x) + (unsigned long)(ac); /
(a) = ROTATE_LEFT ((a), (s)); /
(a) += (b); /
}
#define II(a, b, c, d, x, s, ac) /
{ /
(a) += I ((b), (c), (d)) + (x) + (unsigned long)(ac); /
(a) = ROTATE_LEFT ((a), (s)); /
(a) += (b); /
}

MD5::MD5()
{
PADDING[0] = 0x80;
for(int i = 1; i <= 63; i++)
PADDING[i] = 0;
}

MD5::~MD5()
{

}

// MD5 initialization. Begins an MD5 operation, writing a new context.
void MD5::MD5Init (struct 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;
}

/** MD5 block update operation. Continues an MD5 message-digest
operation, processing another message block, and updating the
context.
*/
void MD5::MD5Update (struct MD5_CTX *context/* context */,
unsigned char *input /* input block */,
unsigned int inputLen /* length of input block */)
{
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 long)inputLen << 3)) < ((unsigned long)inputLen << 3))
context->count[1]++;
context->count[1] += ((unsigned long)inputLen >> 29);

partLen = 64 - index;

// Transform as many times as possible.
if (inputLen >= partLen)
{
MD5_memcpy
((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
MD5_memcpy ((unsigned char*)&context->buffer[index], (unsigned char*)&input[i], inputLen-i);
}

// Note: Replace "for loop" with standard memcpy if possible.
void MD5::MD5_memcpy (unsigned char* output, unsigned char* input, unsigned int len)
{
for (unsigned int i = 0; i < len; i++)
output[i] = input[i];
}

// MD5 basic transformation. Transforms state based on block.
void MD5::MD5Transform (unsigned long state[4], unsigned char block[64])
{
unsigned long 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, 0x02441453); /* 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, 0x04881D05); /* 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;

MD5_memset ((unsigned char*)x, 0, sizeof (x));
}

/** Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4.
*/
void MD5::Encode (unsigned char *output, unsigned long *input, unsigned int len)
{
for (unsigned int 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);
}
}

/** Encodes input (UINT4) into output (unsigned char). Assumes len is
a multiple of 4.
*/
void MD5::Decode (unsigned long* output, unsigned char *input, unsigned int len)
{
for (unsigned int i = 0, j = 0; j < len; i++, j += 4)
output[i] = ((unsigned long)input[j]) |
(((unsigned long)input[j+1]) << 8) |
(((unsigned long)input[j+2]) << 16) |
(((unsigned long)input[j+3]) << 24);
}

/** Decodes input (unsigned char) into output (UINT4). Assumes len is
a multiple of 4.
*/
void MD5::MD5_memset (unsigned char* output, int value, unsigned int len)
{
for (unsigned int i = 0; i < len; i++)
((char *)output)[i] = (char)value;
}

/** MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context.
*/
void MD5::MD5Final (unsigned char digest[16]/* message digest */, MD5_CTX *context /* 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.
MD5_memset ((unsigned char*)context, 0, sizeof (*context));
}
CString MD5::ResultToHex(unsigned char digest[16], bool bUppercase)
{
CString strRetVal = "";
for(int i = 0 ; i < 16; i++)
{
char buf[3] = {0};
if(bUppercase)
sprintf(buf, "%02X", digest[i]);
else
sprintf(buf, "%02x", digest[i]);
strRetVal += buf;
}
return strRetVal;
}

CString MD5::MD5String(unsigned char *pData, unsigned long DataLen, bool bUppercase)
{
MD5_CTX context;
unsigned char digest[16];

MD5Init (&context);
MD5Update (&context, pData, DataLen);
MD5Final (digest, &context);

return ResultToHex(digest, bUppercase);
}

 

 

 

 

 

 

 

 

 

 

 

 

 

/

/

//生成MD5类,从而在需要使用的类中调用MD5中的方法
//在需要使用MD5的类的cpp文件中的开始位置加入#include"MD5.h"

 

MD5 m_MD5;
        CString strInput="admin888";
        bool m_bUpper = 1;

        CString strMD5 = m_MD5.MD5String((unsigned char*)(LPCTSTR) /

              strInput, strInput.GetLength(), m_bUpper);
        //std::string->CString for example:

 CString strStl=strMD5;
        AfxMessageBox(strStl);
 //CString names=strStl.c_str();//output string to names

        //admin888字符串MD5_16加密的请换成 469e80d32c0559f8
        //admin888字符串MD5_32加密的换成 7fef6171469e80d32c0559f88b377245

 

/

/

//将MD5封装成一个类文件,其cpp文件如上(直接拷贝即可)//

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值