involves writing an encryption program in C++. The program uses the following encryption key: "936712845". The encryption algorithm is to go character by character through the name, converting the letter to upper case, and finding the letter or space in this alphabet string: "ABCDEFGHIJKLMNOPQRSTUVWXYZ ". Find the index position of the letter or space in the alphabet string. If a character in the name is not in the alphabet string, display an error message. If the letter in the name is in an even position (we always start counting with 0 in C++), subtract the key, which is 9 for the first letter, from the index position. If the result is less than 0, add that number to 27, which is the length of the alphabet string. If the letter in the name is in the odd position, add the key, which is 3 for the second letter, to the index position. If the result is >= 27, subtract 27 from that number. The encryption key is 9 digits. Once you have gone through the nine digits on the encryption key, start back at index position 0. Here is some code to help you get started.
#include <iostream>
#include <string>
using namespace std;
const string ENCRYPTION_KEY = "936712845";
const int SIZE_ALPHABET = 27;
void main()
{
string name, newName;
int keyIdx, singleKey, singleChar, alphaKey;
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
// convert char digit to a numeric digit
int singleKey = ((int) ENCRYPTION_KEY[keyIdx]) - 48;
Sample Output from Program:
Enter name (blank to quit): Bob
Encrypted Name: TRW
Enter name (blank to quit): 987sdfsdf
ERROR - Invalid character in name
Enter name (blank to quit): Susan
Encrypted Name: JXMHM
Enter name (blank to quit): Thomas Jefferson
Encrypted Name: KKIT USN OCKKTMV
Enter name (blank to quit): Abraham Lincoln
Encrypted Name: SELHGCEDGRKIHML
Enter name (blank to quit): DeVry University
Encrypted Name: VHPYXBMRDDBXLJRF
So far what I have:Code
#include <iostream>
#include <string>
using namespace std;
const string ENCRYPTION_KEY = "936712845";
const int SIZE_ALPHABET = 27;
void main()
{
string name, newName;
int keyIdx, singleKey, singleChar, alphaKey;
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
while (true)
{
cout << "Enter name (Blank to quit): ";
getline(cin, name);
if ((int) name.length()==0)
break;
for (int i = 0; i < (int) name.length(); i++)
name [i] = toupper(name[i]);
newName = "";
keyIdx = 0;
for (int i = 0; i < (int) name.length(); i++)
{
alphaKey = (int) alphabet.find(name[i]);
if(alphaKey == (int) string::npos)
{
newName = "";
cout << "ERROR - Invalid character in name" << endl;
break;
}
int singleKey = ((int) ENCRYPTION_KEY[keyIdx]) - 48;
keyIdx %= 9;
if(i % 2 == 0)
singleChar = alphaKey - singleKey;
else
singleChar = alphaKey + singleKey;
if(singleChar >= SIZE_ALPHABET)
singleChar = singleChar - SIZE_ALPHABET;
else if(singleChar < 0)
singleChar = SIZE_ALPHABET + singleChar;
}
}
}
What am I missing? Any help would be appreciated!