【问题标题】:How do I make a program where it encrypts and decrypts a message using a specific word?如何制作一个使用特定单词加密和解密消息的程序?
【发布时间】: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++


【解决方案1】:

好的,我不会为你做作业,但我会尽力帮助你解决问题。

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

一块读取文件并提供对发生的事情的基本控制。这变成了一个名为 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);
}

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

从那里,实现我没有实现的三个方法。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 2020-11-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多