AuthLicense.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include "md5.h"
  5. /*
  6. preHandleIndex:
  7. 0: |
  8. 1: &
  9. 2: ^
  10. 3: !^
  11. 4: +
  12. 5: -
  13. */
  14. int8_t preHandle(uint32_t *preGUIDptr, uint32_t key, uint8_t preHandleIndex)
  15. {
  16. uint8_t i;
  17. switch(preHandleIndex)
  18. {
  19. case 0: // |
  20. for(i=0;i<3;i++)
  21. {
  22. preGUIDptr[i] |= key;
  23. }
  24. break;
  25. case 1: // &
  26. for(i=0;i<3;i++)
  27. {
  28. preGUIDptr[i] &= key;
  29. }
  30. break;
  31. case 2: // ^
  32. for(i=0;i<3;i++)
  33. {
  34. preGUIDptr[i] ^= key;
  35. }
  36. break;
  37. case 3: // !^
  38. for(i=0;i<3;i++)
  39. {
  40. preGUIDptr[i] ^= key;
  41. preGUIDptr[i] = ~preGUIDptr[i];
  42. }
  43. break;
  44. case 4: // +
  45. for(i=0;i<3;i++)
  46. {
  47. preGUIDptr[i] += key;
  48. }
  49. break;
  50. case 5: // -
  51. for(i=0;i<3;i++)
  52. {
  53. preGUIDptr[i] -= key;
  54. }
  55. break;
  56. default:
  57. printf("´íÎóµ±Ç°Ñ¡ÔñµÄÔËË㷽ʽ²»¿ÉÓÃ\r\n");
  58. return -1;
  59. }
  60. return 1;
  61. }
  62. int8_t bitDiffuse(uint8_t decrypt[16], uint8_t License[256], uint8_t bitSelectIndex)
  63. {
  64. uint8_t i, j;
  65. //À©É¢µ½128×Ö½Ú
  66. switch (bitSelectIndex) {
  67. case 0:
  68. for(i=0;i<16;i++)
  69. {
  70. for(j=0;j<8;j++)
  71. {
  72. if((decrypt[i]&(0x01<<j)))
  73. License[i*8+j] |= 0x01;
  74. else
  75. License[i*8+j] &= ~0x01;
  76. }
  77. }
  78. break;
  79. case 1:
  80. for(i=0;i<16;i++)
  81. {
  82. for(j=0;j<8;j++)
  83. {
  84. if((decrypt[i]&(0x01<<j)))
  85. License[i*8+j] |= 0x02;
  86. else
  87. License[i*8+j] &= ~0x02;
  88. }
  89. }
  90. break;
  91. case 2:
  92. for(i=0;i<16;i++)
  93. {
  94. for(j=0;j<8;j++)
  95. {
  96. if((decrypt[i]&(0x01<<j)))
  97. License[i*8+j] |= 0x04;
  98. else
  99. License[i*8+j] &= ~0x04;
  100. }
  101. }
  102. break;
  103. case 3:
  104. for(i=0;i<16;i++)
  105. {
  106. for(j=0;j<8;j++)
  107. {
  108. if((decrypt[i]&(0x01<<j)))
  109. License[i*8+j] |= 0x08;
  110. else
  111. License[i*8+j] &= ~0x08;
  112. }
  113. }
  114. break;
  115. case 4:
  116. for(i=0;i<16;i++)
  117. {
  118. for(j=0;j<8;j++)
  119. {
  120. if((decrypt[i]&(0x01<<j)))
  121. License[i*8+j] |= 0x10;
  122. else
  123. License[i*8+j] &= ~0x10;
  124. }
  125. }
  126. break;
  127. case 5:
  128. for(i=0;i<16;i++)
  129. {
  130. for(j=0;j<8;j++)
  131. {
  132. if((decrypt[i]&(0x01<<j)))
  133. License[i*8+j] |= 0x20;
  134. else
  135. License[i*8+j] &= ~0x20;
  136. }
  137. }
  138. break;
  139. case 6:
  140. for(i=0;i<16;i++)
  141. {
  142. for(j=0;j<8;j++)
  143. {
  144. if((decrypt[i]&(0x01<<j)))
  145. License[i*8+j] |= 0x40;
  146. else
  147. License[i*8+j] &= ~0x40;
  148. }
  149. }
  150. break;
  151. case 7:
  152. for(i=0;i<16;i++)
  153. {
  154. for(j=0;j<8;j++)
  155. {
  156. if((decrypt[i]&(0x01<<j)))
  157. License[i*8+j] |= 0x80;
  158. else
  159. License[i*8+j] &= ~0x80;
  160. }
  161. }
  162. break;
  163. default:
  164. printf("´íÎóÎÞЧµÄÀ©É¢Î»Ñ¡Ôñ\r\n");
  165. return -1;
  166. }
  167. return 1;
  168. }
  169. int8_t Authorize(uint8_t decrypt[16], uint8_t License[128], uint8_t bitSelectIndex)
  170. {
  171. uint8_t i, j;
  172. switch (bitSelectIndex) {
  173. case 0:
  174. for(i=0;i<16;i++)
  175. {
  176. for(j=0;j<8;j++)
  177. {
  178. if ( ( License[i*8+j] & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  179. {
  180. //printf("run\r\n");
  181. }
  182. else{
  183. return -1;
  184. //printf("bitDiffuse2 error\r\n");
  185. }
  186. }
  187. }
  188. //printf("bitDiffuse2 ok\r\n");
  189. break;
  190. case 1:
  191. for(i=0;i<16;i++)
  192. {
  193. for(j=0;j<8;j++)
  194. {
  195. if ( ( (License[i*8+j] >> 1) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  196. {
  197. //printf("run\r\n");
  198. }
  199. else{
  200. return -1;
  201. //printf("bitDiffuse2 error\r\n");
  202. }
  203. }
  204. }
  205. //printf("bitDiffuse2 ok\r\n");
  206. break;
  207. case 2:
  208. for(i=0;i<16;i++)
  209. {
  210. for(j=0;j<8;j++)
  211. {
  212. if ( ( (License[i*8+j] >> 2) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  213. {
  214. //printf("run\r\n");
  215. }
  216. else{
  217. return -1;
  218. //printf("bitDiffuse2 error\r\n");
  219. }
  220. }
  221. }
  222. //printf("bitDiffuse2 ok\r\n");
  223. break;
  224. case 3:
  225. for(i=0;i<16;i++)
  226. {
  227. for(j=0;j<8;j++)
  228. {
  229. if ( ( (License[i*8+j] >> 3) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  230. {
  231. //printf("run\r\n");
  232. }
  233. else{
  234. return -1;
  235. //printf("bitDiffuse2 error\r\n");
  236. }
  237. }
  238. }
  239. //printf("bitDiffuse2 ok\r\n");
  240. break;
  241. case 4:
  242. for(i=0;i<16;i++)
  243. {
  244. for(j=0;j<8;j++)
  245. {
  246. if ( ( (License[i*8+j] >> 4) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  247. {
  248. //printf("run\r\n");
  249. }
  250. else{
  251. return -1;
  252. //printf("bitDiffuse2 error\r\n");
  253. }
  254. }
  255. }
  256. //printf("bitDiffuse2 ok\r\n");
  257. break;
  258. case 5:
  259. for(i=0;i<16;i++)
  260. {
  261. for(j=0;j<8;j++)
  262. {
  263. if ( ( (License[i*8+j] >> 5) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  264. {
  265. //printf("run\r\n");
  266. }
  267. else{
  268. return -1;
  269. //printf("bitDiffuse2 error\r\n");
  270. }
  271. }
  272. }
  273. //printf("bitDiffuse2 ok\r\n");
  274. break;
  275. case 6:
  276. for(i=0;i<16;i++)
  277. {
  278. for(j=0;j<8;j++)
  279. {
  280. if ( ( (License[i*8+j] >> 6) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  281. {
  282. //printf("run\r\n");
  283. }
  284. else{
  285. return -1;
  286. //printf("bitDiffuse2 error\r\n");
  287. }
  288. }
  289. }
  290. //printf("bitDiffuse2 ok\r\n");
  291. break;
  292. case 7:
  293. for(i=0;i<16;i++)
  294. {
  295. for(j=0;j<8;j++)
  296. {
  297. if ( ( (License[i*8+j] >> 7) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
  298. {
  299. //printf("run\r\n");
  300. }
  301. else{
  302. return -1;
  303. //printf("bitDiffuse2 error\r\n");
  304. }
  305. }
  306. }
  307. //printf("bitDiffuse2 ok\r\n");
  308. break;
  309. default:
  310. //printf("´íÎóÎÞЧµÄÀ©É¢Î»Ñ¡Ôñ\r\n");
  311. return -1;
  312. }
  313. return 1;
  314. }
  315. int8_t ExchangeCharToByte(uint8_t *LicenseKeySrc256, uint8_t *LicenseKeyDst128)
  316. {
  317. uint8_t i, index;
  318. uint8_t tmp;
  319. uint8_t tmp2;
  320. for(i=0;i<128;i++)
  321. {
  322. index = i*2;
  323. if(LicenseKeySrc256[index] >= '0' && LicenseKeySrc256[index] <= '9')
  324. {
  325. tmp = LicenseKeySrc256[index] - '0';
  326. }
  327. else if(LicenseKeySrc256[index] >= 'A' && LicenseKeySrc256[index] <= 'F')
  328. {
  329. tmp = LicenseKeySrc256[index] - 'A'+10;
  330. }
  331. else if(LicenseKeySrc256[index] >= 'a' && LicenseKeySrc256[index] <= 'f')
  332. {
  333. tmp = LicenseKeySrc256[index] - 'a'+10;
  334. }
  335. else
  336. {
  337. return -1;
  338. //printf("error\r\n");
  339. }
  340. if(LicenseKeySrc256[index+1] >= '0' && LicenseKeySrc256[index+1] <= '9')
  341. {
  342. tmp2 = LicenseKeySrc256[index+1] - '0';
  343. }
  344. else if(LicenseKeySrc256[index+1] >= 'A' && LicenseKeySrc256[index+1] <= 'F')
  345. {
  346. tmp2 = LicenseKeySrc256[index+1] - 'A'+10;
  347. }
  348. else if(LicenseKeySrc256[index+1] >= 'a' && LicenseKeySrc256[index+1] <= 'f')
  349. {
  350. tmp2 = LicenseKeySrc256[index+1] - 'a'+10;
  351. }
  352. else
  353. {
  354. return -1;
  355. //printf("error\r\n");
  356. }
  357. LicenseKeyDst128[i] = ((tmp&0x0f) << 4) | (tmp2&0x0f);
  358. }
  359. return 1;
  360. }
  361. int CheckLicense(uint8_t *guid, uint8_t *license)
  362. {
  363. uint32_t tmp = 0;
  364. uint8_t CalLicense[16] = {0}; //计算得出的有效的16字节License
  365. MD5_CTX md5;
  366. int i;
  367. //根据GUID计算License
  368. preHandle((uint32_t*)guid, 0x33445566, 2); //预处理
  369. MD5_Init(&md5);
  370. MD5_Update(&md5,guid,12);
  371. MD5_Final(CalLicense, &md5);
  372. //比对License
  373. if(Authorize(CalLicense, license, 0) == 1) //授权通过
  374. {
  375. printf("License check Success!\n");
  376. return 0;
  377. }
  378. else //授权不通过
  379. {
  380. printf("License is invalid, Please re input! \n");
  381. return -1;
  382. }
  383. }