Assigning encrypt/decrypt function to an inputted string

I'm attempting to write a function that will encrypt or decrypt an inputted string. A string is declared to represent the alphabet (ALPHABET), and lists all acceptable characters. A permutation of this string is then generated randomly, using a user-defined seed. My intent is for each replaced character of the permutation to then replace the corresponding characters as they occur in the string. I'll simulate a running of the code posted below and the output to illustrate what I mean.


Input seed
1
Input message to decrypt:
Original ALPHABET:        - ABCDEFGHIJKLMNOPQRSTUVWXYZ.,'
Permutation of ALPHABET:  - S'XHYIGEO.DBVPQKLZMCFRWTAN,JU
Inverse of Permutation:   - YLTKHUGDF,PQSZINOVAX'MWCERJ.B

Input any single character to begin
Encryption / decryption, or 'Q' to [Q]uit
a
Type your message below
AMESSAGE
Input 'E' to [E]ncrypt, 'D' to [D]ecrypt:


After this, the message should either be encrypted, using the permutation, which has already been coded, or decrypted, using the inverse of that permutation, also already coded.

Here's what I have so far, and thanks in advance for any assistance. I've tried a dozen things to try and make this work so I'd appreciate any help offered.

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
#include <iostream>
#include <string>
#include <algorithm>
#include <random>

using namespace std;

const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,' ";

string create_permutation(string permutation)
{
	cout << "Input seed" << endl;
	unsigned seed;
	cin >> seed;
	shuffle(permutation.begin(), permutation.end(), default_random_engine(seed));
	return permutation;
}

string findPermu2(const string& permu1)
{
	string permu2(ALPHABET);
	for (size_t i = 0; i<ALPHABET.length(); ++i)
		for (size_t j = 0; j<ALPHABET.length(); ++j)
			if (permu1[i] == ALPHABET[j])
			{
				permu2[j] = ALPHABET[i];
				break;
			}
	return permu2;
} 

string finddecrypted;
/*{
	input message is decrypted via decryption function here
}*/

string findencrypted;
/*{
	input message is encrypted via encryption function here
}*/

int main()
{
	string permu = create_permutation(ALPHABET);
	string invpermu = findPermu2(permu);
	/*string decryptreturn = finddecrypted(permu);*/
	/*string encryptreturn = findencrypted(permu);*/

	cout << "Original ALPHABET:        - " << ALPHABET << endl;
	cout << "Permutation of ALPHABET:  - " << permu << endl;
	cout << "Inverse of Permutation:   - " << invpermu << endl;
	cout << endl << "Input any single character to begin" <<
		endl << "Encryption / decryption, or '\Q'\ to [Q]uit" << endl;

	char enterorquit;
	cin >> enterorquit;
	if (enterorquit == 'q' || enterorquit == 'Q')
	{
		return 0;
	}
	else
	{
		string inputmessage;
		cout << "Type your message below" << endl;
		cin >> inputmessage;
		cout << endl << "Input \'E\' to [E]ncrypt, \'D\' to [D]ecrypt:" << endl;
		char encryptordecrypt;
		if (encryptordecrypt == 'd' || encryptordecrypt == 'D')
		{
			cout << "Decrypted message: " << endl /*<< decryptreturn*/;
		}
		else 
			{
				if (encryptordecrypt == 'e' || encryptordecrypt == 'E')
			{
				cout << "Encrypted message: " << endl /*<< encryptreturn*/;
			}
			else 
			{
				if (encryptordecrypt != 'e' || encryptordecrypt != 'E' || encryptordecrypt != 'd' || encryptordecrypt != 'D')
				cout << "Unrecognised character";
					return 0;
			}
		}
	}
}
Last edited on
Topic archived. No new replies allowed.