| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 923 人关注过本帖
标题:base64的问题
只看楼主 加入收藏
zzjking
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-7-16
收藏
 问题点数:0 回复次数:8 
base64的问题

我自己作了BASE64的编码器~

对于普通的文件都能编码和解码(我把自己的编码器都编了也能解)

但是在LINUX下的终端的A.OUT这样的文件却不能,主要是A.OUT里有好多的 @ ^这样的东东,我不明白为什么,请高人指点下~我用的是fread()函数取的流~但是却取不出来~
有兴趣的可以 在LINUX的终端里 vi a.out看下~
主要问题是怎么把里面的 全部东西取出来~

[此贴子已经被作者于2007-7-16 13:06:40编辑过]

搜索更多相关主题的帖子: LINUX 编码器 OUT 终端 兴趣 
2007-07-16 12:05
酒肉弥勒佛
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:399
专家分:0
注 册:2006-6-6
收藏
得分:0 
a.out时可执行文件,相当于exe,你想把他反编译,好像不可能,除非你在编译的时候用上shell

编程是为了提高效率,好的程序是因为他的高效;在编程的道路上,永远追逐高效的算法
2007-07-16 12:59
zzjking
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-7-16
收藏
得分:0 
谢谢 你的提醒~
我想知道 VI A.OUT完了 看到的那些算什么?感觉不是字符~、
可是文件里不放字符 放的是啥呢~
而且 vi 本身就是转化为2进制的。。为什么他的好用 我自己做的东西就不能呢。。奇怪~

2007-07-16 13:05
酒肉弥勒佛
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:399
专家分:0
注 册:2006-6-6
收藏
得分:0 

不是二进制,如果你用gedit打开,他就会提示你选择正确的的编码,应该是其他什么编码,他不能识别,很多的可执行文件都不能用vi打开,有些能打开想firefox这类是因为他用shell脚本编译的,而不是gcc


编程是为了提高效率,好的程序是因为他的高效;在编程的道路上,永远追逐高效的算法
2007-07-16 14:29
zzjking
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-7-16
收藏
得分:0 
回复:(zzjking)base64的问题

终于做出来了~问题主要出现在 这里
char()和函数toascii()这2个东西不是一样的~
感觉toascii只能用在处理文本文件的转化~还是强制转化比较好~


2007-07-18 10:20
zzjking
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-7-16
收藏
得分:0 
回复:(zzjking)base64的问题

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<string.h>

unsigned char old_char[1024*1024];
int three_char[24];
unsigned char four_char[1024*1024];
unsigned char old_64_char[1024*1024];
unsigned char three_char2[1024*1024];
int four_char2[24];

void get_char1(unsigned char old_char[],int &move,char* ar,int &math)
{
unsigned char* p;
unsigned char* q;
int i=0;
unsigned char test[1024*1024];
FILE* fp;

fp=fopen(ar,"rb");
while(!feof(fp))
{
fread(test,1,1,fp);
i++;
}
math=i-1;
printf("math=%d\n",math);
fseek(fp,0,SEEK_SET);
fread(old_char,math,1,fp);
fclose(fp);
fp=fopen("test2.cfg","wb");
fwrite(old_char,math,1,fp);
fclose(fp);
p=old_char;
/* while(*p!='\0')
{
i++;
p++;
}*/


if(math%3==2)
{
old_char[math]=0;
move=1;
}
if(math%3==1)
{
old_char[math]=0;
old_char[math+1]=0;
move=2;
}
printf("move=%d\n",move);
}
void change_into_21(unsigned char &x,unsigned char &y,unsigned char &z,int three_char[])
{
int i;
int k=1;

for(i=7;i>=0;i--)
{
three_char[i]=(x&k)/k;
k=k<<1;

}
k=1;
for(i=15;i>=8;i--)
{
three_char[i]=(y&k)/k;
k=k<<1;
}
k=1;
for(i=23;i>=16;i--)
{
three_char[i]=(z&k)/k;
k=k<<1;
}

}
void change_3_into_4(int three_char[],unsigned char four_char[],int &k,int &move,int &math)
{
char base64[65]={'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','+','/'};
int i;
int j=0;
int u=0;
unsigned char* p;

for(i=0;i<=23;)
{
u=0;
for(j=0;j<=5;j++)
{
u=u+three_char[i+j]*(int)pow(2,5-j);
}
four_char[k]=base64[u];
k++;
i=i+6;

}
/* printf("\n");
p=four_char;
while(*p!='\0')
{
p++;
}
if(move==2)
{
*p='=';
*(p+1)='=';
}
if(move==1)
{
*p='=';
}*/
}

void base_64(unsigned char old_char[],int three_char[],unsigned char four_char[],unsigned char &x,unsigned char &y,unsigned char &z,int &k,int &move,int &math)
{
int i=0;
unsigned char* p;

for(i=0;i<=(math+move-1);)
{
x=old_char[i];
y=old_char[i+1];
z=old_char[i+2];
change_into_21(x,y,z,three_char);
i=i+3;
change_3_into_4(three_char,four_char,k,move,math);

}
p=four_char;
while(*p!='\0')
{
p++;
}
if(move==2)
{
*p='=';
*(p+1)='=';
}
if(move==1)
{
*p='=';
}
}
void save_char(unsigned char four_char[],char* ar,int &math,int &move)
{
FILE* fp;
fp=fopen(ar,"w+b");
int i=0;
while(four_char[i]!='\0')
{
i++;
}
i;
printf("i=%d",i);
fwrite(four_char,i,1,fp);
fclose(fp);
}
void save_char2(unsigned char three_char2[],char* ar,int &move,int &k)
{

if(move==2)
{
three_char2[k-1]=three_char2[k];
three_char2[k-2]=three_char2[k];
}
if(move==1)
{
three_char2[k-1]=three_char2[k];
}
FILE* fp;

fp=fopen(ar,"w+b");
fwrite(three_char2,k-move,1,fp);
fclose(fp);
}
void get_char2(unsigned char old_64_char[],int &move,char* ar)
{
FILE* fp;
unsigned char old_char[1024*1024];
unsigned char* p;
unsigned char* q;
int i=0;

fp=fopen(ar,"rb");
fread(old_char,1024*1024,1,fp);
//printf("%s",old_char);
fclose(fp);
p=old_char;
q=old_64_char;

while(*p!='\0')
{
if(*p!='=')
{
*q=*p;
}
if(*p=='=')
{
move++;
}
p++;
q++;
i++;
}
printf("i(k/3*4)+move=%d\n",i);
}
void change_into_22(unsigned char &w,unsigned char &x,unsigned char &y,unsigned char &z,int four_char2[])
{
char base_64[65]={'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','+','/'};

int i=0;
int k=1;
int j=0;
char* p;
p=base_64;
int a,b,c,d;
j=0;
p=base_64;
while(*p!='\0')
{
if(*p==w)
{
a=j;
}
if(*p==x)
{
b=j;
}
if(*p==y)
{
c=j;
}
if(*p==z)
{
d=j;
}
j++;
p++;
}

k=1;
for(i=5;i>=0;i--)
{
four_char2[i]=(a&k)/k;
k=k<<1;
}
k=1;
for(i=11;i>=6;i--)
{
four_char2[i]=(b&k)/k;
k=k<<1;
}
k=1;
for(i=17;i>=12;i--)
{
four_char2[i]=(c&k)/k;
k=k<<1;
}
k=1;
for(i=23;i>=18;i--)
{
four_char2[i]=(d&k)/k;
k=k<<1;
}
}
void change_4_into_3(int four_char2[],unsigned char three_char2[],int &k,int &move)
{
int i;
int j=0;
int u=0;
unsigned char* p;

for(i=0;i<=23;)
{
u=0;
for(j=0;j<=7;j++)
{
u=u+four_char2[i+j]*(int)pow(2,7-j);

}
three_char2[k]=char(u);
k++;
i=i+8;
}

}
void base_644(unsigned char old_64_char[],int move,unsigned char &w,unsigned char &x,unsigned char &y,unsigned char &z,int &k,int four_char2[],unsigned char three_char2[])
{
int i=0;

while(old_64_char[i]!='\0')
{
w=old_64_char[i];
x=old_64_char[i+1];
y=old_64_char[i+2];
z=old_64_char[i+3];
change_into_22(w,x,y,z,four_char2);
i=i+4;
change_4_into_3(four_char2,three_char2,k,move);
}
}

