base64加密解密c++代码

关于base64加密解密代码:

程序运行功能请自行查看main函数:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#include<stdlib.h>
#include<algorithm>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值