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;
}
相关文章
|
安全 编译器 C语言
深入理解C/C++预处理器指令#pragma once以及与ifndef的比较
深入理解C/C++预处理器指令#pragma once以及与ifndef的比较
1300 0
|
安全 JavaScript 前端开发
浅谈 REST API 身份验证的四种方法
在平时开发中,接口验证是必须的,不然所有人都能请求你的接口,会带来严重的后果,接口验证一般有四种方法
5632 0
浅谈 REST API 身份验证的四种方法
|
8月前
|
安全 Linux iOS开发
Binary Ninja 5.2 发布 - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.2.8614 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
571 0
Binary Ninja 5.2 发布 - 反编译器、反汇编器、调试器和二进制分析平台
|
机器学习/深度学习 Python
在Python中监听变量值的变化
在Python中监听变量值的变化
1399 2
|
机器学习/深度学习 传感器 人工智能
AI与环境保护:可持续发展的伙伴
在科技日新月异的时代,人工智能(AI)不仅改变了我们的生活和工作方式,还在环保和可持续发展领域发挥重要作用。AI通过环境监测、资源优化、垃圾分类、绿色出行和环保教育等多方面的应用,为环保事业注入新活力,推动社会向更加绿色、可持续的方向发展。
|
算法 安全 JavaScript
安全哈希算法:SHA算法
安全哈希算法:SHA算法
1168 1
安全哈希算法:SHA算法
|
机器人
ROS2教程 04 话题Topic
本文是关于ROS2(机器人操作系统2)中话题(Topic)机制的教程,详细介绍了ROS2中话题的命令使用,包括列出、回显、发布、信息查询、类型查询等功能,并通过示例代码展示了如何创建发布者(Publisher)和订阅者(Subscriber)节点,以及如何测试发布-话题-订阅通信。
3403 1
ROS2教程 04 话题Topic
|
Python
在python终端中打印颜色的3中方式(python3经典编程案例)
这篇文章介绍了在Python终端中打印彩色文本的三种方式:使用`colorama`模块、`termcolor`模块和ANSI转义码。
929 8
|
算法 数据安全/隐私保护 Python
数字签名是一种用于验证数据完整性和来源身份的技术。它基于公钥密码学,允许数据的发送方使用其私钥对数据进行签名,而接收方则可以使用发送方的公钥来验证签名的有效性。
数字签名是一种用于验证数据完整性和来源身份的技术。它基于公钥密码学,允许数据的发送方使用其私钥对数据进行签名,而接收方则可以使用发送方的公钥来验证签名的有效性。

热门文章

最新文章