Hi,
I have got to create a program that will gather input from the user and then encrypt & decrypt those characters.
I'm not very confident at coding so I'm sure many parts of my code are written poorly and not following the best practice so I have written a simple version of an algorithm where the program simply adds/subtracts a value of 2 to/from the ASCII values but I have discovered the use of the rand() and srand functions but I'm unsure how to go about using them within both my encrypt and decrypt function as a single value (static variable?).
Here is my code in its entirety at the moment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream >
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
char getMenuSelection (char m);
void encrypt (char key[]);
void decrypt (char code[]);
int main ()
{
ifstream input;
ifstream in;
ofstream out;
char menu;
char message[80];
//char codekey;
//char myname[128];
menu = 0;
menu=getMenuSelection(menu);
if (menu == 'e' || menu == 'E') //Option to Encrypt
{
cout << "Enter Message to be encrypted \n";
cin >> message;
input.open (message);
encrypt (message);
input.close();
cout << "The Ciphertext is: " << message << endl;
return main();
}
if (menu == 'd' || menu == 'D')
{
cout << "Enter Ciphertext to decrypt \n";
cin >> message;
input.open (message);
decrypt (message);
input.close();
cout << "The Decrypted message is: " << message << endl;
return main();
}
if (menu == 'q' || menu == 'Q')
{
cout << "Program exiting.";
cin.ignore();
return 0;
}
cin.ignore();
return 0;
}
char getMenuSelection (char m) // Get the menu function
{
cout << "|||_______Welcome To Elliotts Encryption_______|||\n";
cout << "|||__________and Deciphering Program___________|||\n";
cout << "|||____________________________________________|||\n";
cout << "|||______Please select 'E' to encrypt a________|||\n";
cout << "|||____message, 'D' to decipher a message______|||\n";
cout << "|||______________Or 'Q' to Quit________________|||\n";
cin >> m;
switch (m)
{
case 'e':
case 'E': cout << "Encryption." << endl;
break;
case 'd':
case 'D': cout << "Decryption." << endl;
break;
case 'q':
case 'Q': cout << "Quitting" << endl;
break;
default: cout << "Error, Please try another message. ";
return getMenuSelection(m);
}
return (m);
}
int random (int r)
{
srand(time(0));
r=rand();
return r;
}
void encrypt (char key[], int r) //encryption function.
{
for(int i = 0; i <= 40; i++) key[i] += r;
}
void decrypt (char code[], int r) //decryption function.
{
for(int i = 0; i <= 40; i++) code[i] -= r;
}
|
This code currently doesn't execute due to ' undefined reference to 'encrypt(char*) '
Question
I am asking how to generate the random number and incorporate this into my encrypt and decrypt functions.
1. Would the use of a static or global variable make this work as is?
2. Would I need to create separate class files for both functions?
3. Any other possibilities?
I understand that I will not be given the correct code in full but it would help me greatly if anyone could just point me in the right way or tell me where I'm going wrong.
Thanks, Elliott.