944,118 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 361
  • C++ RSS
Oct 18th, 2009
0

Encryption

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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, 16 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kekz0r is offline Offline
4 posts
since Oct 2009
Oct 18th, 2009
1
Re: Encryption
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.

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 18th, 2009
0

You have my thanks.

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kekz0r is offline Offline
4 posts
since Oct 2009
Oct 18th, 2009
0
Re: Encryption
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
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 19th, 2009
0

Got it!

Think I have it now:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kekz0r is offline Offline
4 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: (Matrix Multiplication with Multi-threading)
Next Thread in C++ Forum Timeline: Beginner wonders about C++ interface apps





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC