关于base64加密解密代码:
程序运行功能请自行查看main函数:
#include#include #include #include #include #include #include using namespace std;char str[70];char str1[1000];char ch[1000010];int ch1[1000010];void f(){ int i,j; memset(str,0,sizeof(str)); memset(str1,0,sizeof(str1)); j=0; for(i=0;i<26;i++){ str1[i+'A']=j; str[j++]=i+'A'; } for(i=0;i<26;i++){ str1[i+'a']=j; str[j++]=i+'a'; } for(i=0;i<=9;i++){ str1[i+'0']=j; str[j++]=i+'0'; } str1['+']=j; str[j++]='+'; str1['/']=j; str[j++]='/'; str[j++]='\0'; str1['=']=0; /*for(i=0;i<65;i++){ printf("%c ",str[i]); } printf("\n");*/}void jiami (){ int len=strlen(ch); int i,j,k=0; int m,l; for(i=len-1;i>=0;i--){ for(j=0;j<8;j++){ ch1[k++]=ch[i]%2; ch[i]/=2; } } m=0; for(i=k-1,j=0,l=0;;i--,j++){ if(j>=6){ if(i>-6) printf("%c",str[m]); else printf("="); l++;j=0;m=0; if(l%76==0){ printf("\n"); } if(i<=0&&l%4==0){ break; } } if(i>=0) m=m*2+ch1[i]; else m*=2; } printf("\n");}void jiemi (){ int len=strlen(ch); int i,j,k=0,l; int x; for(i=len-1;i>=0;i--){ x=str1[ch[i]]; //printf("%c %d\n",ch[i],x); //system("pause"); for(j=0;j<6;j++){ ch1[k++]=x%2; x/=2; // printf("%d ",ch1[k-1]); } } // printf("\n"); int m=0; for(i=k-1,j=0,l=0;;i--,j++){ if(j>=8){ if(i>-8){ printf("%c",m);l++; if(l%76==0){ printf("\n"); } } j=0,m=0; if(i<=0) break; } if(i>=0) m=m*2+ch1[i]; else m*=2; } printf("\n");}int main (){ f(); //printf("%d\n",str1['2']); int i,j,l=0; while(~scanf("%s",ch)){ l++; if(strcmp(ch,"#exit#")==0) break; printf("Case #%d\n",l); if(strcmp(ch,"#s2b#")==0){ getchar(); while(gets(ch)!=NULL){ if(strcmp(ch,"#CaseEnd#")==0) break; jiami(); } }else if(strcmp(ch,"#b2s#")==0){ getchar(); while(gets(ch)!=NULL){ if(strcmp(ch,"#CaseEnd#")==0) break; jiemi(); } } } return 0;}