Clyde Tamburro
Clyde Tamburro

Reputation: 3

Need assistance with Vigenere Cipher program

I am helping a friend create a program that encrypts/decrypts messages using the "Vigenere Cipher." I was unsure what this was, so I did my own research and think I have it figured out.

My code runs fine from a syntax perspective. But, from a logic perspective, it does not work. From my understanding, when I encrypt a message with a key, if I decrypt the encrypted message using the same key, it should give me the original message. Mine does not. From my debugging attempts, I think the issue lies somewhere in my decrypting algorithm, but could be entirely wrong.

Here is my code:

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>

using namespace std;


int main(){

    //initializing functions to be used
    int giveInfo();
    void encrypt(string message, string key);
    void decrypt(string message, string key);
    void newKey(string key);
    string keyInput();
    string messageInput();
    int userChoice();

    giveInfo();

    //loop so that the user can decrypt/encrypt multiple messages
    int counter = 1;
    int userCounter;

    while (counter == 1){
    int choice = userChoice();

    if(choice == 1){
    string inputMessage = messageInput();
    string inputKey = keyInput();

    encrypt(inputMessage, inputKey);
    }

    else{
    string inputMessage = messageInput();
    string inputKey = keyInput();

    decrypt(inputMessage, inputKey);
    }

    cout << "Would you like to decrypt/encrypt another message? (1 = yes, 2 = no)";
    cin >> userCounter;

    counter = userCounter;

    system("CLS");
}

    return 0;
}



//gives the user a basic description of cypher and what they need to input

int giveInfo(){
    cout << "\nThe Vigenere Cypher is a polyalphabetic encryption/decryption method. It utilizes a 'key' (provided by the user, \nany word of any length) to determine which letters will replace others. This means in order to decrypt a message,\n one will need the key the person who encrypted the messafe used, ensuring a secure encryption. To use this program, \nyou will need to enter your message (this will be converted into all capitol letters) and a key which you would like to use. Do not use any spaces in your message.\n\n\n";
    return 0;
}



string messageInput(){
    //message place holder
    string userMessage;

    //asking for message
    cout << "What is the message you would like to encrypt/decrypt?\n";
    cin >> userMessage;

    return userMessage;
}



string keyInput(){
    //key place holder
    string userKey;

    //asking for key
    cout << "What is the key you would like to use?\n";
    cin >> userKey; 

    return userKey;
}



void decrypt(string message, string key){

    //generating new key to match message length
    int x = message.size();

    for (int i = 0; ; i++)
    {
        if (x == i)
            i = 0;
        if (key.size() == message.size())
            break;
        key.push_back(key[i]);
    }

   string orig_text;

    for (int i = 0 ; i < message.size(); i++)
    {
        // converting in range 0-25
        int x = (message[i] - key[i] + 26) %26;

        // convert into alphabets(ASCII)
        x += 'A';
        orig_text.push_back(x);
    }

    cout << "\n\nEncrypted Code: " + message+ "\n";
    cout << "Key: " + key+ "\n";
    cout << "Decrypted message: ";
    cout << orig_text + "\n";
}



//takes user input (message to be encyrpted and key to be used) as arguments and returns encrypted 
void encrypt(string message, string key){

    string cipher_text;

    //generating new key to match message length
    int x = message.size();

    for (int i = 0; ; i++)
    {
        if (x == i)
            i = 0;
        if (key.size() == message.size())
            break;
        key.push_back(key[i]);
    }

    for (int i = 0; i < message.size(); i++)
    {
        // converting in range 0-25
        int x = (message[i] + key[i]) %26;

        // convert into alphabets(ASCII)
        x += 'A';

        cipher_text.push_back(x);
    }

    cout << "\n\nOriginal message: " + message+ "\n";
    cout << "Key: " + key+ "\n";
    cout << "Encrypted message: ";
    cout << cipher_text + "\n";
}



int userChoice(){
    int choice;

    cout << "Would you like to encrypt a message or decrypt a message? (1 = 
    encrypt, 2 = decrypt)\n";
    cin >> choice;

    return choice;
}

Any help?

Upvotes: 0

Views: 691

Answers (1)

user9382590
user9382590

Reputation:

Since your intention is to use an alphabet with a length of 26, you need to make sure your input is properly normalized prior to performing any encryption/decryption operations.

I would suggest ensuring that user input for the message & key is converted to upper case. e.g. utilizing toupper: for(char &c : inputMessage) c = toupper(c)

Could you provide your input & output? Your code works just fine.

Original Message: ATTACKATDAWN
Key: LEMONLEMONLE
Encrypted Message: LXFOPVEFRNHR

Encrypted Code: LXFOPVEFRNHR
Key: LEMONLEMONLE
Decrypted Message: ATTACKATDAWN

Upvotes: 1

Related Questions