问题标题: just a代码关于对称加密(带密钥,标点大小写)

1
0
梁辰晖
梁辰晖
修练者
修练者

由于c++原版限制所以先下载个c++加强版---》下载小熊猫c++

代码如下具体操作里面有

一个亮点是要密钥密钥不对解密不出来

虽然仅限英文

but 可以有标点大小写空格

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <algorithm>
using namespace std;
// 加密/解密函数
string xor_crypt(const string& text,const string& key) {
    if (key.empty()) {
        throw invalid_argument("密钥不能为空");
    }
    string result;
    for (size_t i=0;i<text.size();++i) {
        char c=text[i]^key[i%key.size()];
        result+= c;
    }
    return result;
}

// 将字符串转换为十六进制格式
string to_hex(const string& input) {
    stringstream ss;
    ss << hex << setfill('0');
    for (size_t i = 0; i < input.size(); ++i) {
        ss << setw(2)<<static_cast<int>(static_cast<unsigned char>(input[i]));
        if (i < input.size() - 1) {
            ss << " ";
        }
    }
    return ss.str();
}

// 将字符串转换为可显示格式(处理空格和控制字符)
string to_display_format(const string& input) {
    stringstream ss;
    ss << "\"";
    for (unsigned char c : input) {
        if (c == ' ') {
            ss << " "; // 保留空格
        } else if (isprint(c)) {
            ss << c;
        } else {
            // 控制字符使用转义序列
            switch (c) {
                case '\n': ss << "\\n"; break;
                case '\t': ss << "\\t"; break;
                case '\r': ss << "\\r"; break;
                case '\0': ss << "\\0"; break;
            default:
                ss << "\\x" << hex << setw(2) << setfill('0') << static_cast<int>(c);
            }
        }
    }
    ss << "\"";
    return ss.str();
}

// 从十六进制字符串转换回二进制
string from_hex(const string& hex_str) {
    // 移除所有空格
    string clean_hex;
    for (char c : hex_str) {
        if (!isspace(c)) {
            clean_hex += c;
        }
    }
    
    // 验证长度和格式
    if (clean_hex.size() % 2 != 0) {
        throw invalid_argument("十六进制字符串长度必须为偶数");
    }
    
    if (!all_of(clean_hex.begin(), clean_hex.end(), ::isxdigit)) {
        throw invalid_argument("包含无效的十六进制字符");
    }
    
    string result;
    for (size_t i = 0; i < clean_hex.length(); i += 2) {
        string byte = clean_hex.substr(i, 2);
        char c = static_cast<char>(stoul(byte, nullptr, 16));
        result += c;
    }
    return result;
}

// 主程序
int main() {
    cout << "=== 双对称加密解密工具 ===(支持英文大小写空格标点)" << endl;
    cout << "1. 加密文本" << endl;
    cout << "2. 解密文本" << endl;
    cout << "3. 退出程序" << endl;
    
    int choice;
    string input, key;
    
    while (true) {
        cout << "\n请选择操作 (1-3): ";
        cin >> choice;
        cin.ignore(); // 清除输入缓冲区
        
        try {
            switch (choice) {
                case 1: { // 加密
                    cout << "输入要加密的英文文本: ";
                    getline(cin, input);
                    cout << "输入加密密钥: ";
                    getline(cin, key);
                    
                    if (input.empty() || key.empty()) {
                        cout << "错误:文本和密钥均不能为空!" << endl;
                        break;
                    }
                    
                    string encrypted = xor_crypt(input, key);
                    string hex_result = to_hex(encrypted);
                    string display_result = to_display_format(encrypted);
                    
                    cout << "\n加密结果 (十六进制): " << hex_result << endl;
                    cout << "加密结果 (可显示格式): " << display_result << endl;
                    cout << "\n提示: 请复制十六进制结果用于解密" << endl;
                    break;
                }
                
                case 2: { // 解密
                    cout << "输入要解密的文本 (十六进制格式): ";
                    getline(cin, input);
                    cout << "输入解密密钥: ";
                    getline(cin, key);
                    
                    if (input.empty() || key.empty()) {
                        cout << "错误:文本和密钥均不能为空!" << endl;
                        break;
                    }
                    
                    string text_to_decrypt = from_hex(input);
                    string decrypted = xor_crypt(text_to_decrypt, key);
                    
                    cout << "\n解密结果: " << decrypted << endl;
                    break;
                }
                
                case 3: // 退出
                cout << "程序已退出" << endl;
                return 0;
                
            default:
                cout << "无效选择,请重新输入" << endl;
            }
        } catch (const exception& e) {
            cerr << "错误: " << e.what() << endl;
        }
    }
}


0
0
何晟铭
何晟铭
新手守护
新手守护

#include<iostream>

using namespace std;

int main(){

int n;

cin>>n;

int a[105];

for(int i=1;i<=n;i++){

cin>>a[i];

}

int k;

cin>>k;

for(int i=n-k+1;i<=n;i++){

cout<<a[i]<<" ";

}

return 0;

}

0
0
0
骆芃瑀
骆芃瑀
高级守护
高级守护

厉害୧(๑•̀◡•́๑)૭

0
李泓睿
李泓睿
初级守护
初级守护

厉害୧(๑•̀◡•́๑)૭

我要回答