main(int argc,char *argv[])
{
int math;
int i=0;
for(i=0;i<=argc;i++)
{




if(strcmp(argv[i],"-e")==0)
{

unsigned char x,y,z;
int k=0;
int move=0;

get_char1(old_char,move,argv[i+1],math);
base_64(old_char,three_char,four_char,x,y,z,k,move,math);
save_char(four_char,argv[i+2],math,move);
printf("math=%d",math);
printf("move=%d",move);
printf("%s",four_char);
int j=0;
while(four_char[j]!='\0')
j++;
printf("j=%d\n",j);
printf("%s",four_char);
break;

}
if(strcmp(argv[i],"-d")==0)
{
int k=0;
int move=0;
unsigned char w,x,y,z;

get_char2(old_64_char,move,argv[i+1]);
base_644(old_64_char,move,w,x,y,z,k,four_char2,three_char2);
save_char2(three_char2,argv[i+2],move,k);
//printf("%s",old_64_char);
k=k-2;
printf("finalnum=%d\n",k);
printf("move=%d",move);
break;

}


}
}


2007-07-18 10:26
酒肉弥勒佛
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:399
专家分:0
注 册:2006-6-6
收藏
得分:0 
强,高手
怎么使用啊

编程是为了提高效率,好的程序是因为他的高效;在编程的道路上,永远追逐高效的算法
2007-07-18 11:34
zzjking
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-7-16
收藏
得分:0 

./a.out -e xxx.out xxx.cfg
这个是转为64码的 xxx.out 是你要转的 xxx.cfg 是你要中转存的

./a.out -d xxx.cfg xxx.out
这个是反编译为原文件的 xxx.cfg 是你刚才中转的 xxx.out是你还原的

说白了 -e 是编译为64码
-d 是把64码的转为原文件
后面都跟 2个文件名就成了

有 兴趣的话把你的 a.out复制1个b.out 你转化64 再转回去 看看能不能用 ./b.out -e xxx xxx
或者 直接打 diff -i a.out b.out就能看出来2个文件一样不一样了~呵呵
乱做着玩的~


2007-07-18 11:41
zzjking
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2007-7-16
收藏
得分:0 

改进完的版本~可以不定义数组大小的 1次取3个 直到取完流为止
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

unsigned char old_char[4];
unsigned char four_char[5];
int three_char[24];
unsigned char w,x,y,z;

