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.