d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Some Help Plz > Encryption
12Next
Add Reply New Topic New Poll
Member
Posts: 4,332
Joined: Mar 31 2011
Gold: 71.00
Jun 13 2012 05:45pm
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!
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 13 2012 07:22pm
What error are you having lol. We are all far to lazy to try to find out what you are doing wrong ourselves so post where your stuck or what errors you are having and we will help lol
Member
Posts: 4,332
Joined: Mar 31 2011
Gold: 71.00
Jun 13 2012 08:18pm
Quote (AbDuCt @ Jun 13 2012 08:22pm)
What error are you having lol. We are all far to lazy to try to find out what you are doing wrong ourselves so post where your stuck or what errors you are having and we will help lol


no errors. i know this is the right formula (at least im pretty sure) im just not positive how to output the Encrypted name...
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 13 2012 08:35pm
I would create a separate function that accepts original character, key, and index and returns the new character.

Treat your input string as an array. Replace each char one by one using this new function. Now you have the new string. cout it.

what's the problem?
Member
Posts: 4,332
Joined: Mar 31 2011
Gold: 71.00
Jun 13 2012 08:43pm
Quote (carteblanche @ Jun 13 2012 09:35pm)
I would create a separate function that accepts original character, key, and index and returns the new character.

Treat your input string as an array. Replace each char one by one using this new function. Now you have the new string. cout it.

what's the problem?


im a little lost >.< what would that look like?
thank you
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 13 2012 08:57pm
pseudocode:

for i = 0 to name.size:
name[i] = encrypt(name[i], i, key[i modulo key.size])

print name
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jun 13 2012 11:17pm
Basically he means have your name as a cstring (an array of characters) and to loop through its entire length passing the character at each index to a function along with its position and the key and make it return the encrypted character to replace the original one in the cstring. After you can just cout or printf the string
Member
Posts: 4,332
Joined: Mar 31 2011
Gold: 71.00
Jun 14 2012 09:30pm
im still lost....

Code

for (int i = 0; i < (int) name.length(); i++)
name[i] = singleChar(name[i], i, singleKey[(int) ENCRYPTION_KEY[alphaKey.size]);
cout << name;


What am I doing wrong?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 14 2012 09:51pm
have you tested your singleChar function? what does it print out for each input?
Member
Posts: 4,332
Joined: Mar 31 2011
Gold: 71.00
Jun 17 2012 02:54pm
Quote (carteblanche @ Jun 14 2012 10:51pm)
have you tested your singleChar function? what does it print out for each input?


:wacko:
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll