943,576 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 453
  • C++ RSS
Feb 5th, 2009
0

Encryption application

Expand Post »
Hi there! I'm working on this app that will allow you to encrypt or decrypt simple lines or entire txt files. It's no "real" encryption since it's easy to crack the output lines, but it's a fun project. So far I'm just done with the first option, and the funtions required to encrypt a simple line.

It seems to run fine long as the entered line is not longer than the password. If it is, I get the following error:
Unhandled exception at 0x7c812aeb in Encryptor.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0012fb4c..

Also, if numbers are entered in the line, it seems like it wont be possible to decrypt them to numbers again. If the line "12" is entered using the pass "111111" it will encrypt into "bc". Does this have anything to do with how I handle the data? Should I use char arrays instead of strings?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <conio.h>
  5. using namespace std;
  6.  
  7. string lineInput ();
  8. string createPassword ();
  9. string encryptLine (string line, string password);
  10.  
  11.  
  12. int main()
  13. {
  14. bool loopHold = false;
  15. string menuChoice, tempLine, tempPassword;
  16.  
  17. do {
  18. cout << "Welcome, do you wish to:\n"
  19. "1 - Encrypt\n"
  20. "2 - Decrypt\n"
  21. "3 - Exit\n" << endl;
  22. getline(cin, menuChoice);
  23.  
  24. if (menuChoice == "1") {
  25. system("cls");
  26.  
  27. do {
  28. cout << "Options:\n"
  29. "1 - Encrypt a text string\n"
  30. "2 - Encrypt a text string and save it to a file\n"
  31. "3 - Encrypt a target file and save it to another\n"
  32. "4 - Back to previous menu\n" << endl;
  33. getline(cin, menuChoice);
  34.  
  35. if (menuChoice == "1") {
  36. system("cls");
  37. cout << "Enter the string to be encrypted, finish with #" << endl;
  38. tempLine = lineInput();
  39. tempPassword = createPassword();
  40. cout << encryptLine(tempLine, tempPassword) << endl;
  41. _getch();
  42. }
  43.  
  44. else if (menuChoice == "4") {
  45. system("cls");
  46. break;
  47. }
  48. system("cls");
  49. } while (loopHold);
  50. }
  51.  
  52. else if (menuChoice == "3") {
  53. return 0;
  54. }
  55.  
  56. system("cls");
  57. } while (loopHold);
  58.  
  59. return 0;
  60. }
  61.  
  62. string lineInput ()
  63. {
  64. string tempLine;
  65. char in;
  66. while (cin.get(in) && in != '#') {
  67. tempLine += in;
  68. }
  69. return tempLine;
  70. }
  71.  
  72. string createPassword ()
  73. {
  74. cin.ignore(1000, '\n');
  75. string tempPassword, confirmPassword;
  76. bool loopHold;
  77.  
  78. do {
  79.  
  80. do {
  81. cout << "Enter desired password (min. 6 characters): ";
  82. getline(cin, tempPassword);
  83.  
  84. if (tempPassword.size() < 6) {
  85. cout << "You must enter a longer password!" << endl;
  86. loopHold = true;
  87. }
  88. else {
  89. loopHold = false;
  90. }
  91. } while (loopHold);
  92.  
  93. cout << "Confirm the password: ";
  94. getline(cin, confirmPassword);
  95.  
  96. if (confirmPassword != tempPassword) {
  97. cout << "The passwords do not match!" << endl;
  98. loopHold = true;
  99. }
  100. else {
  101. loopHold = false;
  102. }
  103. } while (loopHold);
  104.  
  105. return confirmPassword;
  106. }
  107.  
  108. string encryptLine (string line, string password)
  109. {
  110. string encryptedLine;
  111.  
  112. for (unsigned pos = 0, pos2 = 0; pos < line.size(); pos ++, pos2 ++) {
  113. encryptedLine += (line.at(pos) + password.at(pos2));
  114.  
  115. if (pos2 > password.size()) {
  116. pos2 = 0;
  117. }
  118. }
  119. return encryptedLine;
  120. }

Any help would be appreciated. Also, constructive comments about my code layout or any way I could make the code more effective would be nice
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheKebab is offline Offline
15 posts
since May 2008
Feb 5th, 2009
1

Re: Encryption application

C++ Syntax (Toggle Plain Text)
  1. encryptedLine += (line.at(pos) + password.at(pos2));
  2.  
  3. if (pos2 > password.size()) {
  4. pos2 = 0;
  5. }
You try to retreive the value at pos2 when pos2 is > than password.size() before you reset pos2 to 0. Also the check should be >= or == since if the length of string is 12, then the pos2 will need to be 13 to reset, yet you only get 0...11 index on your string.

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 5th, 2009
0

Re: Encryption application

Oh that simple, thanks! Working now:
C++ Syntax (Toggle Plain Text)
  1. if (pos2 >= password.size()) {
  2. pos2 = 0;
  3. }
  4. encryptedLine += (line.at(pos) + password.at(pos2));

Anyone knows about the character/number problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheKebab is offline Offline
15 posts
since May 2008
Feb 5th, 2009
0

Re: Encryption application

What exactly is the character problem, '1' + '1' does = 'b'. What is your decode function is it not, 'b'-'1' = '1'.

Or did I miss something?

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 5th, 2009
0

Re: Encryption application

Seems like it was a bug in the old algorithm, working for now. Thanks for the help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TheKebab is offline Offline
15 posts
since May 2008

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: Truble with GetDC()
Next Thread in C++ Forum Timeline: Queue problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC