Chinaunix首页 | 论坛 | 博客
  • 博客访问: 90137
  • 博文数量: 12
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-19 08:56
文章分类

全部博文(12)

文章存档

2015年(8)

2014年(4)

我的朋友

分类: LINUX

2015-05-06 20:49:06

解密程序如下:

点击(此处)折叠或打开


  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <string>
  7. #include <string.h>

  8. #define FAILED 1
  9. #define SUCCESS 0
  10. using namespace std;

  11. const char *stampKey = "linfang2b";
  12. static string stampFile= "/etc/measure/measured.conf";
  13. void strToByte(const char *sptr,int slen,char *bptr)
  14. {
  15.     int i,j,op,tmp;
  16.     for(i=0;i<slen;i++)
  17.     {
  18.     op=0x80;
  19.     for(j=0;j<8;j++)
  20.     {
  21.      tmp=sptr[i]&op;

  22.      if(tmp == 0)

  23.         bptr[i*8+j] = 0x0;
  24.      else

  25.         bptr[i*8+j] = 0x1;
  26.      op>>=1;
  27.     }
  28.     }
  29. }

  30. void byteTostr(const char* bptr,int blen,char* sptr)
  31. {

  32.     int j,op;
  33.     op=0;
  34.     for(j=0;j<blen;j++)
  35.     {
  36.     op|=bptr[j];

  37.     if (j%8 == 7)
  38.     {
  39.      sptr[(j+1)/8-1]=op;
  40.      op=0;
  41.     }
  42.     else
  43.      op<<=1;
  44.     }
  45. }

  46. char* alignStr(const char* src,int slen)
  47. {
  48.     char* dest;
  49.     int alignLen=0,i;
  50.     if(slen%8 != 0)
  51.     alignLen = 8-slen%8;
  52.     dest = (char*)malloc((slen+alignLen+1)*sizeof(char));
  53.     strncpy(dest,src,slen);
  54.     for(i=0;i<alignLen;i++)
  55.     dest[slen+i]='$';
  56.     dest[slen+alignLen]='\0';
  57.     return dest;
  58. }

  59. int unAlignStr(char *string, int slen)
  60. {

  61.     int tlen=slen;
  62.     while(1)
  63.     {
  64.     if(string[tlen-1]=='$')
  65.      string[tlen-1]='\0';

  66.     else

  67.      return tlen;
  68.     tlen--;
  69.     }
  70. }

  71. int enCryptStamp(char* string,int slen,const char* key,int flag)
  72. {
  73.     char* ptr,*buffer,bytekey[64];
  74.     int i,buflen;
  75.     strToByte(key,8,bytekey);
  76.     setkey(bytekey);
  77.     buflen=slen*8;
  78.     buffer=(char*)malloc(buflen*sizeof(char));
  79.     if(NULL == buffer)
  80.     {
  81.     cout << "malloc failed" << endl;
  82.     return FAILED;
  83.     }
  84.     strToByte(string,slen,buffer);
  85.     for(i=0;i<buflen;i+=64)
  86.     {
  87.     ptr=&buffer[i];
  88.     encrypt(ptr,flag);
  89.     }
  90.     byteTostr(buffer,buflen,string);
  91.     free(buffer);
  92.     buffer = NULL;
  93.     return SUCCESS;
  94. }
  95. //int readFromStampConf(string &str)
  96. //{
  97. // fstream file;
  98. // file.open(stampFile.c_str(), ios::in);
  99. // if(!file)
  100. // {
  101. //    cout << "File op failed" << endl;
  102. // return FAILED;
  103. // }
  104. // file >> str;
  105. //
  106. // file.close();
  107. // return SUCCESS;
  108. //
  109. //}
  110. int readFromStampConf(char *str)
  111. {
  112.     fstream file;
  113.     file.open(stampFile.c_str(), ios::in);
  114.     if(!file)
  115.     {
  116.     cout << "File op failed" << endl;
  117.         return FAILED;
  118.     }
  119.     int len = 0;

  120.     //------存在问题,len多加一----------------
  121.     //------在file判断eof时,eof()返回true时是读到文件结束符0xFF,而文件结束符是最后一个字符的下一个字符
  122.     //------因此多加一
  123.     //while(!file.eof())
  124.     //{
  125.     // file.get(str[len++]);
  126.     //}
  127.     //----------------------------------------

  128.     //------在网上搜了一下,有人说通过file.good()方法判断,本人试过,无效!
  129.     //------也可能是使用方法有误,请指出,谢!
  130.     //while(!file.eof())
  131.     //{
  132.     // if(file.good())
  133.     // file.get(str[len++]);
  134.     //}
  135.     //----------------------------------------


  136.     //------也有人说通过file.fail()方法判断,本人试过,无效!
  137.     //------也可能是使用方法有误,请指出,谢!
  138.     //while(!file.eof())
  139.     //{
  140.     // if(file.fail())
  141.     // break;
  142.     // file.get(str[len++]);
  143.     //}
  144.     //----------------------------------------



  145.     //------通过file.peek方法判断,可行!-----
  146.     //while(file.peek() != EOF)
  147.     //{
  148.     // file.get(str[len++]);
  149.     //}
  150.     //---------------------------------------

  151.     //-----下面形式的get也有问题,导致len多加一
  152.     //while(file.get(str[len++]));
  153.     //-----------------------------------------

  154.     //-----下面方法经尝试也可行!
  155.     char c;
  156.     while(file.get(c))
  157.      str[len++] = c;


  158.     str[len] = '\0';
  159.     file.close();
  160.     return SUCCESS;

  161. }
  162. int main()
  163. {
  164.     char str[100];
  165.     //memset(str, 0, 100);
  166.     readFromStampConf(str);
  167.     
  168.     enCryptStamp((char*)str, strlen(str), stampKey, 1);
  169.     int len = unAlignStr((char*)str, strlen(str));
  170.     str[len] = '\0';
  171.     cout <<"The encrypt content is " << str<< endl;


  172.     return 0;
  173. }
