Encryption Algorithm

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.
Last edited on
Your declarations of encrypt and decrypt (11, 12) need to match their definitions (89, 93).

Change
1
2
void encrypt (char key[]);
void decrypt (char code[]);

to
1
2
void encrypt (char key[], int r);
void decrypt (char code[], int r);


your random() function is unnecessary, you should call srand() once, in main and then call rand() each time you need a new random number.

If you're going to "encrypt" by adding a random number, you need a scheme to store that number in, or otherwise recover it from the encrypted file, so you can "decrypt" by subtracting the same number. Since every call to rand() will give you a different number.

considering getMenuSelection() and random() as well, I would suggest you have another look at how pass-by-value works, also declarations vs definitions and function prototypes.

I suggest something like
1
2
3
4
5
6
7
char getMenuSelection ()
{
char m;
cin >> m;
...
return m;
}
2. Would I need to create separate class files for both functions?
You haven't classified anything and so didn't declared any class. So no class files are needed.

If you use encrypt() and decrypt() elsewhere in another file of your project, then you should put their extern declarations into a separate header file which then serves as interface declaration to your implementation.
Topic archived. No new replies allowed.