base64编码/解码

一个base64 编码解码的网站:Base64 在线编码解码 | Base64 加密解密 - Base64.us

#ifndef  __BASE_64_H__
#define  __BASE_64_H__

// base64.h

#include <string>
#include <iostream>

#define UNSIG_CHAR_PTR(x) ((const unsigned char*)(x))
#define INVALID        0xff

static const unsigned char ToBase64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const unsigned char UnBase64[] = {
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62,  255, 255, 255, 63,
            52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  255, 255, 255, 255, 255, 255,
            255, 0,   1,   2,   3,   4,   5,   6,   7,   8,   9,   10,  11,  12,  13,  14,
            15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  255, 255, 255, 255, 255,
            255, 26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,
            41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
            255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};    
            
class Base64{
public:
    /*
    * @brief  base64编码
    * @param  src  需要编码的字符串
    * @return 编码后的字符串
    */
    std::string Encode(const std::string& src);

    /*
    * @brief  base64解码
    * @param  src  需要解码的字符串
    * @return 解码后的字符串
    */
    std::string Decode(const std::string& src);

    
private:

};

#endif  

 

#include "base64.h"

// base64.cpp

std::string Base64::Encode(const std::string& src){
    auto in = src.c_str();
    auto len = src.length();

    const unsigned char *cp;
    int count = 0;
    int size = len * 4 /3;

    unsigned char* dstBuffer = (unsigned char *)malloc(size + 1);
    int out_index = 0;
    for (cp = UNSIG_CHAR_PTR(in), count = len; count > 0; count -= 3, cp += 3)
    {
        dstBuffer[out_index++] = ToBase64[cp[0] >> 2];
        if (count > 1)
        {
            dstBuffer[out_index++] = ToBase64[(cp[0] & 0x3) << 4 | cp[1] >> 4];
            if (count > 2)
            {
                dstBuffer[out_index++] = ToBase64[(cp[1] & 0xf) << 2 | cp[2] >> 6];
                dstBuffer[out_index++] = ToBase64[cp[2] & 0x3f];
            }
            else
            {
                dstBuffer[out_index++] = ToBase64[(cp[1] & 0xf) << 2];
                dstBuffer[out_index++] = '=';
                break;
            }
        }
        else
        {
            dstBuffer[out_index++] = ToBase64[(cp[0] & 0x3) << 4];
            dstBuffer[out_index++] = '=';
            dstBuffer[out_index++] = '=';
            break;
        }
    }

    dstBuffer[out_index] = 0;

    return std::string((char*)dstBuffer,out_index);
}


std::string Base64::Decode(const std::string& src){
    auto in = src.c_str();
    auto len = src.length();

    const unsigned char *cp;
    int     count;
    int     ch0;
    int     ch1;
    int     ch2;
    int     ch3;

    //Sanity check.
    if (len % 4)
    {
        return "";
    }

    unsigned char * dstBuffer = (unsigned char *)malloc(len + 1);
    int out_index = 0;
    for (cp = UNSIG_CHAR_PTR(in), count = 0; count < (int)len; count += 4)
    {
        if ((ch0 = UnBase64[*cp++]) == INVALID|| (ch1 = UnBase64[*cp++]) == INVALID)
            return (0);
        dstBuffer[out_index++] = ch0 << 2 | ch1 >> 4;
        if ((ch2 = *cp++) == '=')
            break;
        if ((ch2 = UnBase64[ch2]) == INVALID)
            return (0);
        dstBuffer[out_index++] = ch1 << 4 | ch2 >> 2;
        if ((ch3 = *cp++) == '=')
            break;
        if ((ch3 = UnBase64[ch3]) == INVALID)
            return (0);
        dstBuffer[out_index++] = ch2 << 6 | ch3;
    }

    dstBuffer[out_index] = 0;

    return std::string((char*)dstBuffer,out_index);
}

 

#include <iostream>
#include "base64.h"

// main.cpp

void testBase64(std::string src){
    std::cout<<"----------- testBase64 -----------"<<std::endl;
    Base64 base64;
    std::string encSrc = base64.Encode(src);
    std::string decEncSrc = base64.Decode(encSrc);
    std::cout<<"src: "<< src <<std::endl;
    std::cout<<"encSrc: "<< encSrc <<std::endl;
    std::cout<<"decEncSrc: "<< decEncSrc <<std::endl;
}

void test1(){
    testBase64("abcabcabc");
    testBase64("abcdefg");
    testBase64("111111");
    testBase64("121212");
    testBase64("123456");
    testBase64("你好123");
    testBase64("123你好123");

    testBase64("数据大屏新告警");
}



int main(){
    test1();

    std::cout<<"hello base64"<<std::endl;
    return 0;
}

 

posted @ 2024-08-21 17:15  He_LiangLiang  阅读(1)  评论(0编辑  收藏  举报