使用C++对数据进行Base64加密解密

定义加密选项

enum Base64Option {
   Base64Encoding = 0,
   Base64UrlEncoding = 1,

   KeepTrailingEquals = 0,
   OmitTrailingEquals = 2
};

对数据的base64加密

std::string encode_base64(const  std::string sourceData, int options = Base64Encoding)
{
    const char alphabet_base64[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
                                   "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
    const char alphabet_base64url[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
                                      "ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
    const char *const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64;
    const char padchar = '=';
    int padlen = 0;

    std::string tmp;
    tmp.resize((sourceData.size() + 2) / 3 * 4);

    int i = 0;
    char *out = &tmp[0];
    while (i < sourceData.size()) {
        // encode 3 bytes at a time
        int chunk = 0;
        chunk |= int(uchar(sourceData.data()[i++])) << 16;
        if (i == sourceData.size()) {
            padlen = 2;
        } else {
            chunk |= int(uchar(sourceData.data()[i++])) << 8;
            if (i == sourceData.size())
                padlen = 1;
            else
                chunk |= int(uchar(sourceData.data()[i++]));
        }

        int j = (chunk & 0x00fc0000) >> 18;
        int k = (chunk & 0x0003f000) >> 12;
        int l = (chunk & 0x00000fc0) >> 6;
        int m = (chunk & 0x0000003f);
        *out++ = alphabet[j];
        *out++ = alphabet[k];

        if (padlen > 1) {
            if ((options & OmitTrailingEquals) == 0)
                *out++ = padchar;
        } else {
            *out++ = alphabet[l];
        }
        if (padlen > 0) {
            if ((options & OmitTrailingEquals) == 0)
                *out++ = padchar;
        } else {
            *out++ = alphabet[m];
        }
    }
    assert((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data()));
    if (options & OmitTrailingEquals)
        tmp.resize(out - tmp.data());
    return tmp;
}

参数sourceData代表要被加密的数据, options表示加密选项flags 默认0

然后是对数据的base64解密

std::string decode_base64(const  std::string sourceData, int options = Base64Encoding)
{
    unsigned int buf = 0;
    int nbits = 0;
    std::string tmp;
    tmp.resize((sourceData.size() * 3) / 4);

    int offset = 0;
    for (int i = 0; i < sourceData.size(); ++i) {
        int ch = sourceData.at(i);
        int d;

        if (ch >= 'A' && ch <= 'Z')
            d = ch - 'A';
        else if (ch >= 'a' && ch <= 'z')
            d = ch - 'a' + 26;
        else if (ch >= '0' && ch <= '9')
            d = ch - '0' + 52;
        else if (ch == '+' && (options & Base64UrlEncoding) == 0)
            d = 62;
        else if (ch == '-' && (options & Base64UrlEncoding) != 0)
            d = 62;
        else if (ch == '/' && (options & Base64UrlEncoding) == 0)
            d = 63;
        else if (ch == '_' && (options & Base64UrlEncoding) != 0)
            d = 63;
        else
            d = -1;

        if (d != -1) {
            buf = (buf << 6) | d;
            nbits += 6;
            if (nbits >= 8) {
                nbits -= 8;
                tmp[offset++] = buf >> nbits;
                buf &= (1 << nbits) - 1;
            }
        }
    }

    tmp.resize(offset);
    return tmp;
}

参数sourceData代表要被解密的数据, options表示加密选项flags,默认0

让我们来看看运行结果:

    std::string srcData = "ABCDEFGHIGKLMN...+++---000123456789~!@#$%^&*()_+";
    std::string srcData_base64 = encode_base64(srcData);
    std::string dstData_base64 = decode_base64(srcData_base64);

    std::cout<< "src data:" << srcData << std::endl;
    std::cout<< "to base64:" << srcData_base64 << std::endl;
    std::cout <<"from base64:" << dstData_base64 << std::endl;

    //url

    srcData = "https://mp.csdn.net/postedit/83345819";
    srcData_base64 = encode_base64(srcData, Base64UrlEncoding & OmitTrailingEquals);
    dstData_base64 = decode_base64(srcData_base64, Base64UrlEncoding & OmitTrailingEquals);

    std::cout<< "src data:" << srcData << std::endl;
    std::cout<< "to base64:" << srcData_base64 << std::endl;
    std::cout <<"from base64:" << dstData_base64 << std::endl;

得到结果:

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值