编解码算法工具bse.exe
本帖最后由 happysxyf 于 2017-1-18 20:35 编辑强悍的可见流编解码工具bse.exe,比certutil快3倍的编码速度。支持9种编码格式,支持6种解码格式。由于汇编不是太熟,用C语言实现的只能每秒编码600M到1300M,我觉得已经够用了,因为机械硬盘每秒才读写50M,根本写不过来。
使用:
bse [-e|-e#|-e+|-eb|-ex|...-d|-d#|-d+|-db|-dx|...] [输入文件] [输出文件]
__________________________________________________________________________________
选项:
-h 帮助信息
-e 编码为 BASE64
-e# 编码为 BASE64#
-e+ 编码为 BASE64+
-eb 编码为 BIN码
-ex 编码为 HEX码
-e92编码为 BASE92
-d BASE64 解码
-d# BASE64#解码
-d+ BASE64+解码
-db BIN码解码
-dx HEX码解码
-d92BASE92 解码
-m 制作标准 BASE64 批处理脚本
-mp 制作压缩 BASE64 加权批处理脚本
-md 制作兼容 BASE64 过滤论坛特殊词脚本
__________________________________________________________________________________
示例:
bse -e a.jpg a.base64 //将图片a.jpg编码为 BASE64
bse -e# a.jpg a.base64# //将图片a.jpg编码为 BASE64#
bse -e92 a.jpg a.base92 //将图片a.jpg编码为 BASE92
bse -d a.base64 a.jpg //将a.base64 解编码为 a.jpg
bse -d# a.base64# a.jpg //将a.base64#解编码为 a.jpg
bse -d92 a.base92 a.jpg //将a.base92 解编码为 a.jpg
bse -m a.jpg a.bat //将图片编码为“标准BASE64编码”批处理
bse -mp a.jpg a.bat //将图片编码为“压缩BASE64加权”批处理
bse -md a.jpg a.bat //将图片编码为“过滤论坛特殊词”批处理
...
__________________________________________________________________________________
英译:
BSE.EXE
>>>-------------------------------------------------------------------------------
COPYRIGHT@2016~2018 BY HAPPY,VERSION 1.1
bse [-e|-e#|-e+|-eb|-ex|-e92|
-d|-d#|-d+|-dx|-db|-d92|
-m|-mp|-md ]
----------------------------------------------------------------------------------
-h Show help information
-e Encode file to BASE64 code
-e# Encode file to BASE64# code
-e+ Encode file to BASE64+ code
-eb Encode file to BIN code
-ex Encode file to HEX code
-e92Encode file to BASE92 code
-d Decode a file from BASE64 code
-d# Decode a file from BASE64# code
-d+ Decode a file from BASE64+ code
-db Decode a file from BIN code
-dx Decode a file from HEX code
-d92Decode a file from BASE92 code
-m Make a ordinary BASE64 batch
-mp Make a press BASE64 batch
-md Make a discuz BASE64 batch
----------------------------------------------------------------------------------
bse.c源码(采取POS开源协议,即发布的每个作品都开源):
/*
COPYRIGHT@2016~2018 BY HAPPY
BSE.EXE
VERSION 1.1
*/
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
//编码限制(单位:M)
#define FILE_MAX_SIZE 128
//BASE64加权压缩行长(单位:字节)
#define PRESS_LINE_SIZE 1000
//设置过滤敏感词数目
#define SENSITIVE_NUM 3
//添加敏感词条目(请用小写定义),过滤时不区分大小写。
static const char* SENSITIVE_WORDS[]={"**", "**", "taidu", "zangdu", "qingzhen", "fenlie", "dfj", "hsd", "xjzz"};
//BSE编码表
static const unsigned char BASE64_CODE="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const unsigned char BASE92_CODE={33,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
static const charHEX_CODE="0123456789ABCDEF";
static const char* BIN_CODE={"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
static const charBASE64_PRESS_CODE="@-#$_}{][A";
//BSE解码表
static const unsigned char BASE64_DECO={0x3E,0x40,0x40,0x40,0x3F,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x40,0x40,0x40,0x40,0x40,0x40,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33};
static const unsigned char BASE92_DECO={255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,0,255,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,255,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
static const unsigned char HEX_DECO={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
//全局变量
int i=0, j=0, FLAG=0, fsize=0;
//敏感词过滤
int Check_SensitiveWords(unsigned char* Str, int position)
{
intn, SN, MARK, L;
for(SN=0; SN<32; SN++){
if(SN>=SENSITIVE_NUM){return 0;}
L=strlen(SENSITIVE_WORDS), MARK=1;
for(n=0; n<L; n++){
if(Str!=SENSITIVE_WORDS && Str+32!=SENSITIVE_WORDS){
MARK=0;break;
}
}
if(MARK==1){return 1;}
}
return 0;
}
/***************编码函数群***************/
//BASE64加权压缩
int Press_Base64(unsigned char* Str, int L, FILE* stream, char* file)
{
int k, N, M, MARK;
char* fname;
char* extension_name;
char* press=(char*)calloc(strlen(Str)+3, sizeof(char));
for(k=0,i=0,j=0; j<L ;j++){
if(Str=='A' && j<L-1 && FLAG!=22){
i++;
}else{
if(i!=0){
while(i>512){
i-=512;
press=BASE64_PRESS_CODE;
}
M=512;
for(N=0; N<10; N++){
if(i>=M){
press=BASE64_PRESS_CODE;
i-=M;
}
M>>=1;
}
}
if(Str=='\0'){break;}else{press=Str;}
if(Check_SensitiveWords(Str, j)){
if(FLAG==22){press=' ';}else{press='.';}
}
}
}
fname=strtok(file, ".");
extension_name=strtok(NULL, ".");
if(FLAG==22){
//生成论坛专用码
int N, MARK=j/64+1;
fprintf(stream
, "@echo off\r\n"
"::*********BASE64 过滤解码器*********\r\n"
"certutil -decode \"%%~f0\" %s.%s&pause&exit /b\r\n"
"::***********************************\r\n"
,
fname,
extension_name
);
fprintf(stream, "\r\n-----BEGIN BASE64-----\r\n");
fwrite(press, k, 1, stream);
fprintf(stream, "\r\n-----END BASE64-----\r\n");
return 0;
}
//生成加权压缩码
MARK=(int)(k/PRESS_LINE_SIZE+1);
fprintf(stream
, "@echo off\r\n"
"setlocal enabledelayedexpansion\r\n\r\n"
"::*********BASE64 加权解码器*********\r\n"
"set $=set [#]&CALL :BASE64_PRESS&set [$]=A&((for %%%%Z in ([,],{,},_,$,#,-,@) do (set [$]=![$]!![$]!&for %%%%S in (![$]!) do (for /l %%%%i in (1,1,%d) do (!$!%%%%i=![#]%%%%i:%%%%Z=%%%%S!))))&for /l %%%%i in (1,1,%d) do (set/p=![#]%%%%i:.=!<NUL))>%s.BSEP&certutil -decode %s.BSEP %s.%s&pause&exit /b\r\n"
"::***********************************\r\n\r\n"
":BASE64_PRESS\r\n"
,
MARK,
MARK,
fname,
fname,
fname,
extension_name
);
for(N=1; N<=MARK; N++){
fprintf(stream, "\r\n!$!%d=", N);
if(N!=MARK){
fwrite(press, PRESS_LINE_SIZE, 1, stream);
press+=PRESS_LINE_SIZE;
}else{
fwrite(press, k%PRESS_LINE_SIZE, 1, stream);
}
}
fprintf(stream, "\r\ngoto :EOF");
return 0;
}
//BASE64编码
int EncodeBase64(FILE* fp, FILE* stream, char* file)
{
unsigned char* RAMread=(unsigned char*)calloc( fsize+3, sizeof(unsigned char));
unsigned char* buf=(unsigned char*)calloc(fsize*4/3+2, sizeof(unsigned char));
fread(RAMread, fsize, 1, fp);fclose(fp);
while(i<fsize){
buf=BASE64_CODE[ (RAMread>>2) ];
buf=BASE64_CODE[ (RAMread&0x03)<<4|(RAMread[++i]>>4) ];
buf=BASE64_CODE[ (RAMread&0x0F)<<2|(RAMread[++i]>>6) ];
buf=BASE64_CODE[ (RAMread&0x3F) ];
}
if(i==fsize+2){buf='=', buf='=';}
if(i==fsize+1){ buf='=';}
free(RAMread);
if(FLAG==20){
//生成兼容性BASE64解码脚本
int N, MARK=j/64+1;
char* fname=strtok(file, ".");
char* extension_name=strtok(NULL, ".");
fprintf(stream
, "@echo off\r\n"
"::*********BASE64 标准解码器*********\r\n"
"certutil -decode \"%%~f0\" %s.%s&pause&exit /b\r\n"
"::***********************************\r\n"
,
fname,
extension_name
);
fprintf(stream, "\r\n-----BEGIN BASE64-----");
for(N=1; N<=MARK; N++){
fprintf(stream, "\r\n");
if(N!=MARK){
fwrite(buf, 64, 1, stream);
buf+=64;
}else{
fwrite(buf, j%64, 1, stream);
}
}
fprintf(stream, "\r\n-----END BASE64-----");
return 0;
}else if(FLAG==21){
//制作加权压缩BASE64解码脚本
Press_Base64(buf, j, stream, file);
}else if(FLAG==22){
//制作DISCUZ论坛专用解码脚本
Press_Base64(buf, j, stream, file);
}else {
fwrite(buf, j, 1, stream);
}
free(buf);
return 0;
}
//BASE64#编码
int EncodeBase64_Tight(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc( fsize+3, sizeof(unsigned char));
unsigned char* buf=(unsigned char*)calloc(fsize*4/3+2, sizeof(unsigned char));
fread(RAMread, fsize, 1, fp);fclose(fp);
while(i<fsize){
buf=BASE64_CODE[(RAMread&0xC0)>>2|(RAMread&0xC0)>>4|(RAMread&0xC0)>>6];
buf=BASE64_CODE[ RAMread&0x3F ];
buf=BASE64_CODE[ RAMread&0x3F ];
buf=BASE64_CODE[ RAMread&0x3F ];
}
if(i==fsize+2){buf='=', buf='=';}
if(i==fsize+1){ buf='=';}
free(RAMread);
fwrite(buf, j, 1, stream);
free(buf);
return 0;
}
//BASE64+编码
int EncodeBase64_Plus(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc( fsize+3, sizeof(unsigned char));
unsigned char*quotes=(unsigned char*)calloc(fsize/3+2, sizeof(unsigned char));
unsigned char* buf=(unsigned char*)calloc(fsize*4/3+2, sizeof(unsigned char));
fread(RAMread, fsize, 1, fp);fclose(fp);
while(i<fsize){
quotes=BASE64_CODE[(RAMread&0xC0)>>2|(RAMread&0xC0)>>4|(RAMread&0xC0)>>6];
buf=BASE64_CODE[ RAMread&0x3F ];
buf=BASE64_CODE[ RAMread&0x3F ];
buf=BASE64_CODE[ RAMread&0x3F ];
}
if(i==fsize+2){buf='=', buf='=';}
if(i==fsize+1){ buf='=';}
free(RAMread);
fwrite(buf, i, 1, stream);
fputs("#", stream);
fwrite(quotes, i/3+1, 1, stream);
free(buf);free(quotes);
return 0;
}
//BASEBIN编码
int EncodeBin(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc(fsize+1, sizeof(unsigned char));
char* buf;
fread(RAMread, fsize, 1, fp);fclose(fp);
while(i!=fsize){
buf=(char*)BIN_CODE[ RAMread>>4 ];
buf=(char*)BIN_CODE[ RAMread&0x0F ];
fprintf(stream, "%s%s", buf, buf);
}
free(RAMread);
return 0;
}
//BASEHEX编码
int EncodeHex(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc(fsize+1, sizeof(unsigned char));
unsigned char* buf=(unsigned char*)calloc(fsize*2+1, sizeof(unsigned char));
fread(RAMread, fsize, 1, fp);fclose(fp);
while(i!=fsize){
buf=HEX_CODE[ RAMread>>4 ];
buf=HEX_CODE[ RAMread&0x0F ];
}
free(RAMread);
fwrite(buf, j, 1, stream);
free(buf);
return 0;
}
//BASE92编码
int Str_Encode(unsigned char* str, FILE* stream)
{
unsigned int sizes;
unsigned long workspace;
unsigned short wssize;
int tmp, len=fsize;
unsigned char c;
unsigned char *res;
sizes=(len*8)%13;
if(sizes==0){
sizes=2*((len*8)/13);
} else if(sizes<7){
sizes=2*((len*8)/13)+1;
} else {
sizes=2*((len*8)/13)+2;
}
res=(unsigned char*)malloc(sizeof(char)*(sizes+1));
workspace=0;
wssize=0;
j=0;
for(i=0; i<len; i++){
workspace=workspace<<8 | str;
wssize+=8;
if(wssize>=13){
tmp=(workspace>>(wssize-13))&8191;
c=BASE92_CODE;
if(c==0){
free(res);
return 1;
}
res=c;
c=BASE92_CODE;
if(c==0){
free(res);
return 1;
}
res=c;
wssize -=13;
}
}
if(0<wssize && wssize<7){
tmp=(workspace<<(6-wssize))&63;
c=BASE92_CODE;
if(c==0){
free(res);
return 1;
}
res=c;
} else if(7<=wssize){
tmp=(workspace<<(13-wssize))&8191;
c=BASE92_CODE;
if(c==0){
free(res);
return 1;
}
res=c;
c=BASE92_CODE;
if(c==0){
free(res);
return 1;
}
res=c;
}
res=0;
fwrite(res, sizes, 1, stream);
return 0;
}
int EncodeBase92(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc(fsize, sizeof(unsigned char));
fread(RAMread, fsize, 1, fp);fclose(fp);
Str_Encode(RAMread, stream);
free(RAMread);
return 0;
}
/***************解码函数群***************/
//BASE64解码
int DecodeBase64(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc( fsize+2, sizeof(unsigned char));
unsigned char* buf=(unsigned char*)calloc(fsize*3/4+3, sizeof(unsigned char));
fread(RAMread, fsize, 1, fp);fclose(fp);
for(; i<fsize; i++){
buf=BASE64_DECO-43]<<2 | BASE64_DECO-43]>>4;
buf=BASE64_DECO-43]<<4 | BASE64_DECO-43]>>2;
buf=BASE64_DECO-43]<<6 | BASE64_DECO-43] ;
}
if(RAMread[--i]=='='){j--;}
if(RAMread[--i]=='='){j--;}
free(RAMread);
fwrite(buf, j, 1, stream);
free(buf);
return 0;
}
//BASE64#解码
int DecodeBase64_Tight(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc( fsize+2, sizeof(unsigned char));
unsigned char* buf=(unsigned char*)calloc(fsize*3/4+3, sizeof(unsigned char));
unsigned char pre;
fread(RAMread, fsize, 1, fp);fclose(fp);
while(i<fsize){
pre=BASE64_DECO-43];
buf=(pre&0x30)<<2 | BASE64_DECO-43];
buf=(pre&0x0C)<<4 | BASE64_DECO-43];
buf=(pre&0x03)<<6 | BASE64_DECO-43];
}
if(RAMread[--i]=='='){j--;}
if(RAMread[--i]=='='){j--;}
free(RAMread);
fwrite(buf, j, 1, stream);
free(buf);
return 0;
}
//BASE64+解码
int DecodeBase64_Plus(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc( fsize+3, sizeof(unsigned char));
unsigned char* buf=(unsigned char*)calloc(fsize*3/4+3, sizeof(unsigned char));
unsigned char pre;
fread(RAMread, fsize, 1, fp);fclose(fp);
int p=(int)strchr(RAMread, '#')-(int)RAMread;
while(i<p){
pre=BASE64_DECO-43];
buf=(pre&0x30)<<2 | BASE64_DECO-43];if(i==p){break;}
buf=(pre&0x0C)<<4 | BASE64_DECO-43];if(i==p){break;}
buf=(pre&0x03)<<6 | BASE64_DECO-43];
}
if(RAMread=='='){i--;}
if(RAMread=='='){i--;}
free(RAMread);
fwrite(buf, i, 1, stream);
free(buf);
return 0;
}
//BASEBIN解码
int DecodeBin(FILE* fp, FILE* stream)
{
int M;
unsigned char* RAMread=(unsigned char*)calloc(fsize+2,sizeof(unsigned char));
unsigned char S;
fread(RAMread, fsize, 1, fp);fclose(fp);
while(i<fsize){
S=0,M=128;
for(j=0; j<8; j++){
if(RAMread==49){
S+=M;
}
M>>=1;
}
fprintf(stream, "%c", S);
}
free(RAMread);
return 0;
}
//BASEHEX解码
int DecodeHex(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc(fsize+2, sizeof(unsigned char));
unsigned char* buf=(unsigned char*)calloc(fsize/2+1, sizeof(unsigned char));
fread(RAMread, fsize, 1, fp);fclose(fp);
for(; i!=fsize; i++){
buf=HEX_DECO-48]<<4|HEX_DECO-48];
}
free(RAMread);
fwrite(buf, j, 1, stream);
free(buf);
return 0;
}
//BASE92解码
int Str_Decode(unsigned char* str, FILE* stream)
{
int b1, b2, len;
int sizes;
unsigned char* res;
unsigned long workspace;
unsigned short wssize;
sizes=strlen(str);
if(strcmp(str, "~")==0 || sizes==0){
res=(unsigned char*)malloc(sizeof(char)*1);
res=0;
return 1;
}
if(sizes<2){
res=(unsigned char*)1;
}
len=((sizes/2*13)+(sizes%2*6))/8;
res=(unsigned char *)malloc(sizeof(char)*(len));
workspace=0;
wssize=0;
j=0;
for(i=0; i+1<sizes; i+=2){
b1=BASE92_DECO];
b2=BASE92_DECO];
workspace=(workspace<<13)|(b1*91+b2);
wssize+=13;
while(wssize>=8){
res=(workspace>>(wssize-8))&255;
wssize -=8;
}
}
if(sizes%2==1){
workspace=(workspace<<6)| BASE92_DECO];
wssize+=6;
while(wssize>=8){
res=(workspace>>(wssize-8))&255;
wssize -=8;
}
}
fwrite(res, len, 1, stream);
return 0;
}
int DecodeBase92(FILE* fp, FILE* stream)
{
unsigned char* RAMread=(unsigned char*)calloc(fsize, sizeof(unsigned char));
fread(RAMread, fsize, 1, fp);fclose(fp);
Str_Decode(RAMread, stream);
free(RAMread);
return 0;
}
/***************功能函数群***************/
//帮助信息
void Help_Information(FILE* stream, int Exit_Code)
{
fprintf(stream,
"COPYRIGHT@2016~2018 BY HAPPY, VERSION 1.1\n"
"--------------------------------------------------------------\n"
"bse [-e|-e#|-e+|-eb|-ex|-e92|\n"
" -d|-d#|-d+|-dx|-db|-d92|\n"
" -m|-mp|-md ] \n"
"--------------------------------------------------------------\n"
" -h Show help information\n"
" -e Encode file to BASE64 code\n"
" -e# Encode file to BASE64# code\n"
" -e+ Encode file to BASE64+ code\n"
" -eb Encode file to BIN code\n"
" -ex Encode file to HEX code\n"
" -e92Encode file to BASE92 code\n"
" -d Decode a file from BASE64 code\n"
" -d# Decode a file from BASE64# code\n"
" -d+ Decode a file from BASE64+ code\n"
" -db Decode a file from BIN code\n"
" -dx Decode a file from HEX code\n"
" -d92Decode a file from BASE92 code\n"
" -m Make a ordinary BASE64 batch\n"
" -mp Make a press BASE64 batch\n"
" -md Make a discuz BASE64 batch\n"
"--------------------------------------------------------------\n"
);
exit(Exit_Code);
}
/*************MAIN主函数入口*************/
int main(int argc, char** argv)
{
FILE* fp;FILE* op; char* delims;
if((argc==4) && (argv=='-')){
switch(argv){
case 'E':
case 'e':
delims=(argv+2);
if (delims=='\0'){FLAG= 1;}
else if(delims== '#'){FLAG= 2;}
else if(delims== '+'){FLAG= 3;}
else if(delims== 'b'){FLAG= 4;}
else if(delims== 'x'){FLAG= 5;}
else if(delims== '9'){FLAG= 9;}
else{Help_Information(stderr, 1);}
break;
case 'D':
case 'd':
delims=(argv+2);
if (delims=='\0'){FLAG=11;}
else if(delims== '#'){FLAG=12;}
else if(delims== '+'){FLAG=13;}
else if(delims== 'b'){FLAG=14;}
else if(delims== 'x'){FLAG=15;}
else if(delims== '9'){FLAG=19;}
else{Help_Information(stderr, 1);}
break;
case 'M':
case 'm':
delims=(argv+2);
if (delims=='\0'){FLAG=20;}
else if(delims== 'p'){FLAG=21;}
else if(delims== 'd'){FLAG=22;}
break;
default:
Help_Information(stderr, 2);
}
}else{
Help_Information(stderr, 3);
}
//读文件流
if( (fp=fopen(argv, "rb"))==NULL ){
fputs("Failed to read file.", stdout);
return 2;
}
//测量尺寸
fseek(fp, 0, SEEK_END);
if( (fsize=ftell(fp))>FILE_MAX_SIZE*1024*1024 ){
fputs("File size is too large, out of memory.", stdout);
return 1;
}
//指针复原
fseek(fp, 0, SEEK_SET);
//打开输出文件流
if( (op=fopen(argv, "wb"))==NULL ){
fputs("Failed to read file.", stdout);
return 1;
}
switch(FLAG){
case 1:
EncodeBase64(fp, op, NULL);
break;
case 2:
EncodeBase64_Tight(fp, op);
break;
case 3:
EncodeBase64_Plus(fp, op);
break;
case 4:
EncodeBin(fp, op);
break;
case 5:
EncodeHex(fp, op);
break;
case 9:
EncodeBase92(fp, op);
break;
case 11:
DecodeBase64(fp, op);
break;
case 12:
DecodeBase64_Tight(fp, op);
break;
case 13:
DecodeBase64_Plus(fp, op);
break;
case 14:
DecodeBin(fp, op);
break;
case 15:
DecodeHex(fp, op);
break;
case 19:
DecodeBase92(fp, op);
break;
case 20:
case 21:
case 22:
EncodeBase64(fp, op, argv);
break;
}
fclose(op);
return 0;
}
其中base64+和base64#为自创编码算法,其原理如下:
我根据Base64的构造方式,改良了一种新的编码。这种编码方式与base64具有一样的编码增长率,只是编码的方式略有区别,是一种模拟的base256码,只是可见字符无法达到256种,因此为可见字符构建了引码。引码的取值为0x00、0x01、0x10、0x11,由4个引码与64个可见字符组合成256种不同情况,即组合为一个字节。在编码时,将8位的前两位单独取出作为引码,余下6位直接转化为数据码,同时每3个引码(也就是凑够6位)合成为一个base64+码。这样引码和数据码都转化为了base+码。
批处理原型:
@echo off
::**************Happy's Base64+**********
REM 使用说明:
REM Base64+编码,由“引码”+“数据”构成
REM “引码”是数据的解密引文,安全性极高
REM “引码”和“数据”中间用“+”号 隔开
::**************编码测试*****************
set "原文=589E583EC18895DF88B450831DB8975FC8B0031F68B003D910000C077433D8D0000C0725BBE01000000C704240800000031D289542404E89C0D000083F801747A85C0740EC7042408000000FFD0BBFFFFFFFF89D88B75FC8B5DF889EC5DC204003D940000C074C2774A3D930000C074B489D88B75FC8B5DF889EC5DC20400903D050000C0745B3D1D0000C075C5C704240400000031F689742404E8380D000083F801746A85C074AAC7042404000000FFD0EB9A3D960000C0EBD1C7042408000000B80100000089442404E8080D000085F60F8476FFFFFFE88B090000E96CFFFFFFC704240B00000031C089442404E8E40C000083F801743085C00F8452FFFFFFC704240B000000FFD0E93FFFFFFFC7042404000000B901000000894C2404E8B40C0000E925FFFFFFC704240B000000B80100000089442404E89A0C0000E90BFFFFFF8DB6000000008DBC27000000005589E55383EC24C7042400104000E8050D000083EC04E8FD070000E8F8080000C745F8000000008D45F889442410A104204000C70424044040008944240C8D45F489442408B80040400089442404E84D0C0000A11040400085C07464A3142040008B15F450400085D20F85A100000083FAE0741FA11040400089442404A1F45040008B4030890424E80B0C00008B15F450400083FAC07428A11040400089442404A1F45040008B4050890424E8E70B0000EB0D90909090909090909090909090E8CB0B00008B15142040008910E81607000083E4F0E8EE060000E8A10B00008B0089442408A10040400089442404A104404000890424E89E03000089C3E8760B0000891C24E8060C00008DB60000000089442404A1F45040008B4010890424E8740B00008B15F4504000E940FFFFFF905589E583EC08C7042401000000FF15E8504000E8B8FEFFFF908DB426000000005589E583EC08C7042402000000FF15E8504000E898FEFFFF908DB42600000000558B0D0451400089E55DFFE18D742600558B0DF850400089E55DFFE1909090905589E55DE93F080000909090909090905589E58B4508C700000000008B4508C64004005DC35589E583EC088B45088845FF807DFF3F7609C745F83D000000EB110FB645FF8B15002040000FBE04028945F88B45F8C9C35589E583EC1C8B45188945FC8B450C8945F88B450C0345108945F48B45080FB640048845F38B45088B008945E8837DE8010F8483000000837DE801720F837DE8020F84E5000000E9690100008B45F83B45F475238B55080FB645F38842048B4508C700000000008B55FC8B451829D08945ECE94B0100008B45F889C28D45F8FF000FB6028845F20FB645F225FC000000C1F8028845F30FB645F3890424E82DFFFFFF88C28B451888108D4518FF000FB645F22403C0E0048845F38B45F83B45F475238B55080FB645F3884"
::**************Base64+码表**************
set "CODE_SKIN=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]
::**************Base64+编码器************
setlocal enabledelayedexpansion
set "LINE=!原文!000000"
for /l %%j in (0,1,8000) do (
set "STR=!LINE:~0,6!"
set/a "A0=(0x!STR:~0,2!)&0x3F, A1=(0x!STR:~2,2!)&0x3F, A2=(0x!STR:~4,2!)&0x3F, N0=(0x!STR:~0,2!>>6)&0x3, N1=(0x!STR:~2,2!>>6)&0x3, N2=(0x!STR:~4,2!>>6)&0x3, N=N0*16+N1*4+N2" 2>NUL
for /f "tokens=1,2,3,4" %%a in ("!A0! !A1! !A2! !N!") do (
set "OUT=!OUT!!CODE_SKIN:~%%a,1!!CODE_SKIN:~%%b,1!!CODE_SKIN:~%%c,1!"
set "QU=!QU!!CODE_SKIN:~%%d,1!"
)
set "LINE=!LINE:~6!"
if "!LINE:0=!"=="" (
echo Base64+密文:
set/p=!QU!+!OUT!&exit
)
)
@echo off
::**************Happy's Base64+**********
REM 使用说明:
REM Base64+编码,由“引码”+“数据”构成
REM “引码”是数据的解密引文,安全性极高
REM “引码”和“数据”中间用“+”号 隔开
::**************解码测试*****************
set "密文=ZOumKeBjAE8AsAVgAlbwIZHVgMvqC]1YAJWcCETT9WAy]1UCALWcJ]VgIAlawr]QD0AZDVQCcDbQqBYA2lBAVQJfWWgCVvAAEUQJQE0AIcAJUQIJawlAIYAJUQIJZgsAAAAqCUCJQEtCIIlgAlRAlsCJgtnDQCVECCWYJQI]GTlQDYCvzUAZOVANgK]NQBtAnzUbgJ8AGe4AABmZACZExk6ac]ZbAjXkA9JpoZPmnnmnElm0ZJomONADgzjUCgJpUm1IZkAJ5onoCaNs0k1HAIk1Jb]iaNs1EBkmlSbUg+YeYAXHocHAswCAAAAP9L]]]]4dI3fI1fIeFcgADZAAMHMn0jZwAMHLIdI3fI1fIeFcgAJDQQAMHFzRQAMHccwCAAAADfoXCAODAQAI]AXGocHKswCAAAAP9O5jZgAMO9cwCAAAALAQAAIUCAOAAQAIfg4Hv]]]8wCAwAADcIUCAOOAAAI]AXDIcA4Fv]]8wCAwAAP9OT]]]8wCAAAALQQAAIUCAOLAAAOSf]]8wCAwAALAQAAIUCAOJgAAOQ]]]4bgAAAIbCwAAAFYeV4uHB6REEAIUCAKfFEAI0DIQCOAwAAIxfFEAI]sHCKREEAIUCAKfFEAI0FIQCOOwwAOwZJJJJJJJJJJJJOMwwAIxRCEAIROBgwAI]5FYeY5IbCgAAAFYeY5IbCgAAAFYwQFUAIeVf]4Mo0RIBIURPwA7kfiA8OAIEf40fD0fHS41QA7kf4EAA"
::**************Base64+构建码表**********
setlocal enabledelayedexpansion
set "CODE=-1"
set "SKIN=0123456789ABCDEF"
for %%A in (#A,#B,#C,#D,#E,#F,#G,#H,#I,#J,#K,#L,#M,#N,#O,#P,#Q,#R,#S,#T,#U,#V,#W,#X,#Y,#Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,[,]) do (
set/a "CODE+=1,UQ_%%A=CODE,SUF=CODE&0xF,CACHE=CODE>>4,PRE0=CACHE,PRE1=4+CACHE,PRE2=8+CACHE,PRE3=12+CACHE"
for %%N in (0,1,2,3) do (
for /f "tokens=1,2" %%a in ("!PRE%%N! !SUF!") do (
set "BASE%%N_%%A=!SKIN:~%%a,1!!SKIN:~%%b,1!"
)
)
if !CODE! lss 26 (
set "CASE=%%A"
set "%%A=!CASE!"
)
)
::**************Base64+解码器************
for /f "tokens=1,2 delims=+" %%a in ("!密文!") do (
set "QU=%%a"
set LINE=%%b###"
)
for /l %%j in (0,1,1000) do (
set "PRE=!QU:~%%j,1!"
for %%Q in (!PRE!) do (
if "#%%Q"=="!#%%Q!" (
set "PRE=#!PRE!"
)
)
set "STR=!LINE:~0,3!"
set/a "N2=UQ_!PRE!&0x3,N1=(UQ_!PRE!>>2)&0x3,N0=(UQ_!PRE!>>4)&0x3"
for %%i in (0,1,2) do (
for /f "tokens=1,2" %%a in ("!N%%i! !STR:~%%i,1!") do (
if "%%b"=="#" (
echo 二进制明文:
set/p=!OUT!&exit
)
if "#%%b"=="!#%%b!" (
set "OUT=!OUT!!BASE%%a_#%%b!"
) else (
set "OUT=!OUT!!BASE%%a_%%b!"
)
)
)
set "LINE=!LINE:~3!"
)
页:
[1]