如何制作一个程序,它使用特定单词加密和解密消息?



所以现在我正在尝试读入和归档文本,该文本给出了一个关键字,后跟我必须加密或解密的消息。在阅读特定关键字后,我无法弄清楚如何加密和解密消息。同样对于我的关键字,我将其放入一个 5x5 2D 数组中,不包括字母"Z"。我只能在 2D 数组中的每个字母中有一个,然后打印出字母表的其余部分。因此,如果这个词是"幸福",它看起来像这样:

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

然后使用它,我必须加密或解密来自同一文件的消息。 输入文件如下所示:

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

其中"Z"表示关键字,"E"表示加密,"D"表示解密。

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.

所以我只能在输入关键字时制作代码,但是我该怎么做才能加密或解密呢?

这是我到目前为止所拥有的:

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

我可能在编码中也做错了什么,但这就是我所拥有的。

好吧,我不会为你做功课,但我会尽力帮助解决这个问题。

首先,我认为你应该把这个问题分解成碎片。

一块读取文件并提供对发生的情况的基本控制。这变成了一个名为 run(( 的方法。

当你得到一条"Z"线时,一件会处理它 - 这是你的关键字。调用此方法 makeKey((,它从 run(( 调用。

接下来,如果你得到一个 E 行,你需要调用 encrypt((。

如果你得到一个 D 行,你需要调用 decrypt((。

所以我们的类看起来有点像这样:

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

从这里开始,您必须将保存密钥所需的变量添加到类中。这可能是你已经写过的内容的摘录。

从那里,实现我尚未实现的三种方法。

这向您展示了如何将问题分解为可管理的部分,您可以一次处理一个。然后,每种方法都非常短,因此您需要考虑的更少。

最新更新