【发布时间】:2020-03-27 12:36:48
【问题描述】:
所以现在我正在尝试读取并输入文本,其中给出了一个关键字,然后是我必须加密或解密的消息。在阅读特定关键字后,我无法弄清楚如何加密和解密消息。同样对于我的关键字,我将其放入不包括字母“Z”的 5x5 2D 数组中。我只能在 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;
}
我的编码可能也做错了什么,但这就是我所拥有的。
【问题讨论】:
-
您在哪方面需要帮助?你有没有花时间调试?如果不是,您可能需要这样做。确切的问题是什么?
-
如何制作一个使用特定单词加密和解密消息的程序?我会为此使用库。 https://stackoverflow.com/questions/180870/what-is-the-best-encryption-library-in-c-c
-
你从来没有描述过加密/解密算法是什么。
-
您的代码非常荒谬......并且被硬编码为仅在关键字恰好是
"HAPPINESS"而没有其他情况下才起作用。你会想让它与任何关键字一起工作,这意味着在字母表中保留一组字母,迭代关键字中的字母并将它们从集合中删除并将它们放入另一个集合中,并用元素填充你的二维数组关键字集后跟字母集中的元素。 -
正如雷蒙德所说,这个问题缺少对如何我们应该加密或解密每一行的描述。使用您设置的这个二维数组,乍一看我看不到明显的模式。这是一个家庭作业问题还是编码挑战或其他什么?如果是这样,它很可能包含对所涉及算法的描述,您应该在问题中分享。
标签: c++