Encryption application

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

Join Date: May 2008
Posts: 14
Reputation: TheKebab is an unknown quantity at this point 
Solved Threads: 0
TheKebab's Avatar
TheKebab TheKebab is offline Offline
Newbie Poster

Encryption application

 
0
  #1
Feb 5th, 2009
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?

  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Encryption application

 
1
  #2
Feb 5th, 2009
  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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 14
Reputation: TheKebab is an unknown quantity at this point 
Solved Threads: 0
TheKebab's Avatar
TheKebab TheKebab is offline Offline
Newbie Poster

Re: Encryption application

 
0
  #3
Feb 5th, 2009
Oh that simple, thanks! Working now:
  1. if (pos2 >= password.size()) {
  2. pos2 = 0;
  3. }
  4. encryptedLine += (line.at(pos) + password.at(pos2));

Anyone knows about the character/number problem?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Encryption application

 
0
  #4
Feb 5th, 2009
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 14
Reputation: TheKebab is an unknown quantity at this point 
Solved Threads: 0
TheKebab's Avatar
TheKebab TheKebab is offline Offline
Newbie Poster

Re: Encryption application

 
0
  #5
Feb 5th, 2009
Seems like it was a bug in the old algorithm, working for now. Thanks for the help
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC