Web QQ 协议 登录加密算法 —— VC++实现

时间:2023-03-08 17:02:04
Web QQ 协议 登录加密算法 —— VC++实现
  1. BOOL ToHexStr(const CHAR * lpStr, int nSrcLen, CHAR * lpHex, int nDestLen)
  2. {
  3. const CHAR cHexTable[] = "0123456789ABCDEF";
  4. if (lpStr == NULL || nSrcLen <= 0
  5. || lpHex == NULL || nDestLen <= 0)
  6. return FALSE;
  7. if (nDestLen <= nSrcLen * 2)
  8. return FALSE;
  9. int i = 0;
  10. for (int j = 0; j < (int)strlen(lpStr); j++)
  11. {
  12. unsigned int a = (unsigned int)lpStr[j];
  13. lpHex[i++] = cHexTable[(a & 0xf0) >> 4];
  14. lpHex[i++] = cHexTable[(a & 0x0f)];
  15. }
  16. lpHex[i] = '\0';
  17. return TRUE;
  18. }
  19. BOOL StrToHex(const CHAR * lpStr, CHAR * lpHex, int nLen)
  20. {
  21. const CHAR cHexTable[] = "0123456789ABCDEF";
  22. if (lpStr == NULL || lpHex == NULL || nLen <= 0)
  23. return FALSE;
  24. int nSrcLen = strlen(lpStr);
  25. if (nLen <= nSrcLen * 2)
  26. return FALSE;
  27. int i = 0;
  28. for (int j = 0; j < (int)strlen(lpStr); j++)
  29. {
  30. unsigned int a = (unsigned int)lpStr[j];
  31. lpHex[i++] = cHexTable[(a & 0xf0) >> 4];
  32. lpHex[i++] = cHexTable[(a & 0x0f)];
  33. }
  34. lpHex[i] = '\0';
  35. return TRUE;
  36. }
  37. BOOL CalcHash(char *lpQQPwd,char *lpPtUin,char *lpVerifyCode,char *lpPwdHash)
  38. {
  39. CHAR cQQPwd[128] = {0};
  40. CHAR cVerifyCode[128] = {0};
  41. CHAR cHex[36] = {0};
  42. CHAR cTemp[256] = {0};
  43. const byte * lpDigest;
  44. MD5 md5;
  45. int nPtUinLen = 8;
  46. int nLen = 8;
  47. if (NULL == lpQQPwd || NULL == lpVerifyCode
  48. || NULL == lpPtUin || nPtUinLen <= 0
  49. || NULL == lpPwdHash || nLen <= 0)
  50. return FALSE;
  51. memcpy(cQQPwd,lpQQPwd,strlen(lpQQPwd));
  52. memcpy(cVerifyCode,lpVerifyCode,strlen(lpVerifyCode));
  53. //UnicodeToUtf8(lpQQPwd, cQQPwd, sizeof(cQQPwd));
  54. //UnicodeToUtf8(lpVerifyCode, cVerifyCode, sizeof(cVerifyCode));
  55. md5.reset();
  56. md5.update((const void *)cQQPwd, strlen(cQQPwd));
  57. lpDigest = md5.digest();
  58. memset(cTemp, 0, sizeof(cTemp));
  59. memcpy(cTemp, lpDigest, 16);
  60. memcpy(&cTemp[16], lpPtUin, nPtUinLen);
  61. md5.reset();
  62. md5.update((const void *)cTemp, 16 + nPtUinLen);
  63. lpDigest = md5.digest();
  64. ToHexStr((const CHAR *)lpDigest, 16, cHex, sizeof(cHex));
  65. _strupr(cHex);
  66. int nHexLen = strlen(cHex);
  67. int nVerifyCodeLen = strlen(cVerifyCode);
  68. memset(cTemp, 0, sizeof(cTemp));
  69. memcpy(cTemp, cHex, nHexLen);
  70. memcpy(&cTemp[nHexLen], cVerifyCode, nVerifyCodeLen);
  71. md5.reset();
  72. md5.update((const void *)cTemp, nHexLen + nVerifyCodeLen);
  73. lpDigest = md5.digest();
  74. ToHexStr((const CHAR *)lpDigest, 16, cHex, sizeof(cHex));
  75. _strupr(cHex);
  76. memcpy(lpPwdHash,cHex,strlen(cHex));
  77. //Utf8ToUnicode(cHex, lpPwdHash, nLen);
  78. return TRUE;
  79. }

以下是MD5类代码:

md5.h

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

md5.cpp

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

Web QQ 协议 登录加密算法 —— VC++实现

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

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

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