关键词搜索

源码搜索 ×
×

tars源码分析之17

发布2022-07-10浏览724次

详情内容

tea可不是茶,而是腾讯采用的一个加密算法,细节不用掌握,知道功能就行:

  1. #include "util/tc_tea.h"
  2. #include <arpa/inet.h>
  3. #include <iostream>
  4. #include <sstream>
  5. #include <stdlib.h>
  6. namespace tars
  7. {
  8. vector<char> TC_Tea::encrypt(const char *key, const char *sIn, size_t iLength)
  9. {
  10. size_t outlen = 16 + 2*iLength;
  11. vector<char> v;
  12. v.resize(outlen);
  13. oi_symmetry_encrypt(sIn, iLength, key, &v[0], &outlen);
  14. v.resize(outlen);
  15. return v;
  16. }
  17. vector<char> TC_Tea::decrypt(const char *key, const char *sIn, size_t iLength)
  18. {
  19. size_t outlen = 2*iLength;
  20. vector<char> v;
  21. v.resize(outlen);
  22. if (!oi_symmetry_decrypt(sIn, iLength, key, &v[0], &outlen))
  23. {
  24. throw TC_Tea_Exception("[TC_Tea::decrypt] decrypt error.");
  25. }
  26. v.resize(outlen);
  27. return v;
  28. }
  29. vector<char> TC_Tea::encrypt2(const char *key, const char *sIn, size_t iLength)
  30. {
  31. size_t outlen = oi_symmetry_encrypt2_len(iLength);
  32. vector<char> v;
  33. v.resize(outlen);
  34. oi_symmetry_encrypt2(sIn, iLength, key, &v[0], &outlen);
  35. v.resize(outlen);
  36. return v;
  37. }
  38. vector<char> TC_Tea::decrypt2(const char *key, const char *sIn, size_t iLength)
  39. {
  40. size_t outlen = 2*iLength;
  41. vector<char> v;
  42. v.resize(outlen);
  43. if (!oi_symmetry_decrypt2(sIn, iLength, key, &v[0], &outlen))
  44. {
  45. throw TC_Tea_Exception("[TC_Tea::decrypt] decrypt error.");
  46. }
  47. v.resize(outlen);
  48. return v;
  49. }
  50. //
  51. const uint32_t DELTA = 0x9e3779b9;
  52. #define SALT_LEN 2
  53. #define ZERO_LEN 7
  54. #define ROUNDS 16
  55. #define LOG_ROUNDS 4
  56. void TC_Tea::TeaEncryptECB(const char *pInBuf, const char *pKey, char *pOutBuf)
  57. {
  58. uint32_t y, z;
  59. uint32_t sum;
  60. uint32_t k[4];
  61. int i;
  62. /*plain-text is TCP/IP-endian;*/
  63. /*GetBlockBigEndian(in, y, z);*/
  64. y = ntohl(*((uint32_t*)pInBuf));
  65. z = ntohl(*((uint32_t*)(pInBuf+4)));
  66. /*TCP/IP network byte order (which is big-endian).*/
  67. for ( i = 0; i<4; i++)
  68. {
  69. /*now key is TCP/IP-endian;*/
  70. k[i] = ntohl(*((uint32_t*)(pKey+i*4)));
  71. }
  72. sum = 0;
  73. for (i=0; i<ROUNDS; i++)
  74. {
  75. sum += DELTA;
  76. y += (z << 4) + (k[0] ^ z) + (sum ^ (z >> 5)) + k[1];
  77. z += (y << 4) + (k[2] ^ y) + (sum ^ (y >> 5)) + k[3];
  78. }
  79. *((uint32_t*)pOutBuf) = htonl(y);
  80. *((uint32_t*)(pOutBuf+4)) = htonl(z);
  81. /*now encrypted buf is TCP/IP-endian;*/
  82. }
  83. void TC_Tea::TeaDecryptECB(const char *pInBuf, const char *pKey, char *pOutBuf)
  84. {
  85. uint32_t y, z, sum;
  86. uint32_t k[4];
  87. int i;
  88. /*now encrypted buf is TCP/IP-endian;*/
  89. /*TCP/IP network byte order (which is big-endian).*/
  90. y = ntohl(*((uint32_t*)pInBuf));
  91. z = ntohl(*((uint32_t*)(pInBuf+4)));
  92. for ( i=0; i<4; i++)
  93. {
  94. /*key is TCP/IP-endian;*/
  95. k[i] = ntohl(*((uint32_t*)(pKey+i*4)));
  96. }
  97. sum = DELTA << LOG_ROUNDS;
  98. for (i=0; i<ROUNDS; i++)
  99. {
  100. z -= (y << 4) + (k[2] ^ y) + (sum ^ (y >> 5)) + k[3];
  101. y -= (z << 4) + (k[0] ^ z) + (sum ^ (z >> 5)) + k[1];
  102. sum -= DELTA;
  103. }
  104. *((uint32_t*)pOutBuf) = htonl(y);
  105. *((uint32_t*)(pOutBuf+4)) = htonl(z);
  106. /*now plain-text is TCP/IP-endian;*/
  107. }
  108. //
  109. ///
  110. /*
  111. ÊäÈë:pInBufΪÐè¼ÓÃܵÄÃ÷ÎIJ¿·Ö(Body),nInBufLenΪpInBuf³¤¶È;
  112. Êä³ö:pOutBufΪÃÜÎĸñʽ,pOutBufLenΪpOutBufµÄ³¤¶ÈÊÇ8byteµÄ±¶Êý;
  113. */
  114. /*TEA¼ÓÃÜËã·¨,CBCģʽ*/
  115. /*ÃÜÎĸñʽ:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
  116. void TC_Tea::oi_symmetry_encrypt(const char* pInBuf, size_t nInBufLen, const char* pKey, char* pOutBuf, size_t *pOutBufLen)
  117. {
  118. size_t nPadSaltBodyZeroLen/*PadLen(1byte)+Salt+Body+ZeroµÄ³¤¶È*/;
  119. size_t nPadlen;
  120. char src_buf[8], zero_iv[8], *iv_buf;
  121. size_t src_i, i, j;
  122. /*¸ù¾ÝBody³¤¶È¼ÆËãPadLen,×îС±ØÐ賤¶È±ØÐèΪ8byteµÄÕûÊý±¶*/
  123. nPadSaltBodyZeroLen = nInBufLen/*Body³¤¶È*/+1+SALT_LEN+ZERO_LEN/*PadLen(1byte)+Salt(2byte)+Zero(7byte)*/;
  124. if((nPadlen=nPadSaltBodyZeroLen%8)) /*len=nSaltBodyZeroLen%8*/
  125. {
  126. /*Ä£8Óà0Ðè²¹0,Óà1²¹7,Óà2²¹6,...,Óà7²¹1*/
  127. nPadlen=8-nPadlen;
  128. }
  129. /*srand( (unsigned)time( NULL ) ); ³õʼ»¯Ëæ»úÊý*/
  130. /*¼ÓÃܵÚÒ»¿éÊý¾Ý(8byte),È¡Ç°Ãæ10byte*/
  131. src_buf[0] = (((char)rand()) & 0x0f8)/*×îµÍÈýλ´æPadLen,ÇåÁã*/ | (char)nPadlen;
  132. src_i = 1; /*src_iÖ¸Ïòsrc_bufÏÂÒ»¸öλÖÃ*/
  133. while(nPadlen--)
  134. src_buf[src_i++]=(char)rand(); /*Padding*/
  135. /*come here, i must <= 8*/
  136. memset(zero_iv, 0, 8);
  137. iv_buf = zero_iv; /*make iv*/
  138. *pOutBufLen = 0; /*init OutBufLen*/
  139. for (i=1;i<=SALT_LEN;) /*Salt(2byte)*/
  140. {
  141. if (src_i<8)
  142. {
  143. src_buf[src_i++]=(char)rand();
  144. i++; /*i inc in here*/
  145. }
  146. if (src_i==8)
  147. {
  148. /*src_i==8*/
  149. for (j=0;j<8;j++) /*CBC XOR*/
  150. src_buf[j]^=iv_buf[j];
  151. /*pOutBuffer¡¢pInBuffer¾ùΪ8byte, pKeyΪ16byte*/
  152. TeaEncryptECB(src_buf, pKey, pOutBuf);
  153. src_i=0;
  154. iv_buf=pOutBuf;
  155. *pOutBufLen+=8;
  156. pOutBuf+=8;
  157. }
  158. }
  159. /*src_iÖ¸Ïòsrc_bufÏÂÒ»¸öλÖÃ*/
  160. while(nInBufLen)
  161. {
  162. if (src_i<8)
  163. {
  164. src_buf[src_i++]=*(pInBuf++);
  165. nInBufLen--;
  166. }
  167. if (src_i==8)
  168. {
  169. /*src_i==8*/
  170. for (i=0;i<8;i++) /*CBC XOR*/
  171. src_buf[i]^=iv_buf[i];
  172. /*pOutBuffer¡¢pInBuffer¾ùΪ8byte, pKeyΪ16byte*/
  173. TeaEncryptECB(src_buf, pKey, pOutBuf);
  174. src_i=0;
  175. iv_buf=pOutBuf;
  176. *pOutBufLen+=8;
  177. pOutBuf+=8;
  178. }
  179. }
  180. /*src_iÖ¸Ïòsrc_bufÏÂÒ»¸öλÖÃ*/
  181. for (i=1;i<=ZERO_LEN;)
  182. {
  183. if (src_i<8)
  184. {
  185. src_buf[src_i++]=0;
  186. i++; /*i inc in here*/
  187. }
  188. if (src_i==8)
  189. {
  190. /*src_i==8*/
  191. for (j=0;j<8;j++) /*CBC XOR*/
  192. src_buf[j]^=iv_buf[j];
  193. /*pOutBuffer¡¢pInBuffer¾ùΪ8byte, pKeyΪ16byte*/
  194. TeaEncryptECB(src_buf, pKey, pOutBuf);
  195. src_i=0;
  196. iv_buf=pOutBuf;
  197. *pOutBufLen+=8;
  198. pOutBuf+=8;
  199. }
  200. }
  201. }
  202. /*
  203. ÊäÈë:pInBufΪÃÜÎĸñʽ,nInBufLenΪpInBufµÄ³¤¶ÈÊÇ8byteµÄ±¶Êý;
  204. Êä³ö:pOutBufΪÃ÷ÎÄ(Body),pOutBufLenΪpOutBufµÄ³¤¶È;
  205. ·µ»ØÖµ:Èç¹û¸ñʽÕýÈ··µ»ØTRUE;
  206. */
  207. /*TEA½âÃÜËã·¨,CBCģʽ*/
  208. /*ÃÜÎĸñʽ:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
  209. bool TC_Tea::oi_symmetry_decrypt(const char* pInBuf, size_t nInBufLen, const char* pKey, char* pOutBuf, size_t *pOutBufLen)
  210. {
  211. size_t nPadLen, nPlainLen;
  212. char dest_buf[8];
  213. const char *iv_buf;
  214. size_t dest_i, i, j;
  215. if ((nInBufLen%8) || (nInBufLen<16)) return false;
  216. TeaDecryptECB(pInBuf, pKey, dest_buf);
  217. nPadLen = dest_buf[0] & 0x7/*Ö»Òª×îµÍÈýλ*/;
  218. /*ÃÜÎĸñʽ:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
  219. i = nInBufLen-1/*PadLen(1byte)*/-nPadLen-SALT_LEN-ZERO_LEN; /*Ã÷Îij¤¶È*/
  220. if (*pOutBufLen<i) return false;
  221. *pOutBufLen = i;
  222. //here it should never success
  223. //if (*pOutBufLen < 0) return false;
  224. iv_buf = pInBuf; /*init iv*/
  225. nInBufLen -= 8;
  226. pInBuf += 8;
  227. dest_i=1; /*dest_iÖ¸Ïòdest_bufÏÂÒ»¸öλÖÃ*/
  228. /*°ÑPaddingÂ˵ô*/
  229. dest_i+=nPadLen;
  230. /*dest_i must <=8*/
  231. /*°ÑSaltÂ˵ô*/
  232. for (i=1; i<=SALT_LEN;)
  233. {
  234. if (dest_i<8)
  235. {
  236. dest_i++;
  237. i++;
  238. }
  239. if (dest_i==8)
  240. {
  241. /*dest_i==8*/
  242. TeaDecryptECB(pInBuf, pKey, dest_buf);
  243. for (j=0; j<8; j++)
  244. dest_buf[j]^=iv_buf[j];
  245. iv_buf = pInBuf;
  246. nInBufLen -= 8;
  247. pInBuf += 8;
  248. dest_i=0; /*dest_iÖ¸Ïòdest_bufÏÂÒ»¸öλÖÃ*/
  249. }
  250. }
  251. /*»¹Ô­Ã÷ÎÄ*/
  252. nPlainLen=*pOutBufLen;
  253. while (nPlainLen)
  254. {
  255. if (dest_i<8)
  256. {
  257. *(pOutBuf++)=dest_buf[dest_i++];
  258. nPlainLen--;
  259. }
  260. else if (dest_i==8)
  261. {
  262. /*dest_i==8*/
  263. TeaDecryptECB(pInBuf, pKey, dest_buf);
  264. for (i=0; i<8; i++)
  265. dest_buf[i]^=iv_buf[i];
  266. iv_buf = pInBuf;
  267. nInBufLen -= 8;
  268. pInBuf += 8;
  269. dest_i=0; /*dest_iÖ¸Ïòdest_bufÏÂÒ»¸öλÖÃ*/
  270. }
  271. }
  272. /*УÑéZero*/
  273. for (i=1;i<=ZERO_LEN;)
  274. {
  275. if (dest_i<8)
  276. {
  277. if(dest_buf[dest_i++]) return false;
  278. i++;
  279. }
  280. else if (dest_i==8)
  281. {
  282. /*dest_i==8*/
  283. TeaDecryptECB(pInBuf, pKey, dest_buf);
  284. for (j=0; j<8; j++)
  285. dest_buf[j]^=iv_buf[j];
  286. iv_buf = pInBuf;
  287. nInBufLen -= 8;
  288. pInBuf += 8;
  289. dest_i=0; /*dest_iÖ¸Ïòdest_bufÏÂÒ»¸öλÖÃ*/
  290. }
  291. }
  292. return true;
  293. }
  294. //
  295. /*TEA¼ÓÃÜËã·¨,CBCģʽ*/
  296. /*ÃÜÎĸñʽ:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
  297. size_t TC_Tea::oi_symmetry_encrypt2_len(size_t nInBufLen)
  298. {
  299. size_t nPadSaltBodyZeroLen/*PadLen(1byte)+Salt+Body+ZeroµÄ³¤¶È*/;
  300. size_t nPadlen;
  301. /*¸ù¾ÝBody³¤¶È¼ÆËãPadLen,×îС±ØÐ賤¶È±ØÐèΪ8byteµÄÕûÊý±¶*/
  302. nPadSaltBodyZeroLen = nInBufLen/*Body³¤¶È*/+1+SALT_LEN+ZERO_LEN/*PadLen(1byte)+Salt(2byte)+Zero(7byte)*/;
  303. if((nPadlen=nPadSaltBodyZeroLen%8)) /*len=nSaltBodyZeroLen%8*/
  304. {
  305. /*Ä£8Óà0Ðè²¹0,Óà1²¹7,Óà2²¹6,...,Óà7²¹1*/
  306. nPadlen=8-nPadlen;
  307. }
  308. return nPadSaltBodyZeroLen+nPadlen;
  309. }
  310. /*pKeyΪ16byte*/
  311. /*
  312. ÊäÈë:pInBufΪÐè¼ÓÃܵÄÃ÷ÎIJ¿·Ö(Body),nInBufLenΪpInBuf³¤¶È;
  313. Êä³ö:pOutBufΪÃÜÎĸñʽ,pOutBufLenΪpOutBufµÄ³¤¶ÈÊÇ8byteµÄ±¶Êý;
  314. */
  315. /*TEA¼ÓÃÜËã·¨,CBCģʽ*/
  316. /*ÃÜÎĸñʽ:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
  317. void TC_Tea::oi_symmetry_encrypt2(const char* pInBuf, size_t nInBufLen, const char* pKey, char* pOutBuf, size_t *pOutBufLen)
  318. {
  319. size_t nPadSaltBodyZeroLen/*PadLen(1byte)+Salt+Body+ZeroµÄ³¤¶È*/;
  320. size_t nPadlen;
  321. char src_buf[8], iv_plain[8], *iv_crypt;
  322. size_t src_i, i, j;
  323. /*¸ù¾ÝBody³¤¶È¼ÆËãPadLen,×îС±ØÐ賤¶È±ØÐèΪ8byteµÄÕûÊý±¶*/
  324. nPadSaltBodyZeroLen = nInBufLen/*Body³¤¶È*/+1+SALT_LEN+ZERO_LEN/*PadLen(1byte)+Salt(2byte)+Zero(7byte)*/;
  325. if((nPadlen=nPadSaltBodyZeroLen%8)) /*len=nSaltBodyZeroLen%8*/
  326. {
  327. /*Ä£8Óà0Ðè²¹0,Óà1²¹7,Óà2²¹6,...,Óà7²¹1*/
  328. nPadlen=8-nPadlen;
  329. }
  330. /*srand( (unsigned)time( NULL ) ); ³õʼ»¯Ëæ»úÊý*/
  331. /*¼ÓÃܵÚÒ»¿éÊý¾Ý(8byte),È¡Ç°Ãæ10byte*/
  332. // src_buf[0] = ((char)rand()) & 0x0f8/*×îµÍÈýλ´æPadLen,ÇåÁã*/ | (char)nPadlen;
  333. src_buf[0] = (((char)rand()) & 0x0f8) | (char)nPadlen;
  334. src_i = 1; /*src_iÖ¸Ïòsrc_bufÏÂÒ»¸öλÖÃ*/
  335. while(nPadlen--)
  336. src_buf[src_i++]=(char)rand(); /*Padding*/
  337. /*come here, src_i must <= 8*/
  338. for ( i=0; i<8; i++)
  339. iv_plain[i] = 0;
  340. iv_crypt = iv_plain; /*make zero iv*/
  341. *pOutBufLen = 0; /*init OutBufLen*/
  342. for (i=1;i<=SALT_LEN;) /*Salt(2byte)*/
  343. {
  344. if (src_i<8)
  345. {
  346. src_buf[src_i++]=(char)rand();
  347. i++; /*i inc in here*/
  348. }
  349. if (src_i==8)
  350. {
  351. /*src_i==8*/
  352. for (j=0;j<8;j++) /*¼ÓÃÜÇ°Òì»òÇ°8¸öbyteµÄÃÜÎÄ(iv_cryptÖ¸ÏòµÄ)*/
  353. src_buf[j]^=iv_crypt[j];
  354. /*pOutBuffer¡¢pInBuffer¾ùΪ8byte, pKeyΪ16byte*/
  355. /*¼ÓÃÜ*/
  356. TeaEncryptECB(src_buf, pKey, pOutBuf);
  357. for (j=0;j<8;j++) /*¼ÓÃܺóÒì»òÇ°8¸öbyteµÄÃ÷ÎÄ(iv_plainÖ¸ÏòµÄ)*/
  358. pOutBuf[j]^=iv_plain[j];
  359. /*±£´æµ±Ç°µÄiv_plain*/
  360. for (j=0;j<8;j++)
  361. iv_plain[j]=src_buf[j];
  362. /*¸üÐÂiv_crypt*/
  363. src_i=0;
  364. iv_crypt=pOutBuf;
  365. *pOutBufLen+=8;
  366. pOutBuf+=8;
  367. }
  368. }
  369. /*src_iÖ¸Ïòsrc_bufÏÂÒ»¸öλÖÃ*/
  370. while(nInBufLen)
  371. {
  372. if (src_i<8)
  373. {
  374. src_buf[src_i++]=*(pInBuf++);
  375. nInBufLen--;
  376. }
  377. if (src_i==8)
  378. {
  379. /*src_i==8*/
  380. for (j=0;j<8;j++) /*¼ÓÃÜÇ°Òì»òÇ°8¸öbyteµÄÃÜÎÄ(iv_cryptÖ¸ÏòµÄ)*/
  381. src_buf[j]^=iv_crypt[j];
  382. /*pOutBuffer¡¢pInBuffer¾ùΪ8byte, pKeyΪ16byte*/
  383. TeaEncryptECB(src_buf, pKey, pOutBuf);
  384. for (j=0;j<8;j++) /*¼ÓÃܺóÒì»òÇ°8¸öbyteµÄÃ÷ÎÄ(iv_plainÖ¸ÏòµÄ)*/
  385. pOutBuf[j]^=iv_plain[j];
  386. /*±£´æµ±Ç°µÄiv_plain*/
  387. for (j=0;j<8;j++)
  388. iv_plain[j]=src_buf[j];
  389. src_i=0;
  390. iv_crypt=pOutBuf;
  391. *pOutBufLen+=8;
  392. pOutBuf+=8;
  393. }
  394. }
  395. /*src_iÖ¸Ïòsrc_bufÏÂÒ»¸öλÖÃ*/
  396. for (i=1;i<=ZERO_LEN;)
  397. {
  398. if (src_i<8)
  399. {
  400. src_buf[src_i++]=0;
  401. i++; /*i inc in here*/
  402. }
  403. if (src_i==8)
  404. {
  405. /*src_i==8*/
  406. for (j=0;j<8;j++) /*¼ÓÃÜÇ°Òì»òÇ°8¸öbyteµÄÃÜÎÄ(iv_cryptÖ¸ÏòµÄ)*/
  407. src_buf[j]^=iv_crypt[j];
  408. /*pOutBuffer¡¢pInBuffer¾ùΪ8byte, pKeyΪ16byte*/
  409. TeaEncryptECB(src_buf, pKey, pOutBuf);
  410. for (j=0;j<8;j++) /*¼ÓÃܺóÒì»òÇ°8¸öbyteµÄÃ÷ÎÄ(iv_plainÖ¸ÏòµÄ)*/
  411. pOutBuf[j]^=iv_plain[j];
  412. /*±£´æµ±Ç°µÄiv_plain*/
  413. for (j=0;j<8;j++)
  414. iv_plain[j]=src_buf[j];
  415. src_i=0;
  416. iv_crypt=pOutBuf;
  417. *pOutBufLen+=8;
  418. pOutBuf+=8;
  419. }
  420. }
  421. }
  422. /*
  423. ÊäÈë:pInBufΪÃÜÎĸñʽ,nInBufLenΪpInBufµÄ³¤¶ÈÊÇ8byteµÄ±¶Êý; *pOutBufLenΪ½ÓÊÕ»º³åÇøµÄ³¤¶È
  424. Ìرð×¢Òâ*pOutBufLenÓ¦Ô¤ÖýÓÊÕ»º³åÇøµÄ³¤¶È!
  425. Êä³ö:pOutBufΪÃ÷ÎÄ(Body),pOutBufLenΪpOutBufµÄ³¤¶È,ÖÁÉÙÓ¦Ô¤ÁônInBufLen-10;
  426. ·µ»ØÖµ:Èç¹û¸ñʽÕýÈ··µ»ØTRUE;
  427. */
  428. /*TEA½âÃÜËã·¨,CBCģʽ*/
  429. /*ÃÜÎĸñʽ:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
  430. bool TC_Tea::oi_symmetry_decrypt2(const char* pInBuf, size_t nInBufLen, const char* pKey, char* pOutBuf, size_t *pOutBufLen)
  431. {
  432. size_t nPadLen, nPlainLen;
  433. char dest_buf[8], zero_buf[8];
  434. const char *iv_pre_crypt, *iv_cur_crypt;
  435. size_t dest_i, i, j;
  436. //const char *pInBufBoundary;
  437. size_t nBufPos;
  438. nBufPos = 0;
  439. if ((nInBufLen%8) || (nInBufLen<16)) return false;
  440. TeaDecryptECB(pInBuf, pKey, dest_buf);
  441. nPadLen = dest_buf[0] & 0x7/*Ö»Òª×îµÍÈýλ*/;
  442. /*ÃÜÎĸñʽ:PadLen(1byte)+Padding(var,0-7byte)+Salt(2byte)+Body(var byte)+Zero(7byte)*/
  443. i = nInBufLen-1/*PadLen(1byte)*/-nPadLen-SALT_LEN-ZERO_LEN; /*Ã÷Îij¤¶È*/
  444. if ((*pOutBufLen<i)) return false;
  445. *pOutBufLen = i;
  446. //pInBufBoundary = pInBuf + nInBufLen; /*ÊäÈ뻺³åÇøµÄ±ß½ç£¬ÏÂÃæ²»ÄÜpInBuf>=pInBufBoundary*/
  447. for ( i=0; i<8; i++)
  448. zero_buf[i] = 0;
  449. iv_pre_crypt = zero_buf;
  450. iv_cur_crypt = pInBuf; /*init iv*/
  451. pInBuf += 8;
  452. nBufPos += 8;
  453. dest_i=1; /*dest_iÖ¸Ïòdest_bufÏÂÒ»¸öλÖÃ*/
  454. /*°ÑPaddingÂ˵ô*/
  455. dest_i+=nPadLen;
  456. /*dest_i must <=8*/
  457. /*°ÑSaltÂ˵ô*/
  458. for (i=1; i<=SALT_LEN;)
  459. {
  460. if (dest_i<8)
  461. {
  462. dest_i++;
  463. i++;
  464. }
  465. else if (dest_i==8)
  466. {
  467. /*½â¿ªÒ»¸öеļÓÃÜ¿é*/
  468. /*¸Ä±äÇ°Ò»¸ö¼ÓÃÜ¿éµÄÖ¸Õë*/
  469. iv_pre_crypt = iv_cur_crypt;
  470. iv_cur_crypt = pInBuf;
  471. /*Òì»òÇ°Ò»¿éÃ÷ÎÄ(ÔÚdest_buf[]ÖÐ)*/
  472. for (j=0; j<8; j++)
  473. {
  474. if( (nBufPos + j) >= nInBufLen)
  475. {
  476. return false;
  477. }
  478. dest_buf[j]^=pInBuf[j];
  479. }
  480. /*dest_i==8*/
  481. TeaDecryptECB(dest_buf, pKey, dest_buf);
  482. /*ÔÚÈ¡³öµÄʱºò²ÅÒì»òÇ°Ò»¿éÃÜÎÄ(iv_pre_crypt)*/
  483. pInBuf += 8;
  484. nBufPos += 8;
  485. dest_i=0; /*dest_iÖ¸Ïòdest_bufÏÂÒ»¸öλÖÃ*/
  486. }
  487. }
  488. /*»¹Ô­Ã÷ÎÄ*/
  489. nPlainLen=*pOutBufLen;
  490. while (nPlainLen)
  491. {
  492. if (dest_i<8)
  493. {
  494. *(pOutBuf++)=dest_buf[dest_i]^iv_pre_crypt[dest_i];
  495. dest_i++;
  496. nPlainLen--;
  497. }
  498. else if (dest_i==8)
  499. {
  500. /*dest_i==8*/
  501. /*¸Ä±äÇ°Ò»¸ö¼ÓÃÜ¿éµÄÖ¸Õë*/
  502. iv_pre_crypt = iv_cur_crypt;
  503. iv_cur_crypt = pInBuf;
  504. /*½â¿ªÒ»¸öеļÓÃÜ¿é*/
  505. /*Òì»òÇ°Ò»¿éÃ÷ÎÄ(ÔÚdest_buf[]ÖÐ)*/
  506. for (j=0; j<8; j++)
  507. {
  508. if( (nBufPos + j) >= nInBufLen)
  509. {
  510. return false;
  511. }
  512. dest_buf[j]^=pInBuf[j];
  513. }
  514. TeaDecryptECB(dest_buf, pKey, dest_buf);
  515. /*ÔÚÈ¡³öµÄʱºò²ÅÒì»òÇ°Ò»¿éÃÜÎÄ(iv_pre_crypt)*/
  516. pInBuf += 8;
  517. nBufPos += 8;
  518. dest_i=0; /*dest_iÖ¸Ïòdest_bufÏÂÒ»¸öλÖÃ*/
  519. }
  520. }
  521. /*УÑéZero*/
  522. for (i=1;i<=ZERO_LEN;)
  523. {
  524. if (dest_i<8)
  525. {
  526. if(dest_buf[dest_i]^iv_pre_crypt[dest_i]) return false;
  527. dest_i++;
  528. i++;
  529. }
  530. else if (dest_i==8)
  531. {
  532. /*¸Ä±äÇ°Ò»¸ö¼ÓÃÜ¿éµÄÖ¸Õë*/
  533. iv_pre_crypt = iv_cur_crypt;
  534. iv_cur_crypt = pInBuf;
  535. /*½â¿ªÒ»¸öеļÓÃÜ¿é*/
  536. /*Òì»òÇ°Ò»¿éÃ÷ÎÄ(ÔÚdest_buf[]ÖÐ)*/
  537. for (j=0; j<8; j++)
  538. {
  539. if( (nBufPos + j) >= nInBufLen)
  540. {
  541. return false;
  542. }
  543. dest_buf[j]^=pInBuf[j];
  544. }
  545. TeaDecryptECB(dest_buf, pKey, dest_buf);
  546. /*ÔÚÈ¡³öµÄʱºò²ÅÒì»òÇ°Ò»¿éÃÜÎÄ(iv_pre_crypt)*/
  547. pInBuf += 8;
  548. nBufPos += 8;
  549. dest_i=0; /*dest_iÖ¸Ïòdest_bufÏÂÒ»¸öλÖÃ*/
  550. }
  551. }
  552. return true;
  553. }
  554. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载