加密程序如下:

点击(此处)折叠或打开

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <string>
  5. #include <iostream>
  6. #include <fstream>
  7. using namespace std;
  8. #define SUCCESS 0
  9. #define FAILED 1
  10. static string stampFile= "/etc/measure/measured.conf";
  11. const char *stampKey = "linfang2b";
  12. void strToByte(const char *sptr,int slen,char *bptr)
  13. {

  14.     int i,j,op,tmp;
  15.     for(i=0;i<slen;i++)
  16.     {
  17.     op=0x80;
  18.     for(j=0;j<8;j++)
  19.     {
  20.      tmp=sptr[i]&op;

  21.      if(tmp == 0)

  22.         bptr[i*8+j] = 0x0;
  23.      else

  24.         bptr[i*8+j] = 0x1;
  25.      op>>=1;
  26.     }
  27.     }
  28. }

  29. void byteTostr(const char* bptr,int blen,char* sptr)
  30. {

  31.     int j,op;
  32.     op=0;
  33.     for(j=0;j<blen;j++)
  34.     {
  35.     op|=bptr[j];

  36.     if (j%8 == 7)
  37.     {
  38.      sptr[(j+1)/8-1]=op;
  39.      op=0;
  40.     }
  41.     else
  42.      op<<=1;
  43.     }
  44. }

  45. char* alignStr(const char* src,int slen)
  46. {
  47.     char* dest;
  48.     int alignLen=0,i;
  49.     if(slen%8 != 0)
  50.     alignLen = 8-slen%8;
  51.     dest = (char*)malloc((slen+alignLen+1)*sizeof(char));
  52.     strncpy(dest,src,slen);
  53.     for(i=0;i<alignLen;i++)
  54.     dest[slen+i]='$';
  55.     dest[slen+alignLen]='\0';
  56.     return dest;
  57. }

  58. int unAlignStr(char *string, int slen)
  59. {

  60.     int tlen=slen;
  61.     while(1)
  62.     {
  63.     if(string[tlen-1]=='$')
  64.      string[tlen-1]='\0';

  65.     else

  66.      return tlen;
  67.     tlen--;
  68.     }
  69. }

  70. int enCryptStamp(char* string,int slen,const char* key,int flag)
  71. {
  72.     char* ptr,*buffer,bytekey[64];
  73.     int i,buflen;
  74.     strToByte(key,8,bytekey);
  75.     setkey(bytekey);
  76.     buflen=slen*8;
  77.     buffer=(char*)malloc(buflen*sizeof(char));
  78.     if(NULL == buffer)
  79.     {
  80.      return FAILED;
  81.     }
  82.     strToByte(string,slen,buffer);
  83.     for(i=0;i<buflen;i+=64)
  84.     {
  85.     ptr=&buffer[i];
  86.     encrypt(ptr,flag);
  87.     }
  88.     byteTostr(buffer,buflen,string);
  89.     free(buffer);
  90.     buffer = NULL;
  91.     return SUCCESS;
  92. }
  93. int isValidStamp(string &stamp)
  94. {
  95.     for(unsigned int i=0; i<stamp.size(); i++)
  96.     {
  97.     if(!isdigit(stamp[i]))
  98.      return FAILED;
  99.     }
  100.     return SUCCESS;
  101. }
  102. int writeToStampConf(const char *key)
  103. {
  104.     fstream file;
  105.     file.open(stampFile.c_str(), ios::out);
  106.     if(!file)
  107.     {
  108.         return FAILED;
  109.     }

  110.     file << key;

  111.     file.close();
  112.     return SUCCESS;

  113. }
  114. int main()
  115. {
  116.     string stamp("36");
  117.     char *key = NULL;
  118.     int iRet;
  119.     key = alignStr(stamp.c_str(), stamp.size());
  120.     iRet = enCryptStamp(key, strlen(key), stampKey, 0);
  121.     if(iRet)
  122.     {
  123.      return FAILED;
  124.     }
  125.     iRet = writeToStampConf(key);
  126.     if(iRet)
  127.      return FAILED;

  128.     free(key);
  129.     key = NULL;
  130.     return SUCCESS;
  131. }

如解密程序注释部分,之前对file.eof的理解有误区,导致解密程序对len计算有问题,导致解密失败。
其实只要将memset那句注释打开,有些问题也就不存在了,因为str[100]可能有非0初值,因此当len在判断eof时多加一,碰巧那一位有值,导致解密失败。
如果在其他环境下,应该对eof进行合理的判断。

此外,encrypt函数在调用过程中需要将字符串转换为“01串”!
阅读(1627) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~