d2jsp
Gaming and Trading Community
Gaming and Trading Community
Hourly Raffle
Ladder Slasher
Trade Finder
Photo Gallery
Forum Gold FAQ
Instant Messenger
Help and Rules
Live Streams
Account RecoveryResend Validation Email
Hello, GuestLog InRegister
d2jsp Forums > Programmer's Haven > C/C++/C# > Some Help Plz > Encryption

Add ReplyNew TopicNew Poll
Page 1 of 2 12
BouAnCafe
#1 Jun 13 2012 05:45pm
Group: Members
Posts: 3,098
Joined: Mar 31 2011
Gold: 0.00
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!
AbDuCt
#2 Jun 13 2012 07:22pm
Group: Members
Posts: 8,550
Joined: Sep 29 2007
Gold: 719.06
Warn: 40%
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
BouAnCafe
#3 Jun 13 2012 08:18pm
Group: Members
Posts: 3,098
Joined: Mar 31 2011
Gold: 0.00
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...
carteblanche
#4 Jun 13 2012 08:35pm
Group: Members
Posts: 18,869
Joined: Jul 23 2006
Gold: 2,461.00
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?
BouAnCafe
#5 Jun 13 2012 08:43pm
Group: Members
Posts: 3,098
Joined: Mar 31 2011
Gold: 0.00
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
carteblanche
#6 Jun 13 2012 08:57pm
Group: Members
Posts: 18,869
Joined: Jul 23 2006
Gold: 2,461.00
pseudocode:

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

print name
AbDuCt
#7 Jun 13 2012 11:17pm
Group: Members
Posts: 8,550
Joined: Sep 29 2007
Gold: 719.06
Warn: 40%
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
BouAnCafe
#8 Jun 14 2012 09:30pm
Group: Members
Posts: 3,098
Joined: Mar 31 2011
Gold: 0.00
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?
carteblanche
#9 Jun 14 2012 09:51pm
Group: Members
Posts: 18,869
Joined: Jul 23 2006
Gold: 2,461.00
have you tested your singleChar function? what does it print out for each input?
BouAnCafe
#10 Jun 17 2012 02:54pm
Group: Members
Posts: 3,098
Joined: Mar 31 2011
Gold: 0.00
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 C/C++/C# Topic List
Page 1 of 2 12