jeudi 3 décembre 2020

Vigenere cipher in c++ with txt file input/output

I've got to make a Vigenere cipher in c++ that inputs plain text/key from txt files, ciphers/deciphers it and outputs the ciphered text into file as well. I am completely green with coding, but i managed to write this:

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) 
{ 


}

on the net i found this piece of code and i think i can sew this together to get it working

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;
}

how can i make it work like this, but on vectors? or what do i do to transform vector into ints for it to work? maybe should i start from scratch using something different? Any help will be greatly apperiecated. Thank you

Aucun commentaire:

Enregistrer un commentaire