签到

05月06日
尚未签到

共有回帖数 0

    李小主任

    等级:
    前言:百度帖吧生来就没有放上文件的功能,这一点上说,有点遗憾,因为大家无法通过它共享文件。filetxt & txtfile 就是为这一点而制作的。

    原理:filetxt 将两进制文件转化为可供发表的文本,txtfile 将文本还原为原来的两进制文件
    实现细节:filetxt将两进制文件以2字节为单位读入,转换为3字节文本并输出,输出文本以64个称为key的ASCII字符为标准(可以更换以便加密)
    三个字节表示了6*6*6种情况,其中的5*5*6用作两字节数据,剩余两个位分别作为循环标识和单字节标识(文件长度不可能都是偶数)

    关于循环:filetxt会处理所有的字循环,也即是当下一个读入的字与上一个相同时,它会统计后面相同的字数,并将这个写入的单元替之以循环次数。
    在不循环处理的情况下,被转换文件将会增容到1.5倍(处理压缩文件时道理是一样的,压缩文件内部较少相同字节),循环处理可以明显减少这个数目。
    (一个以知的错误是,如果循环次数大于65536,将导致数据错误,所以还是建议压缩一下再转换:-()

    关于KEY:filetxt & txtfile被设计为可以自定义输出密钥,目的在于,有些东西你可能须要保密,用自定义密钥的话可以实现这个功能,
    缺省的密钥是: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz{|",可以通过导入密钥文件来完成自定义,其中密钥文件必
    须符合以下标准:各不相同的(=64)个字节文件,(会自动检查合法性)。

    用法:
    filetxt 源文件.* [-o生成文件名] [-k密钥文件名]
    txtfile 源文件.txt [-o生成文件名] [-k密钥文件名]

    例:filetxt aa.exe -oCBAR.txt -kKEY.txt 以KEY.txt规则编码aa.exe生成CBAR.txt, 缺省输出文件名为data.txt
      txtfile CBAR.txt -oaa2.exe -kKEY.txt 以KEY.txt规则解码CBAR.txt生成aa2.exe, 缺省输出文件名为data.bin
       
    上例中可以看出,filetxt并没有处理源文件名,所以必须向其他人提供输出文件名。另外文件长度也是必须提供的,它可以简单判断输出的文件是否正确。

    关于txtfile: txtfile会处理干扰字符,所谓干扰字符即不在密钥范围内的字符,它通常以跳过处理(屏幕输出会告诉你跳过多少)。

    百忙抽空之作,忘对大家有用。
    另外,还存在着很多漏洞,望大家一起修正。

    无效昵称
    2005.6.30
           i[2] = (data & 0x003f);
           if (flag == 1) i[0] = i[0] | 0x0020;                /*not bytes, but repeat count*/
           if (flag == 2) i[1] = i[1] | 0x0020;                /*single byte end*/
           for (j = 0; j  3; j++)
                   ou.c[j] = key[i[j]];
           ou.c[3] = 0;
           fputs(ou.c, pf);
    }
           
    int main(int argc, char *argv[]) {
           FILE *sFile, *dFile, *kFile;
           long repcount = 0;
           long i, j;
           int rflag;
           char *oFileName = "data.txt";
           char *kFileName = NULL;
           char keyBuf[70];
           union unit ic[2];
           unsigned long fileLength = 0;
           
           if (argc == 1) {
                   fprintf(stderr, s_license, argv[0]);
                   return EXIT_SUCCESS;
           }
           else {
                   if ((sFile = fopen(argv[1], "rb")) == NULL) {
                           fprintf(stderr, "error: can't open file %sn", argv[1]);
                           return -1;
                   }
                   for (i = 2; i  argc; i++){
                        if (argv[0] == '-' || argv[0] == '/') {
                                   if (argv[1] == 'o' || argv[1] == 'O') {
                                           oFileName = argv;
                                           oFileName += 2;
                                   }
                                   if (argv[1] == 'k' || argv[1] == 'K') {
                                           kFileName = argv;
                                           kFileName += 2;
                                   }
                           }
                   }
           }
           if (argc  4)
                   fprintf(stderr, "Warnning: Too many argument detected! the result may be unexpectedn");
           /*cammand line argument deal*/        
           
           if ((dFile = fopen(oFileName, "w")) == NULL) {
                   fprintf(stderr, "error: can't create output file %sn", oFileName);
                   return -1;
           }
           if (kFileName != NULL) {
                   if ((kFile = fopen(kFileName, "r")) == NULL) {
                           fprintf(stderr, "error: can't open key-file %sn", kFileName);
                           return -1;
                   }
                   for (i = 0; i  64 && (!feof(kFile)); i++)
                           keyBuf = fgetc(kFile);                /*read the key file into keyBuf*/
                   
                   for (i = 0; i  64; i++) {
                           for (j = i + 1; j  64; j++) {
                                   if (keyBuf == keyBuf[j]) {
                                           fprintf(stderr, "error: illegal key file detected!n");
                                           return -1;
                                   }

                           }
                   }
                   
                   for (i = 0; i  64; i++)
                           key = keyBuf;
                   
                   fclose(kFile);
           }
           
           while(!feof(sFile)) {
                   fileLength++;
                   fgetc(sFile);
           }
           printf("nLength:   %ld bytesnn", --fileLength);                /*here we got the length of file*/
           
           fseek(sFile, 0, SEEK_SET);
           fread(ic[0].cc, 2, 1, sFile);                        /*read the first 2 bytes*/
           
           j = (fileLength - 2) / 2;
           if(fileLength == 1) writeunit(ic[0].ii, 2, dFile);
           if(fileLength == 2) writeunit(ic[0].ii, 0, dFile);
           else if(fileLength == 3) {
                   writeunit(ic[0].ii, 0, dFile);
                   fread(ic[0].cc, 2, 1, sFile);
                   writeunit(ic[0].ii, 2, dFile);                /*the end sign (when len % 2!=0)*/

           }
           else {
                   for(i = 0; i  j; i++) {                        /*here we control the loop in bytes*/
                           repcount = 0;                                /*but not feof function*/
                           rflag = 0;
                           writeunit(ic[0].ii, 0, dFile);
                           fread(ic[1].cc, 2, 1, sFile);
                           
                           if (ic[0].ii == ic[1].ii) {
                                   repcount = 1;
                                   for(; i  j - 1; i++) {
                                           fread(ic[1].cc, 2, 1, sFile);
                                           if (ic[0].ii == ic[1].ii)
                                                   repcount++;
                                           else break;
                                   }
                                   writeunit(repcount, 1, dFile);
                                   i++;
                                   rflag = 1;
                           }
                           ic[0].ii = ic[1].ii;
                   }
                   if(rflag != 1)
                           writeunit(ic[0].ii, 0, dFile);
                   if(fileLength % 2 != 0) {
                           fread(ic[0].cc, 2, 1, sFile);
                           writeunit(ic[0].ii, 2, dFile);
                   }
           }
           
           printf("The most situation, "txtfile" will avoid some unknown char in the data text,n
    but offering the FILE LENGTH may help others easily check out whether the filen
    is correct or not.n");
           if (kFileName != NULL) {
                   printf("nIn this file, you used the new KEY-CODE to recode the data text, so don'tn
    forget to offer the key-file to you friend.n");
           }
           fclose(sFile);
           fclose(dFile);
           return 1;
    }

           fputc(out[1], pf);
           filelen++;
           if (flag == 0) {
                   fputc(out[0], pf);
                   filelen++;
           }
    }

    char trans(char c) {
           return (char)keymap[c];
    }

    int check(char c) {
           int i;
           for(i = 0; i  64; i++) {
                   if(c == key) return 1;
           }
           return 0;
    }        
    int main(int argc, char *argv[]) {
           FILE *sFile, *dFile, *kFile;
           long i, j;
           char *oFileName = "data.bin";
           char *kFileName = NULL;
           char keyBuf[70];
           union unit ic[2];
           int tmp, cidx;
           int passcount = 0;
           if (argc == 1) {
                   fprintf(stderr, s_license, argv[0]);
                   return EXIT_SUCCESS;
           }
           else {
                   if ((sFile = fopen(argv[1], "rb")) == NULL) {
                           fprintf(stderr, "error: can't open file %sn", argv[1]);
                           return -1;
                   }
                   for (i = 2; i  argc; i++){
                          if (argv[0] == '-' || argv[0] == '/') {
                                   if (argv[1] == 'o' || argv[1] == 'O') {
                                           oFileName = argv;
                                           oFileName += 2;
                                   }
                                   if (argv[1] == 'k' || argv[1] == 'K') {
                                           kFileName = argv;
                                           kFileName += 2;
                                   }
                           }
                   }
           }
           if (argc  4)
                   fprintf(stderr, "Warnning: Too many argument detected! the result may be unexpectedn");
           /*cammand line argument deal*/        
           
           if ((dFile = fopen(oFileName, "wb")) == NULL) {


                   fprintf(stderr, "error: can't create output file %sn", oFileName);
                   return -1;
           }
           if (kFileName != NULL) {
                   if ((kFile = fopen(kFileName, "r")) == NULL) {
                           fprintf(stderr, "error: can't open key-file %sn", kFileName);
                           return -1;
                   }
                   for (i = 0; i  64 && (!feof(kFile)); i++)
                           keyBuf = fgetc(kFile);                /*read the key file into keyBuf*/
                   
                   for (i = 0; i  64; i++) {
                           for (j = i + 1; j  64; j++) {
                                   if (keyBuf == keyBuf[j]) {
                                           fprintf(stderr, "error: illegal key file detected!n");
                                           return -1;
                                   }

                        }
                   }
                   
                   for (i = 0; i  64; i++)
                           key = keyBuf;
                   
                   fclose(kFile);
           }
           
           for(i = 0; i  64; i++)
                   keymap[(int)key] = i;
           
           cidx = 0;
           while(!feof(sFile)) {
                   ic[0].cc[cidx] = fgetc(sFile);
                   if (!check(ic[0].cc[cidx])) {
                           if (feof(sFile)) break;
                           passcount++;
                           continue;
                   }
                   ic[0].cc[cidx] = trans(ic[0].cc[cidx]);
                   cidx++;
                   if(cidx == 3) {
                           cidx = 0;
                           if (ic[0].cc[0] & 0x20) {                        /*is a repeat count*/



                                   tmp = ((ic[0].cc[0] & 0x001f)  11) + ((ic[0].cc[1] & 0x001f)  6) + (ic[0].cc[2] & 0x003f);
                                   for(i = 0; i  tmp; i++)
                                           writeunit(ic[1].cc, 0, dFile);                /*zero means normal output*/
                           }
                           else if (ic[0].cc[1] & 0x20) {
                                   ic[0].cc[1] = ic[0].cc[1] & 0x1f;
                                   writeunit(ic[0].cc, 1, dFile);
                           }
                           else {
                                   writeunit(ic[0].cc, 0, dFile);
                                   ic[1].ii = ic[0].ii;
                           }
                   }
           }  
           
           printf("PASS:        %dn", passcount);
           printf("File Length: %ld bytesn", filelen);
           printf("nPlease check that your FILE LENGTH is the same as the one offered to you!n");
           fclose(sFile);
           fclose(dFile);
           return 1;
    }

    楼主 2016-03-16 12:41 回复

共有回帖数 0
  • 回 帖
  • 表情 图片 视频
  • 发表

登录直线网账号

Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号 意见反馈 | 关于直线 | 版权声明 | 会员须知