Encryption

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 4
Reputation: kekz0r is an unknown quantity at this point 
Solved Threads: 0
kekz0r kekz0r is offline Offline
Newbie Poster

Encryption

 
0
  #1
Oct 18th, 2009
Having some problems creating an encoder for class. I have the decoder, but I can't seem to make my encoder work properly.

I have attached the decoder to the post as a file.

Encryption example: "Hello" would be "xxxxx!H#xxxxxxx#e¤xxx¤l&xxxxxxxxx&l$xxxxxxxxxxx$o", where x is randomly generated letters, and ! is the first key. After the first key comes the first character in the message, and then the next key.

What I am having problems with is simply what to use... I tried using only char[] and loops, but was unable to separate the independent characters in the message when introducing them to the encoded string. I got H, He, Hel, Hell, Hello in the encoded string instead of the single characters. Perhaps I have to use arrays?

For creating new keys, I used
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. srand((unsigned) time(NULL));
  8.  
  9. char c = (char) (rand() % 26 + 'a');
  10. cout << char(c) << endl;
  11. cout << int(c) << endl;
  12.  
  13.  
  14. }

However, I was unable to integrate them into the encryption due to type differance. I managed to go from char to char[], but I got the ASCII number instead of the letter.

Meh. Any suggestions?
Attached Files
File Type: cpp melding.cpp (2.9 KB, 7 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
1
  #2
Oct 18th, 2009
I can't analyze the code you attached since it's in a language I don't know. But here's an example of a very simple encoding technique that simply pads to "encrypt" (not really encryption), and takes out the pads to "decrypt". It uses a character array and isolates characters using the [] operator.

  1. #include <iostream>
  2. #include <cmath>
  3. #include <ctime>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7.  
  8. void encrypt (char* plaintext, char* encrypted)
  9. {
  10. // stick in five random characters for each real character.
  11.  
  12. int length = strlen (plaintext);
  13.  
  14. for (int i = 0; i < length; i++)
  15. {
  16. encrypted[i * 6] = plaintext[i];
  17. for (int j = 1; j < 6; j++)
  18. {
  19. char randomLetter = (rand () % 26) + 'a';
  20. encrypted[i * 6 + j] = randomLetter;
  21. }
  22. }
  23.  
  24. encrypted[length * 6] = 0; // null terminator
  25. }
  26.  
  27. void decrypt (char* plaintext, char* encrypted)
  28. {
  29. // remove random characters
  30.  
  31. int length = strlen (encrypted);
  32.  
  33. for (int i = 0; i < length; i+=6)
  34. {
  35. plaintext[i/6] = encrypted[i];
  36. }
  37.  
  38. plaintext[length / 6] = 0; // null terminator
  39. }
  40.  
  41. int main ()
  42. {
  43. srand (time (NULL));
  44.  
  45. char plaintext[11];
  46. char encrypted[61];
  47.  
  48. cout << "Enter plaintext (length <= 10 characters) : ";
  49. cin >> plaintext;
  50.  
  51. encrypt (plaintext, encrypted);
  52. cout << "Encrypted : " << encrypted << endl;
  53. decrypt (plaintext, encrypted);
  54. cout << "Decrypted : " << plaintext << endl;
  55.  
  56. return 0;
  57. }
Last edited by VernonDozier; Oct 18th, 2009 at 5:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: kekz0r is an unknown quantity at this point 
Solved Threads: 0
kekz0r kekz0r is offline Offline
Newbie Poster

You have my thanks.

 
0
  #3
Oct 18th, 2009
Thanks for the reply. I did not think of using operators inside the brackets, so this helped alot. Now I just have to figure out some way to add a key in front of every character which is part of the message, using the method described in my first post. I would have to add something like this:

Message: SECRET
Output: goakaSogajkoErasjkarCiagkalaiRkagakEqagklqTakgal

Using caps for viewing convenience in this example. The letter in front of the first part of the message, 'S', is a. This is the first key. The letter just after 'S', is 'o'. This is the next key. The next 'o' in the string is followed by the next part of the message, 'E', which is in turn followed by the next key, 'r'. The next 'r' is followed by the next part of the message, 'C', and so on.

I guess I could just use the position of message characters +/- 1 to place these keys, but they would have to follow the pattern AB BC CD DE etc...

Now then, how would I go about implementing this pattern...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,677
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso
 
0
  #4
Oct 18th, 2009
If you know the first key then maybe something like this will work.

Encrypt:
while there are letters to encrypt
generate random letters until current key is generated
place a letter of message
generate random letter to act as next key
repeat

Decrypt---need to know first key
while no further characters in message
find current key
extract a letter of message
find next key
repeat
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 4
Reputation: kekz0r is an unknown quantity at this point 
Solved Threads: 0
kekz0r kekz0r is offline Offline
Newbie Poster

Got it!

 
0
  #5
Oct 19th, 2009
Think I have it now:

  1. #include <iostream>
  2. #include <ctime>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void main()
  7. {
  8. char spam = 'k';
  9. char key = 'r';
  10. string melding;
  11. bool end = false;
  12. unsigned int seed = time(0);
  13. srand(seed);
  14. int i = 0;
  15.  
  16. getline(cin, melding);
  17.  
  18. while(spam != key && end != true)
  19. {
  20. spam = 65 + rand() % 63;
  21. cout << spam;
  22.  
  23. if(spam == key)
  24. {
  25. cout << melding[i];
  26. key = 65 + rand() % 63;
  27. cout << key;
  28. i++;
  29. }
  30. if(melding[i] == '\0')
  31. {
  32. end = true;
  33. }
  34.  
  35. }
  36.  
  37.  
  38. cout << endl << endl;
  39.  
  40.  
  41. }

Confirmed working by my decoder app
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC