需要Vigenere Cipher程序的帮助

Need assistance with Vigenere Cipher program

本文关键字:帮助 程序 Cipher Vigenere 需要      更新时间:2023-10-16

我正在帮助一个朋友创建一个使用"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