跳到正文

sdywLYW

1568660

在洛谷打开
资料、奖项与版本

资料

编号
1568660
抓取

陶片放逐

+ 自由发言 · 使用专栏
− 自由发言 · 使用专栏
− 自由发言

查看记录

这个人很懒,什么都没写。

与我聊天可用此程序加密:C++14

C++
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
using namespace std;

string xorEncrypt(const string& input, unsigned int key) {
    string output = input;
    for (size_t i = 0; i < input.size(); ++i) {
        output[i] = input[i] ^ (key & 0xFF);
    }
    return output;
}

string stringToHex(const string& input) {
    stringstream ss;
    for (unsigned char c : input) {
        ss << hex << setw(2) << setfill('0') << (int)c;
    }
    return ss.str();
}

string hexToString(const string& hex) {
    string result;
    for (size_t i = 0; i < hex.length(); i += 2) {
        result.push_back((char)strtol(hex.substr(i, 2).c_str(), nullptr, 16));
    }
    return result;
}

string xorDecrypt(const string& hexInput, unsigned int key) {
    return xorEncrypt(hexToString(hexInput), key);
}

int main() {
    unsigned int key = 1000007;
    string input;
    int choice;
    cout << "1. Encrypt 2. Decrypt 3. File Encrypt 4. File Decrypt: ";
    cin >> choice;
    cin.ignore();
    if (choice == 1 || choice == 3) {
        if (choice == 3) {
            string inputFile, outputFile;
            cout << "Input file name: (no extension)";
            getline(cin, inputFile);
            cout << "Output file name: (no extension)";
            getline(cin, outputFile);
            ifstream fin(inputFile+".txt");
            ofstream fout(outputFile+".txt");
            if (!fin || !fout) {
                cerr << "File open failed!" << endl;
                return 1;
            }
            string line;
            while (getline(fin, line)) {
                fout << stringToHex(xorEncrypt(line, key)) << endl;
            }
            fin.close();
            fout.close();
        } else {
            cout << "Input text: ";
            getline(cin, input);
            cout << "Encrypted result: " << stringToHex(xorEncrypt(input, key)) << endl;
        }
    } else if (choice == 2 || choice == 4) {
        if (choice == 4) {
            string inputFile, outputFile;
            cout << "Input file name: (no extension)";
            getline(cin, inputFile);
            cout << "Output file name: (no extension)";
            getline(cin, outputFile);
            ifstream fin(inputFile+".txt");
            ofstream fout(outputFile+".txt");
            if (!fin || !fout) {
                cerr << "File open failed!" << endl;
                return 1;
            }
            string line;
            while (getline(fin, line)) {
                fout << xorDecrypt(line, key) << endl;
            }
            fin.close();
            fout.close();
        } else {
            cout << "Input hexadecimal: ";
            getline(cin, input);
            cout << "Decrypted result: " << xorDecrypt(input, key) << endl;
        }
    } else {
        cout << "Invalid choice!" << endl;
    }
    return 0;
}
共 1 篇
  1. 休闲·娱乐

    关于码风~求大佬

    代码码风还是比较有意义的。 求各路大佬的码风,也求大佬给我的码风提下建议。 我的码风: 随便拿一个代码展示一下: 本蒟蒻就拿hash为例。 求大佬评价一下。 我的理想码风:看着舒服,然后打比赛时敲代码的效率也快的。