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; }