void get_char1(unsigned char old_char[],char *ar,int &offset)
{
FILE* fp;
unsigned char* p;

p=old_char;
while(*p!='\0')
{
*p=0;
p++;
}
fp=fopen(ar,"rb");
fseek(fp,offset,SEEK_SET);
fread(old_char,3,1,fp);
//printf("%s\n",old_char);
fclose(fp);
offset=offset+3;

}
void change_into_21(unsigned char &x,unsigned char &y,unsigned char &z,int three_char[])
{
int i;
int k=1;
for(i=7;i>=0;i--)
{
three_char[i]=(x&k)/k;
k=k<<1;
}
k=1;
for(i=15;i>=8;i--)
{
three_char[i]=(y&k)/k;
k=k<<1;
}
k=1;
for(i=23;i>=16;i--)
{
three_char[i]=(z&k)/k;
k=k<<1;
}
//for(i=0;i<=23;i++)
//printf("%d",three_char[i]);
//if(i==23)
//printf("\n");
}
void change_3_into_4(int three_char[],unsigned char four_char[])
{
char base_64[65]={'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','+','/'};
int i;
int k=0;
int j=0;
int u;

for(i=0;i<=23;)
{
u=0;
for(j=0;j<=5;j++)
{
u=u+three_char[i+j]*(int)pow(2,5-j);

}
//printf("u=%d\n",u);
four_char[k]=base_64[u];
k++;
i=i+6;
}
}
void save_char(unsigned char four_char[],char *ar,int &offset)
{
FILE* fp;
fp=fopen(ar,"a+b");
fwrite(four_char,4,1,fp);
fputc('\n',fp); /* */
fclose(fp);
printf("%s",four_char);
}
void base_64(unsigned char &x,unsigned char &y,unsigned char &z,unsigned char four_char[],int three_char[])
{
x=old_char[0];
y=old_char[1];
z=old_char[2];

change_into_21(x,y,z,three_char);
change_3_into_4(three_char,four_char);
}
void move_char(int &move,unsigned char four_char[],char *ar)
{
FILE* fp;
unsigned char* p;
char a='=';

fp=fopen(ar,"a+b");
for(move;move>0;move--)
{
fputc(a,fp);
}
fclose(fp);
}
void get_char2(unsigned char four_char[],char *ar,int &offset)
{
FILE* fp;
int i=0;
unsigned char c;

fp=fopen(ar,"rb");
for(i=0;i<=3;i++)
{
fseek(fp,offset,SEEK_SET);
if((c=fgetc(fp))!='\n')
{
four_char[i]=c;
++offset;
}
if(fgetc(fp)=='\n')
{
offset++;
fseek(fp,offset,SEEK_SET);
}
}
/* printf("\n");
printf("#1=%c,",four_char[0]);
printf("#2=%c,",four_char[1]);
printf("#3=%c,",four_char[2]);
printf("#4=%c",four_char[3]);
printf("\n");*/
fclose(fp);
}
void change_into_22(unsigned char &w,unsigned char &x,unsigned char &y,unsigned char &z,unsigned char four_char[],int three_char[])
{
char base_64[65]={'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','+','/'};
int i=0;
int k=1;
int j=0;
char* p;
int a,b,c,d;

p=base_64;
while(*p!='\0')
{
if(*p==w)
{
a=j;
}
if(*p==x)
{
b=j;
}
if(*p==y)
{
c=j;
}
if(*p==z)
{
d=j;
}
j++;
p++;
}
// printf("a=%d,b=%d,c=%d,d=%d",a,b,c,d);
k=1;
for(i=5;i>=0;i--)
{
three_char[i]=(a&k)/k;
k=k<<1;
}
k=1;
for(i=11;i>=6;i--)
{
three_char[i]=(b&k)/k;
k=k<<1;
}
k=1;
for(i=17;i>=12;i--)
{
three_char[i]=(c&k)/k;
k=k<<1;
}
k=1;
for(i=23;i>=18;i--)
{
three_char[i]=(d&k)/k;
k=k<<1;
}
}
void change_4_into_3(int three_char[],unsigned char old_char[])
{
int i=0;
int j=0;
int u=0;
int k=0;

k=0;
for(i=0;i<=23;)
{
u=0;
for(j=0;j<=7;j++)
{
u=u+three_char[i+j]*(int)pow(2,7-j);
}
old_char[k]=char(u);
k++;
i=i+8;
}
}
void base_644(unsigned char &w,unsigned char &x,unsigned char &y,unsigned char &z,unsigned char four_char[],int three_char[])
{

w=four_char[0];
x=four_char[1];
y=four_char[2];
z=four_char[3];

change_into_22(w,x,y,z,four_char,three_char);
change_4_into_3(three_char,old_char);
}
void save_char2(unsigned char old_char[],char *ar)
{
FILE* fp;
fp=fopen(ar,"a+b");
fwrite(old_char,3,1,fp);
fclose(fp);
}
void move_char2(unsigned char old_char[],char *ar,int &move)
{
FILE *fp;
fp=fopen(ar,"a+b");
if(move==1)
fwrite(old_char,2,1,fp);
if(move==2)
fwrite(old_char,1,1,fp);
if(move==0)
fwrite(old_char,3,1,fp);
fclose(fp);
}
main(int argc,char *argv[])
{
int i=0;
FILE* fp;
FILE* ff;
int offset=0;
unsigned char c;
int j=0;
int move=0;

for(i=0;i<=argc;i++)
{
if(strcmp(argv[i],"-e")==0)
{
fp=fopen(argv[i+1],"rb");
while(!feof(fp))
{
j++;
fgetc(fp);

}

if((j-1)%3==1)
{
j=(j-1)/3+1;
move=move+2;
}
if((j-1)%3==2)
{
j=(j-1)/3+1;
move++;
}
if((j-1)%3==0)
j=(j-1)/3;

printf("j=%d",j);
for(j;j>0;j--)
{
get_char1(old_char,argv[i+1],offset);
base_64(x,y,z,four_char,three_char);
save_char(four_char,argv[i+2],offset);
}
move_char(move,four_char,argv[i+2]);
fclose(fp);
break;
}
if(strcmp(argv[i],"-d")==0)
{
ff=fopen(argv[i+1],"rb");
while(!feof(ff))
{
c=fgetc(ff);
if(c!='='&&c!='\n')
j++;
if(c=='=')
move++;
}
fclose(ff);
j=(j-1)/4;
printf("j=%d,move=%d",j,move);
for(j;j>0;j--)
{
get_char2(four_char,argv[i+1],offset);
base_644(w,x,y,z,four_char,three_char);
if(j!=1)
save_char2(old_char,argv[i+2]);
if(j==1)
move_char2(old_char,argv[i+2],move);

}
printf(" %d",offset);
break;
}

}
}


2007-07-24 12:55
快速回复:base64的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039833 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved