跳到正文

算法·理论

加密器

WizardCoderMinecraft约 9 分钟
原文

这是 2025年10月25日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2025年7月16日
收录
2025年12月12日
更新
2026年9月25日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

加密器

C++
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <vector>
using namespace std;

// 检查是否为有效的36进制字符
bool isBase36Char(char c) {
	c = toupper(c);
	return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
}

// 将36进制字符转换为数值(0-35)
int charToValue(char c) {
	c = toupper(c);
	if (c >= '0' && c <= '9') return c - '0';
	if (c >= 'A' && c <= 'Z') return 10 + (c - 'A');
	throw invalid_argument("Invalid base36 character");
}

// 大数乘法:num * 36
string multiplyBy36(const string& num) {
	if (num == "0") return "0";
	
	string result;
	int carry = 0;
	
	for (int i = num.size() - 1; i >= 0; i--) {
		int digit = (num[i] - '0') * 36 + carry;
		result.push_back(digit % 10 + '0');
		carry = digit / 10;
	}
	
	while (carry > 0) {
		result.push_back(carry % 10 + '0');
		carry /= 10;
	}
	
	reverse(result.begin(), result.end());
	return result;
}

// 大数加法:num + value
string addValue(const string& num, int value) {
	if (num.empty()) return to_string(value);
	
	string result = num;
	int carry = value;
	
	for (int i = result.size() - 1; i >= 0 && carry > 0; i--) {
		int sum = (result[i] - '0') + carry;
		result[i] = sum % 10 + '0';
		carry = sum / 10;
	}
	
	while (carry > 0) {
		result.insert(result.begin(), carry % 10 + '0');
		carry /= 10;
	}
	
	return result;
}

// 转换36进制数字部分
string convertBase36Segment(const string& segment) {
	string decimal = "0";
	
	for (size_t i = 0; i < segment.size(); i++) {
		decimal = multiplyBy36(decimal);
		int value = charToValue(segment[i]);
		decimal = addValue(decimal, value);
	}
	
	return decimal;
}

// 处理完整字符串,保留非字母数字字符
string processMixedString(const string& input) {
	string result;
	string currentSegment;
	
	for (char c : input) {
		if (isBase36Char(c)) {
			currentSegment += c;
		} else {
			if (!currentSegment.empty()) {
				result += convertBase36Segment(currentSegment);
				currentSegment.clear();
			}
			result += c;
		}
	}
	
	// 处理末尾的36进制段
	if (!currentSegment.empty()) {
		result += convertBase36Segment(currentSegment);
	}
	
	return result;
}

int main() {
	string ciphertext;
	getline(cin, ciphertext);
	try {
		string plaintext = processMixedString(ciphertext);
		cout << "密文: " << plaintext << endl;
	} catch (const invalid_argument& e) {
		cerr << "错误: " << e.what() << endl;
		return 1;
	}
	
	return 0;
}

破密器:

C++
#include <iostream>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <cctype>
using namespace std;

// 验证十进制字符串是否有效
bool isValidDecimal(const string& dec) {
	if (dec.empty()) return false;
	for (string::const_iterator it = dec.begin(); it != dec.end(); ++it) {
		if (!isdigit(*it)) return false;
	}
	return true;
}

// 大数除法,返回商和余数
pair<string, int> divideBy36(const string& num) {
	string quotient;
	int remainder = 0;
	
	for (string::const_iterator it = num.begin(); it != num.end(); ++it) {
		int current = remainder * 10 + (*it - '0');
		if (!quotient.empty() || current >= 36) {
			quotient.push_back(current / 36 + '0');
		}
		remainder = current % 36;
	}
	
	if (quotient.empty()) {
		quotient = "0";
	}
	
	return make_pair(quotient, remainder);
}

// 十进制字符串转三十六进制字符串
string decimalToBase36(const string& decimal) {
	if (!isValidDecimal(decimal)) {
		throw invalid_argument("无效的十进制输入");
	}
	
	if (decimal == "0") return "0";
	
	string num = decimal;
	string result;
	
	while (num != "0") {
		pair<string, int> division = divideBy36(num);
		char c;
		if (division.second < 10) {
			c = '0' + division.second;
		} else {
			c = 'A' + (division.second - 10);
		}
		result.push_back(c);
		num = division.first;
	}
	
	reverse(result.begin(), result.end());
	return result;
}

// 处理混合字符串,保留非数字字母字符
string processMixedInput(const string& input) {
	string result;
	string currentNumber;
	
	for (string::const_iterator it = input.begin(); it != input.end(); ++it) {
		if (isdigit(*it)) {
			currentNumber += *it;
		} else {
			// 处理累积的数字
			if (!currentNumber.empty()) {
				try {
					result += decimalToBase36(currentNumber);
				} catch (const invalid_argument& e) {
					cerr << "错误: " << e.what() << endl;
					return "";
				}
				currentNumber.clear();
			}
			// 添加非数字字符
			result += *it;
		}
	}
	
	// 处理末尾的数字
	if (!currentNumber.empty()) {
		try {
			result += decimalToBase36(currentNumber);
		} catch (const invalid_argument& e) {
			cerr << "错误: " << e.what() << endl;
			return "";
		}
	}
	
	return result;
}

int main() {
	try {
		string ciphertext;
		getline(cin, ciphertext);
		
		string plaintext = processMixedInput(ciphertext);
		if (!plaintext.empty()) {
			cout << "明文: " << plaintext << endl;
		}
		
	} catch (const exception& e) {
		cerr << "错误: " << e.what() << endl;
		return 1;
	}
	
	return 0;
}