String encoding/decoding

This is what i intend to do:
Ask the user to enter any string, encrypt it with rot13(shift character 13 places).
All characters are permitted.
After every fifth character, change key to rot7.
Decrypt text, print both encrypted and decrypted on screen.

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

using namespace std;

int main()
{
	// KEYS TO BE USED
	const int ROT13 = 13, ROT7 = 7;

	cout << endl << " Enter a string of text. Press ENTER when finished. " << endl << endl;
	cout << " Text: ";

	string text;
	getline(cin, text);

	string encText = text; // text to encrypt
	
	for (size_t i = 0; i < encText.size(); i++)
	{
		encText[i] = encText[i] + ROT13;

		if (encText[i] % 5 == 0)
			encText[i] = encText[i] + ROT7;
		//encText;  // Add or removing this had no effect
	}

	cout << endl << "Encrypted text is " << endl << encText << endl;
	string decText = encText;  // Text to decrypt

	for (size_t i = 0; i < decText.size(); i++)
	{
		decText[i] = decText[i] - ROT13;

		if (decText[i] % 5 == 0)
			decText[i] = decText[i] - ROT7;
		//decText;
	}

	cout << endl << "Decrypted text is " << endl << decText << endl << endl;
	return 0;
}


This is what i get

 Enter a string of text. Press ENTER when finished.
 Text: Hello world
Encrypted text is
\ryy|4รค|yq

Decrypted text is
Oello'worl]


Where did i go wrong? When i comment out the "change key" part, it works well with just one key.
Last edited on
Hello @blongho

I think you are doing two separate things wrong (possibly three - see the comment at the bottom).

Firstly, you are testing that the 'character' is divisible by 5, not its position. So your lines 23 and 35 should both start
if (i % 5 == 0) ...

Secondly, you are encoding and decoding every 5th character TWICE (because you have already done ROT13 on everything). If that wasn't intended, you should use an
if ... else if 
construct here; say
1
2
		if (i % 5 == 0) ...code for ROT7;
                else               ... code for ROT13;


Get this to work and check your encryption by hand.

I'm assuming that 'ROT13' simply means translating by 13 and not cycling back to the start of the alphabet when you get to 'z' or 'Z', say. You should check this with your assessor, because ROT could be standing for ROTATION.

Hope that helps.
Last edited on
@lastchance, do mean something like
1
2
3
4
5
6
7
8
for (size_t i = 0; i < encText.size(); i++)
	{
		
		if ((i+1)% 5 == 0)
			encText[i] = encText[i] + ROT7;
		else
			encText[i] = encText[i] + ROT13;
	}

lastchance wrote:
You should check this with your assessor, because ROT could be standing for ROTATION.

In this context, rot13 means shifting character 13 steps ahead.
Last edited on
Yes, I guess so. Since they start from 0, using i+1 would be more correct than i alone; you are correct.

Make sure that a similar thing is done at the decryption stage.

If this works you might even consider reducing it to a single line with a ternary expression. That would impress your assessor.
Thanks @lastchance.

This was the magic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int key = ROT13;
	
	for (size_t i = 0; i < encText.size(); i++)
	{
		encText[i] = encText[i] + key;

		if ((i + 1) % 5 == 0)
		{
			if (key == ROT7)
				key = ROT13;
			else
				key = ROT7;
		}
	}
Topic archived. No new replies allowed.