需要Vigenere Cipher程序的帮助
Need assistance with Vigenere Cipher program
我正在帮助一个朋友创建一个使用"Vigenere密码"加密/解密消息的程序。我不确定这是什么,所以我做了自己的研究,并认为我已经弄清楚了。
从语法的角度来看,我的代码运行良好。但是,从逻辑的角度来看,它不起作用。据我了解,当我用密钥加密消息时,如果我使用相同的密钥解密加密的消息,它应该给我原始消息。我的没有。从我的调试尝试来看,我认为问题出在我的解密算法中的某个地方,但可能是完全错误的。
这是我的代码:
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
using namespace std;
int main(){
//initializing functions to be used
int giveInfo();
void encrypt(string message, string key);
void decrypt(string message, string key);
void newKey(string key);
string keyInput();
string messageInput();
int userChoice();
giveInfo();
//loop so that the user can decrypt/encrypt multiple messages
int counter = 1;
int userCounter;
while (counter == 1){
int choice = userChoice();
if(choice == 1){
string inputMessage = messageInput();
string inputKey = keyInput();
encrypt(inputMessage, inputKey);
}
else{
string inputMessage = messageInput();
string inputKey = keyInput();
decrypt(inputMessage, inputKey);
}
cout << "Would you like to decrypt/encrypt another message? (1 = yes, 2 = no)";
cin >> userCounter;
counter = userCounter;
system("CLS");
}
return 0;
}
//gives the user a basic description of cypher and what they need to input
int giveInfo(){
cout << "nThe Vigenere Cypher is a polyalphabetic encryption/decryption method. It utilizes a 'key' (provided by the user, nany word of any length) to determine which letters will replace others. This means in order to decrypt a message,n one will need the key the person who encrypted the messafe used, ensuring a secure encryption. To use this program, nyou will need to enter your message (this will be converted into all capitol letters) and a key which you would like to use. Do not use any spaces in your message.nnn";
return 0;
}
string messageInput(){
//message place holder
string userMessage;
//asking for message
cout << "What is the message you would like to encrypt/decrypt?n";
cin >> userMessage;
return userMessage;
}
string keyInput(){
//key place holder
string userKey;
//asking for key
cout << "What is the key you would like to use?n";
cin >> userKey;
return userKey;
}
void decrypt(string message, string key){
//generating new key to match message length
int x = message.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == message.size())
break;
key.push_back(key[i]);
}
string orig_text;
for (int i = 0 ; i < message.size(); i++)
{
// converting in range 0-25
int x = (message[i] - key[i] + 26) %26;
// convert into alphabets(ASCII)
x += 'A';
orig_text.push_back(x);
}
cout << "nnEncrypted Code: " + message+ "n";
cout << "Key: " + key+ "n";
cout << "Decrypted message: ";
cout << orig_text + "n";
}
//takes user input (message to be encyrpted and key to be used) as arguments and returns encrypted
void encrypt(string message, string key){
string cipher_text;
//generating new key to match message length
int x = message.size();
for (int i = 0; ; i++)
{
if (x == i)
i = 0;
if (key.size() == message.size())
break;
key.push_back(key[i]);
}
for (int i = 0; i < message.size(); i++)
{
// converting in range 0-25
int x = (message[i] + key[i]) %26;
// convert into alphabets(ASCII)
x += 'A';
cipher_text.push_back(x);
}
cout << "nnOriginal message: " + message+ "n";
cout << "Key: " + key+ "n";
cout << "Encrypted message: ";
cout << cipher_text + "n";
}
int userChoice(){
int choice;
cout << "Would you like to encrypt a message or decrypt a message? (1 =
encrypt, 2 = decrypt)n";
cin >> choice;
return choice;
}
有什么帮助吗?
由于您打算使用长度为 26 的字母表,因此您需要确保在执行任何加密/解密操作之前正确规范化您的输入。
我建议确保消息和键的用户输入转换为大写。例如,利用上层:for(char &c : inputMessage) c = toupper(c)
你能提供你的输入和输出吗? 您的代码工作正常。
Original Message: ATTACKATDAWN
Key: LEMONLEMONLE
Encrypted Message: LXFOPVEFRNHR
Encrypted Code: LXFOPVEFRNHR
Key: LEMONLEMONLE
Decrypted Message: ATTACKATDAWN
相关文章:
- C++调用具有 *this 属性的单个帮助程序函数
- 用于检查值是否为其任何参数的帮助程序函数
- 类型限定宏帮助程序
- CoreCLR 中的检测探查器 - 将帮助程序程序集加载到 dotnet 进程的方法
- 无法将 2D 数组传递到 c++ 中的帮助程序函数中
- C2783: 无法推断帮助程序函数的模板参数
- 友元函数需要一个帮助程序函数
- 在何处放置与类方法相关的帮助程序函数C++
- 给定概念,SFINAE 帮助程序是否仍在规范中作为未弃用?
- 使用 IP 帮助程序的 Windows 中活动 UDP 连接的远程地址
- 如何安装性能数据帮助程序
- 用于向公众公开数据成员的帮助程序结构
- 类参数化与类和帮助程序函数混淆
- 仅具有公共静态方法的帮助程序类
- Ebpf:帮助程序的未定义符号
- 函数和指针帮助:程序生成,但崩溃,没有任何特定错误
- 使用带有 MEX 包装器的帮助程序 C 文件从 MATLAB 2016 调用C++代码时出现问题
- 输入到字符串流并在单个语句中转换为字符串.可以没有帮助程序类
- 使用帮助程序模板结构时无法推断模板参数
- IE 探索 11 < c++ ATL COM 浏览器帮助程序对象(加载项)来替换 DOM 中的文本