首页 > 解决方案 > c++ 中的 Vigenere 密码与 txt 文件输入/输出

问题描述

我必须在 C++ 中创建一个 Vigenere 密码,从 txt 文件中输入纯文本/密钥,对其进行密码/解密并将密文输出到文件中。我对编码完全陌生,但我设法写了这个:

char alphabet[32][32] = { 'A','Ą','B','C','D','E','F','G','H','I','J','K','L','Ł','M','N','O','Ó','P','Q','R','S','Ś','T','U','V','W','X','Y','Z','Ź','Ż' };



std::vector<std::string> readPlain(const std::string& filename)
{
    std::string line;
    std::vector<std::string> to_table;
    
    std::ifstream plain (filename);
    if (plain.is_open())
    {   
        while ( getline (plain, line))
        {
            to_table.push_back(line);

        }
        plain.close();
    }
    else std::cout << "cant open file" << std::endl;

    return to_table;
}

std::vector<std::string> readKey(const std::string& key)
{
    std::string line;
    std::vector<std::string> to_table;

    std::ifstream plain(key);
    if (plain.is_open())
    {
        while (getline(plain, line))
        {
            to_table.push_back(line);

        }
        plain.close();
    }
    else std::cout << "cant open file" << std::endl;

    return to_table;
}
void encipher(std::vector<std::string> plaintext , std::vector<std::string> key) 
{ 


}

在网上我找到了这段代码,我想我可以把它缝在一起让它工作

int tableArr[26][26];
void createVigenereTable() {
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            int temp;
            if ((i + 65) + j > 90) {
                temp = ((i + 65) + j) - 26;

                //adding ASCII of alphabet letter in table index position
                tableArr[i][j] = temp;
            }
            else {
                temp = (i + 65) + j;

                //adding ASCII of alphabet letter in table index position
                tableArr[i][j] = temp;
            }
        } // for j loop
    } // for i loop

void cipherEncryption(string message, string mappedKey) {
    createVigenereTable();
    string encryptedText = "";
    for (int i = 0; i < message.length(); i++) {
        if (message[i] == 32 && mappedKey[i] == 32) {
            encryptedText += " ";
        }
        else {
            int x = (int)message[i] - 65;
            int y = (int)mappedKey[i] - 65;
            encryptedText += (char)tableArr[x][y];
        }
    }

    cout << "Encrypted Text: " << encryptedText;
}

我怎样才能让它像这样工作,但是在向量上?或者我该怎么做才能将向量转换为整数才能工作?也许我应该使用不同的东西从头开始?任何帮助都会得到很大的帮助。谢谢

标签: c++c++11

解决方案


推荐阅读