C++ 如何制作一个程序,使用特定的单词对消息进行加密和解密?

C++ 如何制作一个程序,使用特定的单词对消息进行加密和解密?,c++,C++,所以现在我试着读入并填充文本,它给出了一个关键字,后面是我必须加密或解密的消息。在读取特定关键字后,我很难理解如何加密和解密消息。同样对于我的关键字,我将其放入一个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 class EncryptDecrypt { p

所以现在我试着读入并填充文本,它给出了一个关键字,后面是我必须加密或解密的消息。在读取特定关键字后,我很难理解如何加密和解密消息。同样对于我的关键字,我将其放入一个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
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);
}
然后使用它,我必须加密或解密来自同一文件的消息。 输入文件如下所示:

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; 
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream fin(“infle.txt”);
字符数组[5][5];
字符串字,关键字,加密,解密;
布尔正确=错误;
而(财务)
{
fin>>单词;
如果(单词==“Z”)
{
word==关键字;
如果(关键字==“幸福”)
{
对于(int行=0;行<5;行++)
{
for(int col=0;col<5;col++)
{
数组[0][0]=“H”;
数组[0][1]=“A”;
数组[0][2]='P';
数组[0][3]=“I”;
数组[行][col];
}
}
对于(int行=0;行<5;行++)
{
for(int col=0;col<5;col++)
{

好吧,我不会帮你做家庭作业,但我会尽力帮助你

首先,我认为你应该把这个问题分解成几个部分

其中一个片段读取文件并提供对发生什么的基本控制。这将成为一个名为run()的方法

当您得到一个“Z”行时,其中一部分会处理它——这是您的关键字

接下来,如果您得到一条E行,则需要调用encrypt()

如果得到D行,则需要调用decrypt()

我们班看起来有点像这样:

  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
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);
}
类加密解密{
私人:
公众:
空心管路(填充翅片){
std::字符串行;
while(while(std::getline(infle,line)){
std::string firstChar=line.substring(0,1);
if(firstChar=='Z'){
makeKey(line.substring(2));
}
else if(firstChar=='E'){
加密(行子字符串(2));
}
else if(firstChar==“D”){
解密(行子字符串(2));
}
}
}
void makeKey(标准::字符串和行){

你需要帮助做什么?你有没有花时间调试?如果没有,你可能需要这样做。确切的问题是什么?我如何制作一个程序,用一个特定的词对消息进行加密和解密?我会使用一个库来实现这一点。你从来没有描述过加密/解密算法是什么。你的代码非常荒谬l、 …并且仅当关键字为“幸福”时才硬编码为有效
,除此之外没有其他功能。您需要使其与任何关键字一起工作,这意味着在字母表中保留一组字母,迭代关键字中的字母并将其从集合中删除,然后将其放入另一个集合,然后用关键字集合中的元素和字母集合中的元素填充2d数组。如Raymond所述说,这个问题缺少对我们应该如何加密或解密每一行的描述。使用您设置的这个2d数组,我一眼就看不到明显的模式。这是一个家庭作业问题还是一个编码挑战还是什么?如果是的话,它可能包含了对所涉及算法的描述,您应该在问题。