制作凯撒密码,并且它不想解密消息

时间:2019-10-19 14:02:12

标签: c++ visual-studio-code

对于一个项目,我们必须使用类制作一个Caesar Cipher,并将加密后的消息保存在文件中,以便用户可以使用程序对其进行解密。

我输入了消息,并且根据输入的位移/密钥对消息进行加密没有问题(因为我为用户提供了放置位移的选项)。

但是,问题在于解密消息。似乎只抄写了我输入的倒数第二个或最后一个字母,甚至不费心地显示消息的其余字符。

我目前不知道它为什么如此运行,我认为我必须将消息更改为采用char变量而不是字符串,但这将意味着重写大量代码,此刻,我想避免不得不从头开始重写代码。如果没有其他选择,那么我想我将不得不重写代码。

这是代码,(希望能帮到您,如果我的信息看起来很乱,对不起,这是我第一次在此处发布任何内容):

#include<iostream>
#include<string>
#include<fstream>
#include<ctime>
#include<cstdlib>

/* This is a program that will grab a message from the user and encrypt it, then decrypt it 
    It will also generate a random 8-digit character password used to access the encrypted file
    It will also have the ability to allow the user to choose how many spaces the cipher will take into account */

using namespace std;

//Implement a set displacement and get displacement

class Cipherer
{
private:
    int displacement;
    string message;
    //string decryptMessage;

public:
    void setDisplacer(int key);
    int getDisplacer()const;
    void msgEncripter(string, int);
    string getMessage()const;
    void msgDecripter(string);
    string getDecription()const;
};

void Cipherer::setDisplacer(int key)
{
    displacement = key;
}
int Cipherer::getDisplacer()const
{
    return displacement;
}
void Cipherer::msgEncripter(string msg, int key)
{
    string encriptedMsg = msg;
    //.size returns the number of elements
    for (unsigned int i = 0; i < msg.size(); i++)
    {
        if (msg[i] == 32) //32 is the value in ASCII of the space character
        {
            continue;
        }
        else
        {
            if ((msg[i] + key) > 122)
            {
                int temp = (msg[i] + key) - 122;
                encriptedMsg[i] = 96 + temp;
            }
            else if (msg[i] + key > 90 && msg[i] <= 96)
            {
                int temp = (msg[i] + key) - 90;
                encriptedMsg[i] = 64 + temp;
            }
            else
            {
                encriptedMsg[i] += key;
            }
        }
    }
    message = encriptedMsg;
}
string Cipherer::getMessage()const
{
    return message;
}
void Cipherer::msgDecripter(string msg)
{
    string decriptedMsg;

    for (unsigned int i = 0; i < msg.size(); i++)
    {
        if (msg[i] == 32)
        {
            continue;
        }
        else
        {
            if ((msg[i] - displacement) < 97 && (msg[i] - displacement) > 90)
            {
                decriptedMsg[i] = (msg[i] - displacement) + 26;
            }
            else if ((msg[i] - displacement) < 65)
            {
                decriptedMsg[i] = (msg[i] - displacement) + 26;
            }
            else
            {
                decriptedMsg = msg[i] - displacement;
            }
        }
    }
    message = decriptedMsg;
}
string Cipherer::getDecription()const
{
    return message;
}

static const char PASSWORD_POOL[] =
"0123456789";

int poolSize = sizeof(PASSWORD_POOL) - 1;

char getRandChar()
{
    return PASSWORD_POOL[rand() % poolSize];
}

int main()
{
    srand(time(0));
    string pass, input, msg;
    int key;
    Cipherer message;
    ofstream outputFile;
    ifstream inputFile;
    outputFile.open("SecretMSG.txt");
    cout << "Write a message: \n";
    getline(cin, msg);
    cout << "Choose the displacement of the message (0-25): ";
    cin >> key;
    message.setDisplacer(key);
    message.msgEncripter(msg, key);
    outputFile << msg;
    outputFile.close();
    for (int count = 0; count < 1; count++)
    {
        for (int i = 0; i <= 7; i++)
        {
            pass += getRandChar();
        }
        cout << pass << endl;
    }
    cout << "Input password " << pass << " ";
    cin >> input;
    if (input == pass)
    {
        //Make a local variable to read file 
        string encryptedMessage;
        inputFile.open("SecretMSG.txt");
        inputFile >> encryptedMessage;
        inputFile.close();

        cout << message.getMessage() << endl;
        cout << "If you wish to decrypt the message, type in the password once again " << pass << ": ";
        cin >> input;
        if (input == pass)
        {
            message.msgDecripter(encryptedMessage);
            cout << message.getDecription() << endl;
        }
        else
        {
            exit(EXIT_FAILURE);
        }
    }
    else
    {
        exit(EXIT_FAILURE);
    }
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

msgDecripter中,您的string decriptedMsg创建一个大小为0的字符串,因此任何decriptedMsg[i] = is未定义的行为。

在您的msgEncripter中,您写着string encriptedMsg = msg;,并且由于您创建了mgs的副本,因此encriptedMsg的大小相同。

所以您可以string decriptedMsg = msgstring decriptedMsg = std::string(msg.size(), ' ');

但是更像c ++的方法是使用transform

string encriptedMsg = msg;

std::transform(encriptedMsg.begin(), encriptedMsg.end(), encriptedMsg.begin(),
    [](unsigned char c) -> unsigned char { 
      if( c == ' ') {
        return c;
      } else {
        // ...  your other encrypting logic ... 
      }
    });

或使用msg作为源,并使用空字符串作为目标并使用std::back_inserter