Base32系列编码 代码实现过程

简介: Base32系列编码 代码实现过程

Base16

C++实现加解密过程

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
const char base16_table[] = "0123456789abcdef";
// Base16 编码
string base16_encode(const string& input) {
    string output;
    for (unsigned char c : input) {
        output += base16_table[c >> 4];
        output += base16_table[c & 0x0F];
    }
    return output;
}
// Base16 解码
string base16_decode(const string& input) {
    string output;
    for (int i = 0; i < input.length(); i += 2) {
        unsigned char c1 = input[i];
        unsigned char c2 = input[i + 1];
        if (isxdigit(c1) && isxdigit(c2)) {
            output += ((strchr(base16_table, c1) - base16_table) << 4) | (strchr(base16_table, c2) - base16_table);
        } else {
            break;
        }
    }
    return output;
}
int main() {
    string input = "Hello, world!";
    string encoded = base16_encode(input);
    string decoded = base16_decode(encoded);
    cout << "Input: " << input << endl;
    cout << "Encoded: " << encoded << endl;
    cout << "Decoded: " << decoded << endl;
    return 0;
}

Base32

C++实现加密过程

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
string base32padding(string input) {
    int len = input.length();
    int padding_len = 8 - len % 8;
    if (padding_len == 8) {
        return input;
    }
    input.append(padding_len, '=');
    return input;
}
string base32encry(string input) {
    int len = 0, str_len = 0;
    char *encry;
    char *table32 = "Z2CDEFGHIJKLMNOPQRSTUVWXY76543AB";
    len = input.length(); //获取输入长度 
    if (len % 5 == 0)
        str_len = len / 5 * 8;
    else
        str_len = ((len / 5) + 1) * 8;
    encry = new char[str_len + 1];
    for (int i = 0, j = 0; i < len; i += 5, j += 8) {
        encry[j] = table32[input[i] >> 3];
        encry[j + 1] = table32[((input[i] & 0x7) << 2) | ((input[i + 1]) >> 6)];
        encry[j + 2] = table32[(input[i + 1] >> 1) & 0x1f];
        encry[j + 3] = table32[((input[i + 1] & 0x1) << 4) | ((input[i + 2]) >> 4)];
        encry[j + 4] = table32[((input[i + 2] & 0xf) << 1) | ((input[i + 3]) >> 7)];
        encry[j + 5] = table32[(input[i + 3] >> 2) & 0x1f];
        encry[j + 6] = table32[((input[i + 3] & 0x3) << 3) | ((input[i + 4]) >> 5)];
        encry[j + 7] = table32[input[i + 4] & 0x1f];
    }
    switch (len % 5) {
    case 1:
        encry[str_len - 1] = '\0';
        encry[str_len - 2] = '\0';
        encry[str_len - 3] = '\0';
        encry[str_len - 4] = '\0';
        encry[str_len - 5] = '\0';
        encry[str_len - 6] = '\0';
        break;
    case 2:
        encry[str_len - 1] = '\0';
        encry[str_len - 2] = '\0';
        encry[str_len - 3] = '\0';
        encry[str_len - 4] = '\0';
        break;
    case 3:
        encry[str_len - 1] = '\0';
        encry[str_len - 2] = '\0';
        encry[str_len - 3] = '\0';
        break;
    case 4:
        encry[str_len - 1] = '\0';
        break;
    }
    encry[str_len] = '\0';
    string result(encry);
    result=base32padding(result);
    delete[] encry;
    return result;
}

Python实现过程

