需要协助Vigenere Cipher计划

时间:2018-03-16 05:21:19

标签: c++

我正在帮助朋友创建一个使用" Vigenere Cipher加密/解密消息的程序。"我不确定这是什么,所以我做了自己的研究,并认为我已经弄清楚了。

从语法角度来看,我的代码运行良好。但是,从逻辑的角度来看,它不起作用。根据我的理解,当我使用密钥加密消息时,如果我使用相同的密钥解密加密的消息,它应该给我原始消息。我没有。根据我的调试尝试,我认为问题出在我的解密算法中,但可能完全错误。

这是我的代码:

#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.\n\n\n";
    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 << "\n\nEncrypted 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 << "\n\nOriginal 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;
}

任何帮助?

1 个答案:

答案 0 :(得分:1)

由于您打算使用长度为26的字母,因此在执行任何加密/解密操作之前,您需要确保输入已正确规范化。

我建议确保用户输入消息&amp;密钥转换为大写。 例如利用toupperpublic virtual DbSet<UserAccount> UserAccount { get; set; } public CoreContext(DbContextOptions<CoreContext> options) : base(options) { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(@"Connection String"); } } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<UserAccount>(entity => { entity.Property(e => e.Account).IsRequired(); entity.Property(e => e.Pwd) .IsRequired() .HasMaxLength(20); }); }

你能否提供你的意见和建议?输出?你的代码工作正常。

for(char &c : inputMessage) c = toupper(c)