Base64编码与解码

Base64是什么

Base64是一种将二进制编码成64个可见字符的编码方式。

Base64编码把3个8位字节转化为4个6位的字节。如果不足3个字节,则用0填充,输出字符末尾补’=’,因此编码后输出的文本末尾可能会出现1或2个’=’。为了保证所输出的编码位可读字符,Base64制定了一个编码表,以便进行统一转换。编码表的大小为2^6=64,这也是Base64名称的由来。

比如加密字符A
字符A的ASCII码是65,二进制为‭01000001‬
不足24位,末尾补0,变为01000001‬ 00000000 00000000
由于添加了2个8位,所以最终结果要补充2个’=’
划分为4个6位,变为010000 01 0000 000000 000000
查表可得010000对应’Q’,因此最终结果为”QQ==”

实现代码

#ifndef BASE64_H
#define BASE64_H

class Base64
{
    public:
        Base64();
        bool Encode(const unsigned char* bytes, int size, char* str, int maxLen);
        bool Decode(unsigned char* bytes, int& size, const char* str, int maxSize);
        int SizeToLen(int size);
        int LenToSize(int len);
        virtual ~Base64();
    protected:
    private:
        static const char* table;
        int getIdx(char chr);
};

#endif // BASE64_H
#include "Base64.h"

Base64::Base64() { }
Base64::~Base64() { }

const char* Base64::table =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int Base64::SizeToLen(int size) {
    return (size+1)*8%6;
}

int Base64::LenToSize(int len) {
    return len*6/8;
}

bool Base64::Encode(const unsigned char* bytes, int size, char* str, int maxLen) {
    unsigned long bit = 0;
    unsigned long idx[4];
    int len = 0;
    int add = 0;
    for (int i = 0; i < size; i += 3) {
        bit = 0;
        for (int j = 0; j < 3; j++) {
            bit <<= 8;
            if (i + j < size) bit |= bytes[i + j];
            else add++;
        }
        for (int j = 3; j >= 0; j--) {
            if (add) {
                idx[j] = '=';
                add--;
            }
            else idx[j] = table[bit & 63];
            bit >>= 6;
        }
        for (int j = 0; j < 4; j++) {
            if (len >= maxLen) {
                str[len] = '\0';
                return false;
            }
            str[len++] = idx[j];
        }
    }
    str[len] = '\0';
    return true;
}

bool Base64::Decode(unsigned char* bytes, int& size, const char* str, int maxSize) {
    unsigned long bit = 0;
    unsigned long idx[3];
    size = 0;
    int add = 0;
    for (;*str;) {
        bit = 0;
        for (int j = 0; j < 4; j++) {
            bit <<= 6;
            if (*str == '\0') continue;
            if (*str != '=') bit |= getIdx(*str);
            else add++;
            str++;
        }
        for (int j = 2; j >= 0; j--) {
            idx[j] = bit & 255;
            bit >>= 8;
        }
        for (int j = 0; j < 3 - add; j++) {
            if (size >= maxSize) {
                bytes[size] = 0;
                return false;
            }
            bytes[size++] = idx[j];
        }
    }
    bytes[size] = 0;
    size--;
    return true;
}

int Base64::getIdx(char chr) {
    if (chr >= 'A' && chr <= 'Z') return chr - 'A';
    if (chr >= 'a' && chr <= 'z') return chr - 'a' + 26;
    if (chr >= '0' && chr <= '9') return chr - '0' + 52;
    if (chr == '+') return 62;
    if (chr == '/') return 63;
    return -1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值