import base64
# 自定义 base32 编码表
base32_table = b"JKLMNEFGHIXY7654OPQRSTUVWZ2CD3AB"
# 加密函数,使用自定义编码表
def encode_base32(text):
    return base64.b32encode(bytes(text, 'utf-8')).translate(bytes.maketrans(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567", base32_table)).decode('utf-8')
# 解密函数,使用自定义编码表
def decode_base32(text):
    return base64.b32decode(bytes(text.translate(bytes.maketrans(base32_table, b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")), 'utf-8')).decode('utf-8')
# 测试加密和解密函数
text = "Hello Base32"
encrypted_text = encode_base32(text)
print("加密后的字符串:", encrypted_text)
decrypted_text = decode_base32(encrypted_text)
print("解密后的字符串:", decrypted_text)

Base58

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
const string base58_table = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
// Base58 编码
string base58_encode(const vector<unsigned char>& data) {
    // 计算编码后的长度
    int zeros = 0;
    while (zeros < data.size() && data[zeros] == 0) {
        zeros++;
    }
    int size = (data.size() - zeros) * 138 / 100 + 1;
    vector<unsigned char> result(size, 0);
    // 从低位到高位计算编码值
    for (int i = zeros; i < data.size(); i++) {
        int carry = data[i];
        for (int j = size - 1; j >= 0; j--) {
            carry += 256 * result[j];
            result[j] = carry % 58;
            carry /= 58;
        }
    }
    // 将编码值转换为 Base58 编码字符串
    int j = 0;
    while (j < result.size() && result[j] == 0) {
        j++;
    }
    string output;
    output.reserve(zeros + result.size() - j);
    output.assign(zeros, '1');
    for (int i = j; i < result.size(); i++) {
        output += base58_table[result[i]];
    }
    return output;
}
// Base58 解码
vector<unsigned char> base58_decode(const string& data) {
    // 计算解码后的长度
    int zeros = 0;
    while (zeros < data.length() && data[zeros] == '1') {
        zeros++;
    }
    int size = (data.length() - zeros) * 733 / 1000 + 1;
    vector<unsigned char> result(size, 0);
    // 从低位到高位计算解码值
    for (int i = zeros; i < data.length(); i++) {
        char c = data[i];
        if (c < '1' || c > '9') {
            break;
        }
        int carry = base58_table.find(c);
        for (int j = size - 1; j >= 0; j--) {
            carry += 58 * result[j];
            result[j] = carry % 256;
            carry /= 256;
        }
    }
    return vector<unsigned char>(result.begin(), result.end());
}
int main() {
    string input = "Hello, world!";
    vector<unsigned char> data(input.begin(), input.end());
    string encoded = base58_encode(data);
    vector<unsigned char> decoded = base58_decode(encoded);
    string output(decoded.begin(), decoded.end());
    cout << "Input: " << input << endl;
    cout << "Encoded: " << encoded << endl;
    cout << "Decoded: " << output << endl;
    return 0;
}

Base64

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
const string base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Base64 编码
string base64_encode(const vector<unsigned char>& data) {
    string output;
    int i = 0;
    int j = 0;
    int b = 0;
    for (i = 0; i < data.size(); i++) {
        b |= data[i] << (16 - 8 * j);
        j++;
        if (j == 3) {
            output += base64_table[(b >> 18) & 0x3F];
            output += base64_table[(b >> 12) & 0x3F];
            output += base64_table[(b >> 6) & 0x3F];
            output += base64_table[b & 0x3F];
            j = 0;
            b = 0;
        }
    }
    if (j == 1) {
        output += base64_table[(b >> 2) & 0x3F];
        output += base64_table[(b << 4) & 0x3F];
        output += "==";
    } else if (j == 2) {
        output += base64_table[(b >> 10) & 0x3F];
        output += base64_table[(b >> 4) & 0x3F];
        output += base64_table[(b << 2) & 0x3F];
        output += "=";
    }
    return output;
}
// Base64 解码
vector<unsigned char> base64_decode(const string& data) {
    vector<unsigned char> output;
    int i = 0;
    int j = 0;
    int b = 0;
    for (i = 0; i < data.length(); i++) {
        char c = data[i];
        if (c == '=') {
            break;
        }
        int index = base64_table.find(c);
        if (index == string::npos) {
            break;
        }
        b |= index << (18 - 6 * j);
        j++;
        if (j == 4) {
            output.push_back((b >> 16) & 0xFF);
            output.push_back((b >> 8) & 0xFF);
            output.push_back(b & 0xFF);
            j = 0;
            b = 0;
        }
    }
    if (j == 2) {
        output.push_back((b >> 10) & 0xFF);
    } else if (j == 3) {
        output.push_back((b >> 16) & 0xFF);
        output.push_back((b >> 8) & 0xFF);
    }
    return output;
}
int main() {
    string input = "Hello, world!";
    vector<unsigned char> data(input.begin(), input.end());
    string encoded = base64_encode(data);
    vector<unsigned char> decoded = base64_decode(encoded);
    string output(decoded.begin(), decoded.end());
    cout << "Input: " << input << endl;
    cout << "Encoded: " << encoded << endl;
    cout << "Decoded: " << output << endl;
    return 0;
}
相关文章
|
存储 安全 算法
CAN总线位时序的介绍
CAN总线利用CAN_H和CAN_L线的电位差传输数据,显性电平(0,2.5V差值)对应逻辑0,隐性电平(1,0V差值)对应逻辑1。由于NRZ无返回零通信方式,同步是个挑战,特别是距离远时。为解决同步问题,CAN总线采用硬件同步和再同步技术,位时序分为同步段、传播段、两个相位缓冲段,每个段由Tq时间量子构成,允许调整以确保多个单元间的同步采样。
911 0
|
Linux
中断系列第二篇:中断的使用场景?
中断系列第二篇:中断的使用场景?
767 0
|
安全 算法 网络安全
一篇文章让你彻底弄懂SSL/TLS协议
一篇文章让你彻底弄懂SSL/TLS协议
一篇文章让你彻底弄懂SSL/TLS协议
|
安全 Linux iOS开发
Binary Ninja 5.2 发布 - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.2.8614 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
748 0
Binary Ninja 5.2 发布 - 反编译器、反汇编器、调试器和二进制分析平台
|
存储 数据库 索引
B-Tree和B+Tree的区别及各自的优势
B-Tree和B+Tree的区别及各自的优势
1398 0
|
Web App开发 移动开发 JavaScript
纯CSS3+SVG实现的节日庆祝五彩纸屑动画效果源码
这是一款基于纯CSS3+SVG实现的节日庆祝五彩纸屑动画效果源码。画面中左下角是一个圆锥形礼炮卡通效果,呈现出节日庆祝时礼花爆破、五彩纸屑纷飞的动画特效。整体动画效果采用纯css3+svg实现,没有引入任何外部图形或js脚本元素。建议使用支持HTML5与css3效果较好的火狐(Firefox)或谷歌(Chrome)等浏览器预览本源码。
510 6
|
前端开发 流计算
css:text-decoration给文字增加上划线、删除线、下划线
css:text-decoration给文字增加上划线、删除线、下划线
3880 0
css:text-decoration给文字增加上划线、删除线、下划线
|
算法 C语言
C语言中的文件操作技巧,涵盖文件的打开与关闭、读取与写入、文件指针移动及注意事项
本文深入讲解了C语言中的文件操作技巧,涵盖文件的打开与关闭、读取与写入、文件指针移动及注意事项,通过实例演示了文件操作的基本流程,帮助读者掌握这一重要技能,提升程序开发能力。
1139 3
|
SQL 运维 监控
Nest.js 实战 (十):使用 winston 打印和收集日志记录
这篇文章介绍了在Nest服务中如何使用Winston记录日志。文章首先强调了日志记录在后台服务中的重要性,接着提到Nest默认的内部日志记录器,并指出可以通过@nestjs/common包中的Logger类来全面控制日志系统的行为。文章还提到,为了在生产环境中实现更高级的日志功能,可以使用如Winston之类的Node.js日志包。接下来,文章介绍了如何在Nest服务中使用Winston记录日志,包括安装相关依赖、创建winston配置文件以及实现简单的日志记录示例。最后,文章指出更高级的自定义日志功能需要读者自己去探索。
1082 0
Nest.js 实战 (十):使用 winston 打印和收集日志记录