-2

So now i'm trying to read in and infile text which gives a keyword followed by messages that I have to either encrypt or decrypt. I'm having trouble figuring out how to encrypt and decrypt the messages after reading in the specific keyword. Also for my keyword, I put it into a 5x5 2D array that excludes the letter 'Z'. I can only have one of each letter in the 2D array then prints out the rest of the alphabet. So if the word was 'HAPPINESS' it would look like this:

  0 1 2 3 4
0 H A P I N
1 E S B C D
2 F G J K L
3 M O Q R T
4 U V W X Y

Then using this, I would have to encrypt or decrypt a message coming from the same file. The input file looks like this:

Z HAPPINESS
E hello there
D HAWWC XHARA
E attack at dawn
D IAAX IA NUVAR HEIIARSIMXH GRMVBA
E the meeting is in san francisco
D XHMS MUPCRIEXMCU MS AUORYFXAV NSMUB XHA QAYLCRV HEFFMUASS
D XHA EUSLAR XC XHA PMRSX KNASXMCU CU XHA PMUEW LMWW GA XRNA
D OCUBREXNWEXMCUS YCN IEVA MX XHRCNBH XHMS IEOHMUA FRCGWAI

Where 'Z' represents the keyword and 'E' represents encrypt and 'D' represents decrypt.

To encrypt the message:
1. Each letter in the message will be found in the table and the row and column will be noted: e.g. 'g' (when coverted to uppercase) occurs at row 2 column 1 in the above array.
2. It will then be encrypted by reversing the row and column values, so that 'g' will become the character in row 1 column 2 i.e. 'B' in the encrypted message.
So if the was "good luck" it will be encrypted as "BCCV WNOQ" and spaces should be maintaing exactly as they appear.
Then decrypting the message uses the same algorithm but uses changes the incoming message to uppercase instead of lowercase.

So I was only able to make a code for when the keyword is inputted but what do I do to encrypt or decrypt this?

This is what I have so far:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
    ifstream fin("infile.txt");
    char array[5][5];
    string word, keyword, encrypt, decrypt;
    bool correct = false;

    while (fin)
    {
        fin >> word;
        if (word == "Z")
        {
            word == keyword;

            if (keyword == "HAPPINESS")
            {
                for (int row = 0; row < 5; row++)
                {
                    for (int col = 0; col < 5; col++)
                    {
                        array[0][0] = 'H';
                        array[0][1] = 'A';
                        array[0][2] = 'P';
                        array[0][3] = 'I';
                        array[row][col];
                    }
                }
                for (int row = 0; row < 5; row++)
                {
                    for (int col = 0; col < 5; col++)
                    {
                        cout << array[row][col] << " ";
                    }
                    cout << endl;
                }
            }
       }
    }
    return 0; 
}

I might have done something wrong in my coding too but this is what I have.

  • 1
    What part do you need help with? Have you spent time debugging? If not you probably need to do so. What is the exact question? – drescherjm Dec 02 '19 at 21:14
  • Yes I have. That's why I'm on here now. How do I write a code that encrypts and decrypts what is in the infile? – Jason Smith Dec 02 '19 at 21:17
  • 1
    ***How do I make a program where it encrypts and decrypts a message using a specific word?*** I would use a library for that. [https://stackoverflow.com/questions/180870/what-is-the-best-encryption-library-in-c-c](https://stackoverflow.com/questions/180870/what-is-the-best-encryption-library-in-c-c) – drescherjm Dec 02 '19 at 21:18
  • What do you mean? – Jason Smith Dec 02 '19 at 21:18
  • 2
    You never described what the encryption/decryption algorithm is. – Raymond Chen Dec 02 '19 at 21:21
  • Oh sorry. Its supposed to be a cryptography – Jason Smith Dec 02 '19 at 21:23
  • 2
    Your code is quite nonsensical... and is hardcoded to only work if the keyword is exactly `"HAPPINESS"` and nothing else. You'll want to make it work with any keyword, which means keeping a set of letters in the alphabet, iterating over letters in the keyword and removing them from the set and putting them in another set, and filling your 2d array with the elements of the keyword set followed by elements from the alphabet set. – Klaycon Dec 02 '19 at 21:24
  • 2
    And as Raymond says, this question is missing a description of *how* we're supposed to encrypt or decrypt each line. I can see no obvious pattern at first glance using this 2d array you've set up. Is this a homework question or a coding challenge or something? If so it's likely to have included a description of the algorithm involved, which you should share in the question. – Klaycon Dec 02 '19 at 21:25
  • Okay Klaycon. I editted it to show what algorithm to use – Jason Smith Dec 02 '19 at 21:37

1 Answers1

2

Okay, I'm not going to do your homework for you, but I'm going to try to help with the approach.

First, I think you should break this problem up into pieces.

One piece reads the file and provides basic control over what happens. This becomes a method called run().

One piece handles it when you get a "Z" line -- which is your keyword. Call this method makeKey() and it's called from run().

Next, if you get an E line, you need to call encrypt().

If you get a D line, you need to call decrypt().

So our class looks a little like this:

class EncryptDecrypt {
private:

public:
    void run(infile fin) {
        std::string line;
        while (while (std::getline(infile, line)) {
            std::string firstChar = line.substring(0, 1);
            if (firstChar == 'Z') {
                makeKey(line.substring(2));
            }
            else if (firstChar == 'E') {
                encrypt(line.substring(2));
            }
            else if (firstChar == 'D') {
                decrypt(line.substring(2));
            }
        }
    }
    void makeKey(std::string &line) {
        std::cout << "makeKey received: " << line << std::endl;
    }
    void encrypt(std::string &line);
        std::cout << "encrypt received: " << line << std::endl;
    }
    void decrypt(std::string &line);
        std::cout << "encrypt received: " << line << std::endl;
    }

};

int main(int argc, char **argv) {
    ifstream fin("infile.txt");
    EncryptDecrypt encryptDecrypt;

    encryptDecrypt.run(fin);
}

From here, you have to add to your class the variables necessary to hold your key. That's probably an extract from what you already have written.

From there, implement the three methods I haven't implemented.

This is showing you how to break down the problem into manageable pieces that you can work on one at a time. Each method then is really quite short, so you have less you need to think about.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36