Encryption Program Trouble

Hello! I have been given instructions to write an encrypting and decrypting program that utilizes ONE single function to both encipher and decipher.

The instructions exactly are:
"Create a value returning function that will both encipher and decipher the text. This function will accept two arguments – the plain text and the positional shift value.
Call this function from main() twice – once to encipher the plain text and once to decipher the previously enciphered text. Do NOT store the original plain text to display after the last function call."

I have already figured out how to encrypt the code correctly by adding the positional shift value to the ASCII value, but I cannot figure out how to decrypt the message within the same function without using a second function. Every single method I have tried has ended with an error or simply not made sense after I attempted it. I have already contacted my instructor and all he will tell me is "You add a negative number to the shift value to decrypt the string. In other words, if the shirt value to encrypt is 3, then the shift value for decrypting is -3. It's that simple"

I obviously understand that I need to subtract from the string to decrypt it but I do not know specifically where to do this in the function. When I try, it ends up just returning 2 encrypted strings. Here is the working encryption function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string encrypt(string strText, int ascShift)
{
	int x = 0; //string index counter
	int asciiValue = 0; //holds ASCII character numeric value of current character in string
	string asciiChar = ""; //holds the new shifted ASCII character

	while (x < strText.length()) {
		asciiValue = strText[x];
		asciiValue = asciiValue + ascShift;
		asciiChar = static_cast<char>(asciiValue);
		strText.replace(x, 1, asciiChar);
		++x;
	}
	return strText;

}
Hello Lyreia,

You do this in the function parameter "ascShift". If you send +3 to encrypt then you would sent -3 to decrypt. It is as simple as that. Do not over think it.

And for line 9 all you really need is "asciiValue += ascShift" .

Andy
The encrypt function can be simplified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>

std::string encrypt(std::string strText, int ascShift)
{
	for (size_t x = 0; x < strText.size(); ++x)
		strText[x] += ascShift;

	return strText;
}

int main()
{
	const std::string o = "abcdef";
	const std::string e = encrypt(o, 3);
	const std::string d = encrypt(e, -3);

	std::cout << o << '\n' << e << '\n' << d << '\n';
}


Which displays:


abcdef
defghi
abcdef


Note that a range-for can also be used:

1
2
3
4
5
6
7
std::string encrypt(std::string strText, int ascShift)
{
	for (char& c : strText)
		c += ascShift;

	return strText;
}


However, encrypt() has a potential problem. What happens if adding the shift value to the character makes the character overflow/underflow the range of char?
Last edited on
Topic archived. No new replies allowed.