CTF-base64加解密

base64

是一种编码方式。把3个8bit变成4个6bit。然后不足补0,符号是’=’. 然后还有一张表。

测试

ZmxhZ3tUSEVfRkxBR19PRl9USElTX1NUUklOR30=
输出:
flag{THE_FLAG_OF_THIS_STRING}

代码

解密
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <string>
using namespace std;

char str[1010];
char dict(int x,bool flag) {
    if(x == 0) {
        if(flag) return '=';
        else return 'A';
    }
    else if(x < 26) {
        return 'A' + x;
    }
    else if(x < 52) {
        return 'a' + x - 26;
    }
    else if(x < 62){
        return '0' + x - 52;
    }
    else if(x == 63) return '+';
    else return '/';
}

char refdict(char x) {
    if(x >= 'A' && x <= 'Z') {
        return x - 'A';
    }
    else if(x >= 'a' && x <= 'z') {
        return x - 'a' + 26;
    }
    else if(x >= '0' && x <= '9') {
        return x - '0' + 52;
    }
    else if(x == '+') return 63;
    else if(x == '/') return 64;
    else if(x == '=') return 0;
}

int main() {
    while(gets(str)) {
        string ans = "";

        int len = strlen(str);
        for (int i = 0;i + 3 < len;i += 4) {
            int num = 0;
            for (int j = 0;j < 4;j ++) {
                num = (num << 6) + refdict(str[i * 4 + j]);
            }
            string tmp = "";
            for (int j = 0;j < 3;j ++) {
                int val = num & 0x000000ff;
                num >>= 8;
                tmp = char(val) + tmp;
            }
            ans = ans + tmp;
        }
        cout << ans << endl;
    }
}





加密
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <string>
using namespace std;

char str[1010];
char dict(int x,bool flag) {
    if(x == 0) {
        if(flag) return '=';
        else return 'A';
    }
    else if(x < 26) {
        return 'A' + x;
    }
    else if(x < 52) {
        return 'a' + x - 26;
    }
    else if(x < 62){
        return '0' + x - 52;
    }
    else if(x == 63) return '+';
    else return '/';
}

int main() {
    freopen("in.txt","r",stdin);

    while(gets(str)) {
        cout << str << endl;
        string ans = "";
        int len = strlen(str);
        int newlen = ((len + 2) / 3 ) * 3;
        for (int i = len;i < newlen;i ++) {
            str[i] = 0;
        }
        str[newlen] = '\0';

        for (int i = 0;i + 2 < newlen;i += 3) {
            int num = (int)str[i + 2] + (((int)str[i + 1]) << 8) + (((int)str[i]) << 16);
            string tmp = "";
            for (int j = 3;j >= 0;j --) {
                int cur = num & 0x0000003f; //i * 24 + j * 6
                tmp = dict(cur,i * 24 + j * 6 >= len) + tmp;
                num >>= 6;
            }
            ans = ans + tmp;
        }
        cout << ans << endl;
    }
}






  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值