gpt4 book ai didi

c++ - 制作凯撒密码,它不想破译消息

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:01:46 28 4
gpt4 key购买 nike

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

我输入了消息,根据我输入的位移/ key 加密消息没有问题(因为我为用户提供了一个选项,让他们随意放置位移)。

但是,问题在于描述消息。它似乎只描述了我输入的倒数第二个或最后一个字母,甚至懒得显示消息的其余字符。

我目前不知道它为什么会这样,我想我必须更改消息以使用 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;
}

最佳答案

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

在您的 msgEncripter 中,您编写 string encriptedMsg = msg;,因为您创建了 mgs 的拷贝,所以 encriptedMsg具有相同的大小。

所以要么你做 string decriptedMsg = msg 要么 string 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

关于c++ - 制作凯撒密码,它不想破译消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58464465/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com