I recently made a C++ program which encrypts texts based-on the vigenere cipher technique.
The encryption part works fine I think, but the decryption function doesn't seem to output the correct answer in some cases. I was hoping if anyone could have a look at code and tell me what's wrong with it.
CMD dialog:
Enter the key for encrytion: magic
Enter message No. 1: I love C programming
Message encrypted: u rwxq i rdomzcymovi
Decrypted: i love c pXogramming
Somehow it outputted "X" instead of "r" in this case.............
Here are the codes:
Secret.h:
#ifndef CPP_TUTORIALS_SECRET_H
#define CPP_TUTORIALS_SECRET_H
#include <iostream>
using namespace std;
class Secret {
private:
string message;
string key;
bool toEncrypt;
bool toDecrypt;
public:
bool isToDecrypt() const {
return toDecrypt;
}
Secret(const string &message = "", const string &key = "", bool toEncrypt = false);
~Secret();
void setToDecrypt(bool toDecrypt);
void encrypt();
void decrypt();
void display();
};
#endif //CPP_TUTORIALS_SECRET_H
Secret.cpp
#include "Secret.h"
Secret::Secret(const string &message, const string &key, bool toEncrypt) {
Secret::message = message;
Secret::key = key;
Secret::toEncrypt = toEncrypt;
}
Secret::~Secret(){
}
void Secret::setToDecrypt(bool toDecrypt) {
Secret::toDecrypt = toDecrypt;
}
void Secret::display() {
cout << message << endl;
}
void Secret::encrypt() {
if(toEncrypt) {
int keyAscii[key.length()];
int count = 0;
for(unsigned int i = 0; i < key.length(); i++) {
keyAscii[i] = key.at(i);
}
for(unsigned int i = 0; i < message.length(); i++) {
if (message.at(i) > 64 && message.at(i) < 91) {
message.at(i) = (char)((message.at(i) - 65 + (keyAscii[count] - 97)) % 26 + 97);
}
else if (message.at(i) > 96 && message.at(i) < 123) {
message.at(i) = (char)((message.at(i) - 97 + (keyAscii[count] - 97)) % 26 + 97);
}
else{
message.at(i) = message.at(i);
}
count++;
if(count == key.length()) {
count = 0;
}
}
}
}
void Secret::decrypt() {
if(toDecrypt) {
int keyAscii[key.length()];
int count = 0;
for(unsigned int i = 0; i < key.length(); i++) {
keyAscii[i] = key.at(i);
}
for(unsigned int i = 0; i < message.length(); i++) {
if (message.at(i) > 96 && message.at(i) < 123) {
message.at(i) = (char)((message.at(i) - 97) % 26 - (keyAscii[count] - 97) + 97);
}
else {
message.at(i) = message.at(i);
}
count++;
if (count == key.length()) {
count = 0;
}
}
}
}
main.cpp
#include <limits>
#include "Secret.h"
void calcMsgAmount(int &pArraySize);
void inputKey(string &key);
void encryptMsg(Secret &secret, string &msg, const string &key, bool toEncrypt, int index);
int main() {
int arraySize;
string key;
string msg;
calcMsgAmount(arraySize);
inputKey(key);
Secret secrets[arraySize];
for(int i = 0; i < arraySize; i++){
encryptMsg(secrets[i], msg, key, true, i);
}
cout << endl << "Message encrypted: " << endl;
for(Secret i: secrets){
i.display();
i.setToDecrypt(true);
if(i.isToDecrypt()){
i.decrypt();
cout << endl << "Decrypted: " << endl;
i.display();
}
cout << endl << endl;
}
return 0;
}
void calcMsgAmount(int &pArraySize) {
cout << "Enter the amount of messages you want to input: " << endl;
while(!(cin >> pArraySize)){
cout << endl << "There's something really wrong with your input, please enter again." << endl;
cout << "Enter the amount of messages you want to input: " << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
void inputKey(string &key){
cout << "Enter the key for encrytion: " << endl;
cin.ignore();
getline(cin, key);
}
void encryptMsg(Secret &secret, string &msg, const string &key, bool toEncrypt, int index){
cout << "Enter message No. " << index + 1 << ": " << endl;
getline(cin, msg);
secret = Secret(msg, key, toEncrypt);
secret.encrypt();
}
Many thanks