因为不想做题摸出个好玩的东西

Laffey 2022-05-25 9:37:19 2022-12-05 18:56:27

准确来讲算是一个简单的编码加密和解密。效果如图:

image.png

具体编码原理是将输入的文本转化为 01 串,然后每隔一位取出一个二进制位,将它们串在一起作为密钥对原文本进行凯撒密码加密,最后将密钥取反拼在原文本前面,将最后得到的串用 awa 代表 0 qwq 代表 1 输出。

解码的话就是先取密文前三分之一部分取反得到密钥,再对后三分之二部分进行凯撒密码解密后输出。

(可以用来加密通话

附:编码和解码程序的源码。

#include <iostream>
#include <cstring>
using namespace std;

int len;
char c[1002];
int a_len;
bool a[6010];
int key_len;
bool key[3010];

void init()
{
    memset(c, 0, sizeof c);
    memset(a, 0, sizeof a);
    memset(key, 0, sizeof key);
    a_len = key_len = len = 0;
}

void encode()
{
    init();
    cin >> c;
    len = strlen(c);
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < 8; j++) {
            a[++a_len] = (c[i] >> j) & 1;
            if (!(j & 1))
                key[++key_len] = a[a_len];
        }
    }
    for (int i = 1; i <= key_len; i++) {
        if (key[i]) {
            cout << "awa";
        }
        else {
            cout << "qwq";
        }
    }
    for (int i = 1, j = 1; i <= a_len; i++, j++) {
        if (j > key_len) {
            j = 1;
        }
        a[i] ^= key[j];
        if (a[i]) {
            cout << "qwq";
        }
        else {
            cout << "awa";
        }
    }
    cout << endl;
}

void decode()
{
    init();
    bool temp[5000];
    int tlen = 0;
    char t;
    while (cin.get(t) && t != '\n') {
        if (t == 'q') {
            for (int i = 1; i <= 2; i++) {
                cin >> t;
            }
            temp[++tlen] = true;
        }
        else if (t == 'a') {
            for (int i = 1; i <= 2; i++) {
                cin >> t;
            }
            temp[++tlen] = false;
        }
    }
    key_len = tlen / 3;
    for (int i = 1; i <= key_len; i++) {
        key[i] = !temp[i];
    }
    a_len = tlen - key_len;
    for (int i = key_len + 1, j = 1; j <= a_len; i++, j++) {
        a[j] = temp[i];
    }
    for (int i = 1, j = 1; i <= a_len; i++, j++) {
        if (j > key_len) {
            j = 1;
        }
        a[i] ^= key[j];
    }
    for (int i = 1; i <= a_len; i++) {
        int p = i - 1;
        c[p / 8] |= a[i] << (p % 8);
    }
    c[a_len / 8 + 1] = '\0';
    cout << c << endl;
}

int main()
{
    string op;
    while (true) {
        cout << ">";
        cin >> op;
        if (op == "encode") {
            encode();
        }
        else if (op == "decode") {
            decode();
        }
        else if (op == "quit") {
            return 0;
        }
    }
    return 0;
}

感觉可以拿来出题

一定是个毒瘤模拟

2022.5.29 改进:密钥直接随机生成得了,又不需要保证密文唯一,这样破解难度会变高罢(大概

并且现在可以加密带空格的文本力(喜

改进后的编解码程序:

#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <ctime>
using namespace std;

const int MAXN = 1e6 + 10;
int len;
char c[MAXN];
int a_len;
bool a[MAXN];
int key_len;
bool key[MAXN];

void init()
{
    srand(time(0));
    memset(c, 0, sizeof c);
    memset(a, 0, sizeof a);
    memset(key, 0, sizeof key);
    a_len = key_len = len = 0;
}

void encode()
{
    init();
    getchar();
    char t = getchar();
    while (t != '\n' && t != '\r') {
        c[len++] = t;
        t = getchar();
    }
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < 8; j++) {
            a[++a_len] = (c[i] >> j) & 1;
            if (j & 1) {
                key[++key_len] = rand() & 1;
            }
        }
    }
    for (int i = 1; i <= key_len; i++) {
        if (key[i]) {
            cout << "awa";
        }
        else {
            cout << "qwq";
        }
    }
    for (int i = 1, j = 1; i <= a_len; i++, j++) {
        if (j > key_len) {
            j = 1;
        }
        a[i] ^= key[j];
        if (a[i]) {
            cout << "qwq";
        }
        else {
            cout << "awa";
        }
    }
    cout << endl;
}

void decode()
{
    init();
    bool temp[5000];
    int tlen = 0;
    char t;
    while (cin.get(t) && t != '\n') {
        if (t == 'q') {
            for (int i = 1; i <= 2; i++) {
                cin >> t;
            }
            temp[++tlen] = true;
        }
        else if (t == 'a') {
            for (int i = 1; i <= 2; i++) {
                cin >> t;
            }
            temp[++tlen] = false;
        }
    }
    key_len = tlen / 3;
    for (int i = 1; i <= key_len; i++) {
        key[i] = !temp[i];
    }
    a_len = tlen - key_len;
    for (int i = key_len + 1, j = 1; j <= a_len; i++, j++) {
        a[j] = temp[i];
    }
    for (int i = 1, j = 1; i <= a_len; i++, j++) {
        if (j > key_len) {
            j = 1;
        }
        a[i] ^= key[j];
    }
    for (int i = 1; i <= a_len; i++) {
        int p = i - 1;
        c[p / 8] |= a[i] << (p % 8);
    }
    c[a_len / 8 + 1] = '\0';
    cout << c << endl;
}

int main()
{
    string op;
    while (true) {
        cout << "> ";
        cin >> op;
        if (op == "encode") {
            encode();
        }
        else if (op == "decode") {
            decode();
        }
        else if (op == "quit") {
            return 0;
        }
    }
    return 0;
}

2022.12.5 注:开大了内存空间以支持更长的字符串编解码,顺手给提示符加了个空格上去。