Write a function that accepts a password string “WasillaDogSleddingRulz” and per
ID: 3631209 • Letter: W
Question
Write a function that accepts a password string “WasillaDogSleddingRulz” and perform a (very) weak encryption: For each character in the string, change the character to some other garbage character in the ASCII table by performing two consecutive, reversible math operations on it. Be sure the values do not go below 0 or above 127.Write a second function that accepts your 'encrypted' string and reverses the encryption. This function will output another character array such that all the characters are the same as the original string. Include in your main method iterators that output the characters at each index of the three strings for comparison.
Explanation / Answer
//Header file section
#include<iostream>
#include<string>
using namespace std;
//main function
int main()
{
//variable declartion
string message;
string::iterator myIterator;
//Prompt the user for data and read the data from the user
cout << " Enter your choice of a string : ";
getline(cin, message);
myIterator = message.begin();
while ( myIterator != message.end() )
{
if ( *myIterator > 64 && *myIterator < 91 )
*myIterator = ( ( *myIterator - 65 + 13 ) % 26 ) + 65 ;
if ( *myIterator > 96 && *myIterator < 123 )
*myIterator = ( ( *myIterator - 97 + 13 ) % 26 ) + 97 ;
myIterator++;
}
//print the encrypted message
cout << " The encrypted message is " << message;
//move the iterator to the beginning of the message
myIterator = message.begin();
while ( myIterator != message.end() )
{
if ( *myIterator > 64 && *myIterator < 91 )
*myIterator = ( ( *myIterator - 65 + 13 ) % 26 ) + 65 ;
if ( *myIterator > 96 && *myIterator < 123 )
*myIterator = ( ( *myIterator - 97 + 13 ) % 26 ) + 97 ;
myIterator++;
}
//print the decrypted message
cout << " The decrypted message is " << message;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.