extAuth.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "extAuth.h"
  2. #include <stdio.h>
  3. #ifdef I_AM_LTE
  4. #define RAND_STR_LEN 6
  5. const unsigned char srcAuth[16]={0x17,0x0d,0x55,0x9f,0xab,0xcc,0x1c,0x8f,0x30,0x0a,0x28,0xda,0x3c,0x91,0x11,0x6d};
  6. const unsigned char randStr[RAND_STR_LEN]={'s','z','5','0','2','8'};
  7. static unsigned char codeRandStr[RAND_STR_LEN];
  8. unsigned char * extGetCodeRandStr(int *outlen){
  9. *outlen=RAND_STR_LEN;
  10. return codeRandStr;
  11. }
  12. /*随机生成一个种子
  13. offset,偏移时间*/
  14. static unsigned char extRandSeed(unsigned int offset){
  15. unsigned char seed;
  16. srand(getBaseSeconds()+offset);
  17. seed=rand()/256;
  18. return seed;
  19. }
  20. /*使用seed1,seed2对长度为srclen的srcStr进行运算加密*/
  21. static void extCodeToRandStrTmp(unsigned char seed1,unsigned char seed2,unsigned char *srcStr, int srclen){
  22. unsigned int i,j;
  23. unsigned char newSeed=seed1|seed2|0xa7;
  24. for(i=0;i<srclen;i++) srcStr[i] ^= seed1;
  25. for(i=0;i<srclen;i++) if(i%2) srcStr[i] ^= seed2;
  26. for(i=0;i<srclen;i++) if(i%2==0) srcStr[i] ^= newSeed;
  27. }
  28. /*获取随机串,地址赋予outStr输出,同时输出串长度*/
  29. unsigned char *extGetRandStr(int *outlen){
  30. unsigned char seed1,seed2;
  31. unsigned char tmp[RAND_STR_LEN];
  32. static unsigned int step=0;
  33. if(outlen<RAND_STR_LEN) return -1;
  34. memcpy(tmp, randStr, RAND_STR_LEN);
  35. seed1=extRandSeed(step);
  36. seed2=extRandSeed(0x1468+step);
  37. extCodeToRandStrTmp(seed1, seed2,tmp,RAND_STR_LEN);
  38. memcpy(codeRandStr,tmp,RAND_STR_LEN);
  39. *outlen=RAND_STR_LEN;
  40. step += 77;
  41. return codeRandStr;
  42. }
  43. #endif
  44. /*对长度为targetlen的targetString进行加密,入参还有长度为randlen的随机串*/
  45. void extCodeBuffer(unsigned char *randString, int randlen, unsigned char *targetString, int targetlen){
  46. int i,j,k;
  47. unsigned char value;
  48. //先使用随机串依次对targetString异常运行
  49. j=0;
  50. for(i=0;i<targetlen;i++){
  51. targetString[i] ^= randString[j];
  52. if(++j>=randlen) j=0;
  53. }
  54. //再对targetString的奇数索引字节,与randString的高低4位指向的srcAuth索引字节异常
  55. j=0;
  56. for(i=0;i<targetlen;i++){
  57. if(j%2) k=randStr[j]&0x0F;
  58. else k=(randStr[j]>>4)&0x0F;
  59. value=srcAuth[k];
  60. if(++j>=randlen) j=0;
  61. if(i%2) targetString[i] ^= value;
  62. }
  63. }