123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- #include <stdio.h>
- #include <stdint.h>
- #include <stdio.h>
- #include "md5.h"
- /*
- preHandleIndex:
- 0: |
- 1: &
- 2: ^
- 3: !^
- 4: +
- 5: -
- */
- int8_t preHandle(uint32_t *preGUIDptr, uint32_t key, uint8_t preHandleIndex)
- {
- uint8_t i;
- switch(preHandleIndex)
- {
- case 0: // |
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] |= key;
- }
- break;
- case 1: // &
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] &= key;
- }
- break;
- case 2: // ^
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] ^= key;
- }
- break;
- case 3: // !^
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] ^= key;
- preGUIDptr[i] = ~preGUIDptr[i];
- }
- break;
- case 4: // +
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] += key;
- }
- break;
- case 5: // -
- for(i=0;i<3;i++)
- {
- preGUIDptr[i] -= key;
- }
- break;
- default:
- printf("´íÎóµ±Ç°Ñ¡ÔñµÄÔËË㷽ʽ²»¿ÉÓÃ\r\n");
- return -1;
- }
- return 1;
- }
- int8_t bitDiffuse(uint8_t decrypt[16], uint8_t License[256], uint8_t bitSelectIndex)
- {
- uint8_t i, j;
- //À©É¢µ½128×Ö½Ú
- switch (bitSelectIndex) {
- case 0:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x01;
- else
- License[i*8+j] &= ~0x01;
- }
- }
- break;
- case 1:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x02;
- else
- License[i*8+j] &= ~0x02;
- }
- }
- break;
- case 2:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x04;
- else
- License[i*8+j] &= ~0x04;
- }
- }
- break;
- case 3:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x08;
- else
- License[i*8+j] &= ~0x08;
- }
- }
- break;
- case 4:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x10;
- else
- License[i*8+j] &= ~0x10;
- }
- }
- break;
- case 5:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x20;
- else
- License[i*8+j] &= ~0x20;
- }
- }
- break;
- case 6:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x40;
- else
- License[i*8+j] &= ~0x40;
- }
- }
- break;
- case 7:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if((decrypt[i]&(0x01<<j)))
- License[i*8+j] |= 0x80;
- else
- License[i*8+j] &= ~0x80;
- }
- }
- break;
- default:
- printf("´íÎóÎÞЧµÄÀ©É¢Î»Ñ¡Ôñ\r\n");
- return -1;
- }
- return 1;
- }
- int8_t Authorize(uint8_t decrypt[16], uint8_t License[128], uint8_t bitSelectIndex)
- {
- uint8_t i, j;
- switch (bitSelectIndex) {
- case 0:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( License[i*8+j] & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 1:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
-
- if ( ( (License[i*8+j] >> 1) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 2:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 2) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 3:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 3) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 4:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 4) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 5:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 5) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 6:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 6) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- case 7:
- for(i=0;i<16;i++)
- {
- for(j=0;j<8;j++)
- {
- if ( ( (License[i*8+j] >> 7) & 0x01 ) == ( (decrypt[i] >> j) & 0x01 ) )
- {
- //printf("run\r\n");
- }
- else{
- return -1;
- //printf("bitDiffuse2 error\r\n");
- }
- }
- }
- //printf("bitDiffuse2 ok\r\n");
- break;
- default:
- //printf("´íÎóÎÞЧµÄÀ©É¢Î»Ñ¡Ôñ\r\n");
- return -1;
- }
- return 1;
- }
- int8_t ExchangeCharToByte(uint8_t *LicenseKeySrc256, uint8_t *LicenseKeyDst128)
- {
- uint8_t i, index;
- uint8_t tmp;
- uint8_t tmp2;
- for(i=0;i<128;i++)
- {
- index = i*2;
- if(LicenseKeySrc256[index] >= '0' && LicenseKeySrc256[index] <= '9')
- {
- tmp = LicenseKeySrc256[index] - '0';
- }
- else if(LicenseKeySrc256[index] >= 'A' && LicenseKeySrc256[index] <= 'F')
- {
- tmp = LicenseKeySrc256[index] - 'A'+10;
- }
- else if(LicenseKeySrc256[index] >= 'a' && LicenseKeySrc256[index] <= 'f')
- {
- tmp = LicenseKeySrc256[index] - 'a'+10;
- }
- else
- {
- return -1;
- //printf("error\r\n");
- }
-
- if(LicenseKeySrc256[index+1] >= '0' && LicenseKeySrc256[index+1] <= '9')
- {
- tmp2 = LicenseKeySrc256[index+1] - '0';
- }
- else if(LicenseKeySrc256[index+1] >= 'A' && LicenseKeySrc256[index+1] <= 'F')
- {
- tmp2 = LicenseKeySrc256[index+1] - 'A'+10;
- }
- else if(LicenseKeySrc256[index+1] >= 'a' && LicenseKeySrc256[index+1] <= 'f')
- {
- tmp2 = LicenseKeySrc256[index+1] - 'a'+10;
- }
- else
- {
- return -1;
- //printf("error\r\n");
- }
- LicenseKeyDst128[i] = ((tmp&0x0f) << 4) | (tmp2&0x0f);
- }
- return 1;
- }
- int CheckLicense(uint8_t *guid, uint8_t *license)
- {
- uint32_t tmp = 0;
- uint8_t CalLicense[16] = {0}; //计算得出的有效的16字节License
- MD5_CTX md5;
- int i;
- //根据GUID计算License
- preHandle((uint32_t*)guid, 0x33445566, 2); //预处理
- MD5_Init(&md5);
- MD5_Update(&md5,guid,12);
- MD5_Final(CalLicense, &md5);
- //比对License
- if(Authorize(CalLicense, license, 0) == 1) //授权通过
- {
- printf("License check Success!\n");
- return 0;
- }
- else //授权不通过
- {
- printf("License is invalid, Please re input! \n");
- return -1;
- }
- }
|