zoukankan      html  css  js  c++  java
  • Web QQ 协议 登录加密算法 —— VC++实现


     
    1. BOOL ToHexStr(const CHAR * lpStr, int nSrcLen, CHAR * lpHex, int nDestLen)  
    2. {  
    3.     const CHAR cHexTable[] = "0123456789ABCDEF";  
    4.       
    5.     if (lpStr == NULL || nSrcLen <= 0  
    6.         || lpHex == NULL || nDestLen <= 0)  
    7.         return FALSE;  
    8.       
    9.     if (nDestLen <= nSrcLen * 2)  
    10.         return FALSE;  
    11.       
    12.     int i = 0;  
    13.     for (int j = 0; j < (int)strlen(lpStr); j++)  
    14.     {  
    15.         unsigned int a = (unsigned int)lpStr[j];  
    16.         lpHex[i++] = cHexTable[(a & 0xf0) >> 4];  
    17.         lpHex[i++] = cHexTable[(a & 0x0f)];  
    18.     }  
    19.     lpHex[i] = '';  
    20.       
    21.     return TRUE;  
    22. }  
    23.   
    24. BOOL StrToHex(const CHAR * lpStr, CHAR * lpHex, int nLen)  
    25. {  
    26.     const CHAR cHexTable[] = "0123456789ABCDEF";  
    27.       
    28.     if (lpStr == NULL || lpHex == NULL || nLen <= 0)  
    29.         return FALSE;  
    30.       
    31.     int nSrcLen = strlen(lpStr);  
    32.     if (nLen <= nSrcLen * 2)  
    33.         return FALSE;  
    34.       
    35.     int i = 0;  
    36.     for (int j = 0; j < (int)strlen(lpStr); j++)  
    37.     {  
    38.         unsigned int a = (unsigned int)lpStr[j];  
    39.         lpHex[i++] = cHexTable[(a & 0xf0) >> 4];  
    40.         lpHex[i++] = cHexTable[(a & 0x0f)];  
    41.     }  
    42.     lpHex[i] = '';  
    43.       
    44.     return TRUE;  
    45. }  
    46.   
    47. BOOL CalcHash(char *lpQQPwd,char *lpPtUin,char *lpVerifyCode,char *lpPwdHash)  
    48. {  
    49.     CHAR cQQPwd[128] = {0};  
    50.     CHAR cVerifyCode[128] = {0};  
    51.     CHAR cHex[36] = {0};  
    52.     CHAR cTemp[256] = {0};  
    53.     const byte * lpDigest;  
    54.     MD5 md5;  
    55.       
    56.     int nPtUinLen = 8;  
    57.     int nLen = 8;  
    58.       
    59.     if (NULL == lpQQPwd || NULL == lpVerifyCode  
    60.         || NULL == lpPtUin || nPtUinLen <= 0  
    61.         || NULL == lpPwdHash || nLen <= 0)  
    62.         return FALSE;  
    63.       
    64.     memcpy(cQQPwd,lpQQPwd,strlen(lpQQPwd));  
    65.     memcpy(cVerifyCode,lpVerifyCode,strlen(lpVerifyCode));  
    66.       
    67.     //UnicodeToUtf8(lpQQPwd, cQQPwd, sizeof(cQQPwd));  
    68.     //UnicodeToUtf8(lpVerifyCode, cVerifyCode, sizeof(cVerifyCode));  
    69.       
    70.     md5.reset();  
    71.     md5.update((const void *)cQQPwd, strlen(cQQPwd));  
    72.     lpDigest = md5.digest();  
    73.       
    74.     memset(cTemp, 0, sizeof(cTemp));  
    75.     memcpy(cTemp, lpDigest, 16);  
    76.     memcpy(&cTemp[16], lpPtUin, nPtUinLen);  
    77.       
    78.     md5.reset();  
    79.     md5.update((const void *)cTemp, 16 + nPtUinLen);  
    80.     lpDigest = md5.digest();  
    81.       
    82.     ToHexStr((const CHAR *)lpDigest, 16, cHex, sizeof(cHex));  
    83.     _strupr(cHex);  
    84.       
    85.     int nHexLen = strlen(cHex);  
    86.     int nVerifyCodeLen = strlen(cVerifyCode);  
    87.     memset(cTemp, 0, sizeof(cTemp));  
    88.     memcpy(cTemp, cHex, nHexLen);  
    89.     memcpy(&cTemp[nHexLen], cVerifyCode, nVerifyCodeLen);  
    90.       
    91.     md5.reset();  
    92.     md5.update((const void *)cTemp, nHexLen + nVerifyCodeLen);  
    93.     lpDigest = md5.digest();  
    94.       
    95.     ToHexStr((const CHAR *)lpDigest, 16, cHex, sizeof(cHex));  
    96.     _strupr(cHex);  
    97.       
    98.     memcpy(lpPwdHash,cHex,strlen(cHex));  
    99.     //Utf8ToUnicode(cHex, lpPwdHash, nLen);  
    100.       
    101.     return TRUE;  
    102. }  

    以下是MD5类代码:

    md5.h

    1. #ifndef MD5_H  
    2. #define MD5_H  
    3.   
    4. #include <string>  
    5. #include <fstream>  
    6.   
    7. #pragma warning(disable:4786)  
    8.   
    9. using std::string;  
    10. using std::ifstream;  
    11.   
    12. /* MD5 declaration. */  
    13. class MD5 {  
    14.   
    15.     /* Type define */  
    16.     typedef unsigned char byte;  
    17.     typedef unsigned int uint32;  
    18.   
    19. public:  
    20.     MD5();  
    21.     MD5(const void* input, size_t length);  
    22.     MD5(const string& str);  
    23.     MD5(ifstream& in);  
    24.     void update(const void* input, size_t length);  
    25.     void update(const string& str);  
    26.     void update(ifstream& in);  
    27.     const byte* digest();  
    28.     string toString();  
    29.     void reset();  
    30.   
    31. private:  
    32.     void update(const byte* input, size_t length);  
    33.     void final();  
    34.     void transform(const byte block[64]);  
    35.     void encode(const uint32* input, byte* output, size_t length);  
    36.     void decode(const byte* input, uint32* output, size_t length);  
    37.     string bytesToHexString(const byte* input, size_t length);  
    38.   
    39.     /* class uncopyable */  
    40.     MD5(const MD5&);  
    41.     MD5& operator=(const MD5&);  
    42.   
    43. private:  
    44.     uint32 _state[4];   /* state (ABCD) */  
    45.     uint32 _count[2];   /* number of bits, modulo 2^64 (low-order word first) */  
    46.     byte _buffer[64];   /* input buffer */  
    47.     byte _digest[16];   /* message digest */  
    48.     bool _finished;     /* calculate finished ? */  
    49.   
    50.     static const byte PADDING[64];  /* padding for calculate */  
    51.     static const char HEX[16];  
    52.     enum { BUFFER_SIZE = 1024 };  
    53. };  
    54.   
    55. #endif /*MD5_H*/  


    md5.cpp

    1. #include "stdafx.h"  
    2. #include "md5.h"  
    3.   
    4. #pragma warning(disable:4786)  
    5.   
    6. using namespace std;  
    7.   
    8. /* Constants for MD5Transform routine. */  
    9. #define S11 7  
    10. #define S12 12  
    11. #define S13 17  
    12. #define S14 22  
    13. #define S21 5  
    14. #define S22 9  
    15. #define S23 14  
    16. #define S24 20  
    17. #define S31 4  
    18. #define S32 11  
    19. #define S33 16  
    20. #define S34 23  
    21. #define S41 6  
    22. #define S42 10  
    23. #define S43 15  
    24. #define S44 21  
    25.   
    26.   
    27. /* F, G, H and I are basic MD5 functions. 
    28. */  
    29. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))  
    30. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))  
    31. #define H(x, y, z) ((x) ^ (y) ^ (z))  
    32. #define I(x, y, z) ((y) ^ ((x) | (~z)))  
    33.   
    34. /* ROTATE_LEFT rotates x left n bits. 
    35. */  
    36. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))  
    37.   
    38. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 
    39. Rotation is separate from addition to prevent recomputation. 
    40. */  
    41. #define FF(a, b, c, d, x, s, ac) {   
    42.     (a) += F ((b), (c), (d)) + (x) + ac;   
    43.     (a) = ROTATE_LEFT ((a), (s));   
    44.     (a) += (b);   
    45. }  
    46. #define GG(a, b, c, d, x, s, ac) {   
    47.     (a) += G ((b), (c), (d)) + (x) + ac;   
    48.     (a) = ROTATE_LEFT ((a), (s));   
    49.     (a) += (b);   
    50. }  
    51. #define HH(a, b, c, d, x, s, ac) {   
    52.     (a) += H ((b), (c), (d)) + (x) + ac;   
    53.     (a) = ROTATE_LEFT ((a), (s));   
    54.     (a) += (b);   
    55. }  
    56. #define II(a, b, c, d, x, s, ac) {   
    57.     (a) += I ((b), (c), (d)) + (x) + ac;   
    58.     (a) = ROTATE_LEFT ((a), (s));   
    59.     (a) += (b);   
    60. }  
    61.   
    62.   
    63. const byte MD5::PADDING[64] = { 0x80 };  
    64. const char MD5::HEX[16] = {  
    65.     '0', '1', '2', '3',  
    66.     '4', '5', '6', '7',  
    67.     '8', '9', 'a', 'b',  
    68.     'c', 'd', 'e', 'f'  
    69. };  
    70.   
    71.   
    72. /* Default construct. */  
    73. MD5::MD5() {  
    74.     reset();  
    75. }  
    76.   
    77. /* Construct a MD5 object with a input buffer. */  
    78. MD5::MD5(const void* input, size_t length) {  
    79.     reset();  
    80.     update(input, length);  
    81. }  
    82.   
    83. /* Construct a MD5 object with a string. */  
    84. MD5::MD5(const string& str) {  
    85.     reset();  
    86.     update(str);  
    87. }  
    88.   
    89. /* Construct a MD5 object with a file. */  
    90. MD5::MD5(ifstream& in) {  
    91.     reset();  
    92.     update(in);  
    93. }  
    94.   
    95. /* Return the message-digest */  
    96. const byte* MD5::digest() {  
    97.   
    98.     if (!_finished) {  
    99.         _finished = true;  
    100.         final();  
    101.     }  
    102.     return _digest;  
    103. }  
    104.   
    105. /* Reset the calculate state */  
    106. void MD5::reset() {  
    107.   
    108.     _finished = false;  
    109.     /* reset number of bits. */  
    110.     _count[0] = _count[1] = 0;  
    111.     /* Load magic initialization constants. */  
    112.     _state[0] = 0x67452301;  
    113.     _state[1] = 0xefcdab89;  
    114.     _state[2] = 0x98badcfe;  
    115.     _state[3] = 0x10325476;  
    116. }  
    117.   
    118. /* Updating the context with a input buffer. */  
    119. void MD5::update(const void* input, size_t length) {  
    120.     update((const byte*)input, length);  
    121. }  
    122.   
    123. /* Updating the context with a string. */  
    124. void MD5::update(const string& str) {  
    125.     update((const byte*)str.c_str(), str.length());  
    126. }  
    127.   
    128. /* Updating the context with a file. */  
    129. void MD5::update(ifstream& in) {  
    130.   
    131.     if (!in) {  
    132.         return;  
    133.     }  
    134.   
    135.     std::streamsize length;  
    136.     char buffer[BUFFER_SIZE];  
    137.     while (!in.eof()) {  
    138.         in.read(buffer, BUFFER_SIZE);  
    139.         length = in.gcount();  
    140.         if (length > 0) {  
    141.             update(buffer, length);  
    142.         }  
    143.     }  
    144.     in.close();  
    145. }  
    146.   
    147. /* MD5 block update operation. Continues an MD5 message-digest 
    148. operation, processing another message block, and updating the 
    149. context. 
    150. */  
    151. void MD5::update(const byte* input, size_t length) {  
    152.   
    153.     uint32 i, index, partLen;  
    154.   
    155.     _finished = false;  
    156.   
    157.     /* Compute number of bytes mod 64 */  
    158.     index = (uint32)((_count[0] >> 3) & 0x3f);  
    159.   
    160.     /* update number of bits */  
    161.     if ((_count[0] += ((uint32)length << 3)) < ((uint32)length << 3)) {  
    162.         ++_count[1];  
    163.     }  
    164.     _count[1] += ((uint32)length >> 29);  
    165.   
    166.     partLen = 64 - index;  
    167.   
    168.     /* transform as many times as possible. */  
    169.     if (length >= partLen) {  
    170.   
    171.         memcpy(&_buffer[index], input, partLen);  
    172.         transform(_buffer);  
    173.   
    174.         for (i = partLen; i + 63 < length; i += 64) {  
    175.             transform(&input[i]);  
    176.         }  
    177.         index = 0;  
    178.   
    179.     } else {  
    180.         i = 0;  
    181.     }  
    182.   
    183.     /* Buffer remaining input */  
    184.     memcpy(&_buffer[index], &input[i], length - i);  
    185. }  
    186.   
    187. /* MD5 finalization. Ends an MD5 message-_digest operation, writing the 
    188. the message _digest and zeroizing the context. 
    189. */  
    190. void MD5::final() {  
    191.   
    192.     byte bits[8];  
    193.     uint32 oldState[4];  
    194.     uint32 oldCount[2];  
    195.     uint32 index, padLen;  
    196.   
    197.     /* Save current state and count. */  
    198.     memcpy(oldState, _state, 16);  
    199.     memcpy(oldCount, _count, 8);  
    200.   
    201.     /* Save number of bits */  
    202.     encode(_count, bits, 8);  
    203.   
    204.     /* Pad out to 56 mod 64. */  
    205.     index = (uint32)((_count[0] >> 3) & 0x3f);  
    206.     padLen = (index < 56) ? (56 - index) : (120 - index);  
    207.     update(PADDING, padLen);  
    208.   
    209.     /* Append length (before padding) */  
    210.     update(bits, 8);  
    211.   
    212.     /* Store state in digest */  
    213.     encode(_state, _digest, 16);  
    214.   
    215.     /* Restore current state and count. */  
    216.     memcpy(_state, oldState, 16);  
    217.     memcpy(_count, oldCount, 8);  
    218. }  
    219.   
    220. /* MD5 basic transformation. Transforms _state based on block. */  
    221. void MD5::transform(const byte block[64]) {  
    222.   
    223.     uint32 a = _state[0], b = _state[1], c = _state[2], d = _state[3], x[16];  
    224.   
    225.     decode(block, x, 64);  
    226.   
    227.     /* Round 1 */  
    228.     FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */  
    229.     FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */  
    230.     FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */  
    231.     FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */  
    232.     FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */  
    233.     FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */  
    234.     FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */  
    235.     FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */  
    236.     FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */  
    237.     FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */  
    238.     FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */  
    239.     FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */  
    240.     FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */  
    241.     FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */  
    242.     FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */  
    243.     FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */  
    244.   
    245.     /* Round 2 */  
    246.     GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */  
    247.     GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */  
    248.     GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */  
    249.     GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */  
    250.     GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */  
    251.     GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */  
    252.     GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */  
    253.     GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */  
    254.     GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */  
    255.     GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */  
    256.     GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */  
    257.     GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */  
    258.     GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */  
    259.     GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */  
    260.     GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */  
    261.     GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */  
    262.   
    263.     /* Round 3 */  
    264.     HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */  
    265.     HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */  
    266.     HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */  
    267.     HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */  
    268.     HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */  
    269.     HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */  
    270.     HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */  
    271.     HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */  
    272.     HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */  
    273.     HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */  
    274.     HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */  
    275.     HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */  
    276.     HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */  
    277.     HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */  
    278.     HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */  
    279.     HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */  
    280.   
    281.     /* Round 4 */  
    282.     II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */  
    283.     II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */  
    284.     II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */  
    285.     II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */  
    286.     II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */  
    287.     II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */  
    288.     II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */  
    289.     II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */  
    290.     II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */  
    291.     II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */  
    292.     II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */  
    293.     II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */  
    294.     II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */  
    295.     II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */  
    296.     II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */  
    297.     II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */  
    298.   
    299.     _state[0] += a;  
    300.     _state[1] += b;  
    301.     _state[2] += c;  
    302.     _state[3] += d;  
    303. }  
    304.   
    305. /* Encodes input (ulong) into output (byte). Assumes length is 
    306. a multiple of 4. 
    307. */  
    308. void MD5::encode(const uint32* input, byte* output, size_t length) {  
    309.   
    310.     for (size_t i = 0, j = 0; j < length; ++i, j += 4) {  
    311.         output[j]= (byte)(input[i] & 0xff);  
    312.         output[j + 1] = (byte)((input[i] >> 8) & 0xff);  
    313.         output[j + 2] = (byte)((input[i] >> 16) & 0xff);  
    314.         output[j + 3] = (byte)((input[i] >> 24) & 0xff);  
    315.     }  
    316. }  
    317.   
    318. /* Decodes input (byte) into output (ulong). Assumes length is 
    319. a multiple of 4. 
    320. */  
    321. void MD5::decode(const byte* input, uint32* output, size_t length) {  
    322.   
    323.     for (size_t i = 0, j = 0; j < length; ++i, j += 4) {  
    324.         output[i] = ((uint32)input[j]) | (((uint32)input[j + 1]) << 8) |  
    325.         (((uint32)input[j + 2]) << 16) | (((uint32)input[j + 3]) << 24);  
    326.     }  
    327. }  
    328.   
    329. /* Convert byte array to hex string. */  
    330. string MD5::bytesToHexString(const byte* input, size_t length) {  
    331.   
    332.     string str;  
    333.     str.reserve(length << 1);  
    334.     for (size_t i = 0; i < length; ++i) {  
    335.         int t = input[i];  
    336.         int a = t / 16;  
    337.         int b = t % 16;  
    338.         str.append(1, HEX[a]);  
    339.         str.append(1, HEX[b]);  
    340.     }  
    341.     return str;  
    342. }  
    343.   
    344. /* Convert digest to string value */  
    345. string MD5::toString() {  
    346.     return bytesToHexString(digest(), 16);  
    347. }  

    完整工程及代码请移步下载:

    http://download.csdn.net/detail/wangningyu/6523439

    备注:由于代码陈旧,已无法获取好友列表,如有需要自行修改即可!

  • 相关阅读:
    Mac 远程连接 Windows
    更优雅的方式: JavaScript 中顺序执行异步函数
    Node.js 上传文件
    win10 系统右键菜单不显示文字(只有小图标)修复方法
    JavaScript 排序算法(JavaScript sorting algorithms)
    JavaScript addEventListener 第三个参数
    前端小白之每天学习记录----angula2--(1)
    H5如何与Native通信Hybrid交互--(1)
    vuex学习--(1)
    前端小白之每天学习记录----php(9)简单的api
  • 原文地址:https://www.cnblogs.com/liaocheng/p/4266184.html
Copyright © 2011-2022 走看看