新的加密方法——62进制加密法

· · 科技·工程

加密规则

很简单,对吧!

优点

这种方法通过非传统的加密方式,无法通过一一对应的方式破解。

并且两个相似的字符串加密完都会有极大的差别

例:qwert和qnert:

这两个字符串只改变了第二个字符,却导致从密文第七个开始有了区别,并且后面再也没有大面积的重复了。

测试与实操

测试:

实操:

加密: 以十进制为跳板进行加密。

例:(Hi)_ {62} = (2684)_ {10} = (101001111100)_ {2}

解密: 同样以十进制为跳板进行解密。

例:(101001111100)_ {2} = (2684)_ {10} = (Hi)_ {62}

代码:

但是如果加密特别长的文字很容易 把我们累死 导致加密的不正确,所以我设计了下面的两个代码:

加密部分:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int t(char c) {
    if (c >= '0' && c <= '9') {
        return c - '0';
    }
    else if (c >= 'a' && c <= 'z') {
        return 10 + (c - 'a');
    }
    else if (c >= 'A' && c <= 'Z') {
        return 36 + (c - 'A');
    }
    return -1; 
}
char r(int d) {
    if (d >= 0 && d <= 9) {
        return '0' + d;
    }
    else if (d >= 10 && d <= 35) {
        return 'a' + (d - 10);
    }
    else if (d >= 36 && d <= 61) {
        return 'A' + (d - 36);
    }
    return '?'; 
}
string os(string s) {
    int i = 0;
    while (i < s.length() && s[i] == '0') {
        i++;
    }
    if (i == s.length()) {
        return "0";
    }
    return s.substr(i);
}
string y(string s) {
    s = os(s);
    if (s == "0") {
        return "0";
    }
    string bin = "";
    while (s != "0") {
        string quotient = "";
        int rem = 0;
        bool leadingZero = true;
        for (int i = 0; i < s.length(); i++) {
            int digit = t(s[i]);
            if (digit == -1) {
                digit = 0; 
            }
            int current = digit + rem * 62;
            int q = current / 2;
            rem = current % 2;

            if (q != 0 || !leadingZero) {
                quotient += r(q);
                leadingZero = false;
            }
        }
        if (quotient.empty()) {
            quotient = "0";
        }
        bin = string(1, '0' + rem) + bin;
        s = quotient;
    }
    return bin;
}
int main() {
    string input;
    getline(cin, input);

    string num_str = "";
    string result = "";

    for (char c : input) {
        if ((c >= '0' && c <= '9') ||
            (c >= 'a' && c <= 'z') ||
            (c >= 'A' && c <= 'Z')) {
            num_str += c;
        }
        else {
            if (!num_str.empty()) {
                result += y(num_str);
                num_str.clear();
            }
            result += c;
        }
    }
    if (!num_str.empty()) {
        result += y(num_str);
    }
    cout << result << endl;
    return 0;
}

解密部分:

#include <iostream>
#include <string>
#include <cctype>
#include <utility>
#include <algorithm>
using namespace std;
char d(int d) {
    if (d >= 0 && d <= 9) {
        return '0' + d;
    }
    else if (d >= 10 && d <= 35) {
        return 'a' + (d - 10);
    }
    else if (d >= 36 && d <= 61) {
        return 'A' + (d - 36);
    }
    return '?';
}
string mul(string num) {
    string result = "";
    int carry = 0;
    for (int i = num.size() - 1; i >= 0; i--) {
        int digit = num[i] - '0';
        int temp = digit * 2 + carry;
        carry = temp / 10;
        char c = (temp % 10) + '0';
        result = c + result;
    }
    if (carry) {
        result = char(carry + '0') + result;
    }
    return result;
}
string add(string num) {
    string result = num;
    int carry = 1;
    for (int i = result.size() - 1; i >= 0 && carry; i--) {
        int digit = result[i] - '0';
        int sum = digit + carry;
        carry = sum / 10;
        result[i] = (sum % 10) + '0';
    }
    if (carry) {
        result = "1" + result;
    }
    return result;
}
pair<string, int> divide(string num, int divisor) {
    string quotient = "";
    int remainder = 0;
    for (int i = 0; i < num.size(); i++) {
        int digit = num[i] - '0';
        int current = remainder * 10 + digit;
        if (current < divisor) {
            if (!quotient.empty()) {
                quotient += '0';
            }
            remainder = current;
            continue;
        }
        int q = current / divisor;
        remainder = current % divisor;
        quotient += (q + '0');
    }
    if (quotient.empty()) {
        quotient = "0";
    }
    return make_pair(quotient, remainder);
}
string de(string dec) {
    if (dec == "0") {
        return "0";
    }
    string base62 = "";
    string current = dec;
    while (current != "0") {
        pair<string, int> p = divide(current, 62);
        base62 = d(p.second) + base62;
        current = p.first;
    }
    return base62;
}
string bi(string bin) {
    if (bin.empty()) {
        return "0";
    }
    string dec = "0";
    for (int i = 0; i < bin.length(); i++) {
        dec = mul(dec);
        if (bin[i] == '1') {
            dec = add(dec);
        }
    }
    return dec;
}

int main() {
    string input;
    getline(cin, input);
    string bin_str = "";
    string result = "";

    for (char c : input) {
        if (c == '0' || c == '1') {
            bin_str += c;
        }
        else {
            if (!bin_str.empty()) {
                string dec = bi(bin_str);
                string base62 = de(dec);
                result += base62;
                bin_str.clear();
            }
            result += c;
        }
    }
    if (!bin_str.empty()) {
        string dec = bi(bin_str);
        string base62 = de(dec);
        result += base62;
    }
    cout << result << endl;
    return 0;
}