Why does this not work?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 27
Reputation: Kioti16 is an unknown quantity at this point 
Solved Threads: 0
Kioti16 Kioti16 is offline Offline
Light Poster

Why does this not work?

 
0
  #1
Sep 8th, 2009
I am trying to add a user password protection to my program but it does not work it will quit if password is right or wrong.
The file name "setpCS4.dll" is the file the password will be stored in.
Thanks for your help.
Attached Files
File Type: cpp Crypto 1.1.cpp (4.4 KB, 8 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,357
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why does this not work?

 
-7
  #2
Sep 8th, 2009
Your program makes zero sense. Why are you using ofstream to write to a dll ?????
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 27
Reputation: Kioti16 is an unknown quantity at this point 
Solved Threads: 0
Kioti16 Kioti16 is offline Offline
Light Poster

Re: Why does this not work?

 
0
  #3
Sep 8th, 2009
It is not a real .dll file just a .txt file.
Oh and the part I need help with says:
//Problems start here
//<------------------------------------------->
//Problems end here
//<------------------------------------------->

Can you help me, thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,357
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why does this not work?

 
-7
  #4
Sep 9th, 2009
  1. int main(int check) {
  2. char comd;

Actually, the problem starts with the above two lines. main() must have either no parameters or two prameters
int main(int argc, char* argv[]) There are no other options, except possible adding a third parameter which is normally an array of environment strings. And you are NEVER allowed to call main() from within the program. That function is reserved by the compiler to be the entry point into your program.

>> gets(pass1);
NEVER EVER use gets() in either C or C++ programs because it can corrupt your program's memory. It has no checks to see that you can type more characters into the array then the charcater array can hold. In C++ use either cin >> pass1 or cin.getline(pass1, sizeof(pass1));
>>goto over;
Replace that with the break statement. goto in C and C++ is considered to be poor coding practice.
Last edited by Ancient Dragon; Sep 9th, 2009 at 12:07 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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

Re: Why does this not work?

 
0
  #5
Sep 9th, 2009
Originally Posted by Kioti16 View Post
It is not a real .dll file just a .txt file.
Oh and the part I need help with says:
//Problems start here
//<------------------------------------------->
//Problems end here
//<------------------------------------------->

Can you help me, thanks.

Call it a text file then. Anyone seeing a .dll extension expects it to be a DLL file. Anyone seeing .txt expects it to be a text file. Anyone seeing a .csv extension expects a comma separated text file. The same goes for labels.

  1. over:
  2. system("CLS");
  3. cout <<"*-----------------------------------*\n"
  4. <<"| Welcome to Crypto! |\n"
  5. <<"*-----------------------------------*\n";
  6. cout <<"Please type a command.\n"
  7. <<"Type \"m\" for the command menu.\n";
  8. over2:
  9. while(true) {
  10. cin >> comd;
  11. switch(comd) {
  12.  
  13. case 'e' :
  14. system("CLS");
  15. cout <<"Please type name of input file(.txt): ";
  16. cin >> einput;
  17. doCrypt(einput);
  18. break;
  19. break;
  20.  
  21. case 'd' :
  22. system("CLS");
  23. cout <<"Please type name of input file(.txt): ";
  24. cin >> einput;
  25. doDcrypt(einput);
  26. break;
  27. break;
  28.  
  29. case 'm' :
  30. cout <<"\n<COMMAND MENU>\n"
  31. <<"*--------------------*\n"
  32. <<"| e = 'Encrypt' |\n"
  33. <<"| d = 'Decrypt' |\n"
  34. <<"| m = 'Command Menu' |\n"
  35. <<"| q = 'Quit' |\n"
  36. <<"*--------------------*\n";
  37. goto over2;
  38. break;
  39. break;
  40.  
  41. case 'q' :
  42. cout <<"\nThanks For Using 'Crypto'\n";
  43. system("PAUSE");
  44. return 0;
  45. break;
  46. break;
  47.  
  48. default :
  49. goto over;
  50. break;
  51. break;
  52. }
  53. }
  54. }

over? over2? How about DisplayMenu or something?

Anyway, here's an old thread on the same topic where you put in some comments, but then took them out here.

http://www.daniweb.com/forums/thread213717.html

I think people are expecting functions like:

  1. string decrypt (string encryptedMessage, string key);
  2. string encrypt (string plainText, string key);

Sticking code like this:

  1. fileContent[i] -= 0x4f;

with no explanation isn't going to make sense. Some indentation would also make it more readable.

Basically, indentation, comments, good filenames, descriptive labels, and descriptive variables like "key", "plainText", "encryptedText" are necessary if you want people to follow your code.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 27
Reputation: Kioti16 is an unknown quantity at this point 
Solved Threads: 0
Kioti16 Kioti16 is offline Offline
Light Poster

Re: Why does this not work?

 
0
  #6
Sep 9th, 2009
Thanks but it still has the same problem.
I have tried everything I can think of.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,357
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why does this not work?

 
-7
  #7
Sep 9th, 2009
1) Learn to format code in a readable style.

2) Remove all those unnecessary header files. You can always add some back when you need them.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. string str;
  6.  
  7. bool password();
  8.  
  9. void doCrypt(char *einput)
  10. {
  11. system("CLS");
  12. ifstream in;
  13. ofstream out;
  14. char c;
  15. string fileContent;
  16.  
  17. in.open(einput);
  18.  
  19. if(in.fail()) {
  20. in.close();
  21. cout <<"Could not find file: "<<einput<<endl;
  22. system("PAUSE");
  23. system("CLS");
  24. cout <<"*-----------------------------------*\n"
  25. <<"| Welcome to Crypto! |\n"
  26. <<"*-----------------------------------*\n";
  27. cout <<"Please type a command.\n"
  28. <<"Type \"m\" for the command menu.\n";
  29. return;
  30. }
  31.  
  32. while(in.get(c))
  33. {
  34. fileContent += c;
  35. }
  36. in.close();
  37. for(size_t i = 0; i < fileContent.size(); i++)
  38. {
  39. fileContent[i] += 0x4f;
  40. }
  41. out.open(einput, ios::trunc);
  42. out << fileContent;
  43. out.close();
  44.  
  45. cout <<"FILE ENCRYPTED!\n";
  46. system("PAUSE");
  47. system("CLS");
  48. cout <<"*-----------------------------------*\n"
  49. <<"| Welcome to Crypto! |\n"
  50. <<"*-----------------------------------*\n";
  51. cout <<"Please type a command.\n"
  52. <<"Type \"m\" for the command menu.\n";
  53. return;
  54. }
  55.  
  56. void doDcrypt(char *einput)
  57. {
  58. system("CLS");
  59. ifstream in;
  60. ofstream out;
  61. char c;
  62. string fileContent;
  63.  
  64. in.open(einput);
  65.  
  66. if(in.fail())
  67. {
  68. in.close();
  69. cout <<"Could not find file: "<<einput<<endl;
  70. system("PAUSE");
  71. system("CLS");
  72. cout <<"*-----------------------------------*\n"
  73. <<"| Welcome to Crypto! |\n"
  74. <<"*-----------------------------------*\n";
  75. cout <<"Please type a command.\n"
  76. <<"Type \"m\" for the command menu.\n";
  77. return;
  78. }
  79.  
  80. while(in.get(c))
  81. {
  82. fileContent += c;
  83. for(size_t i = 0; i < fileContent.size(); i++)
  84. {
  85. fileContent[i] -= 0x4f;
  86. }
  87. }
  88.  
  89. in.close();
  90. out.open(einput, ios::trunc);
  91. out << fileContent;
  92. out.close();
  93.  
  94. cout <<"FILE DECRYPTED!\n";
  95. system("PAUSE");
  96. system("CLS");
  97. cout <<"*-----------------------------------*\n"
  98. <<"| Welcome to Crypto! |\n"
  99. <<"*-----------------------------------*\n";
  100. cout <<"Please type a command.\n"
  101. <<"Type \"m\" for the command menu.\n";
  102. return;
  103. }
  104.  
  105.  
  106. int main(int argc, char* argv[])
  107. {
  108. char einput[20];
  109.  
  110. //Problems Start Here
  111. //<------------------------------------------------------------------------------>
  112.  
  113. char input_line[81];
  114. char pass1[50];
  115. char pass2[50];
  116. char comd;
  117.  
  118. ifstream file_in("setpCS4.dll");
  119.  
  120. if (! file_in) {
  121. file_in.close();
  122. cout <<"Please make a password: ";
  123. cin >> pass2;
  124. ofstream file_out("setpCS4.dll");
  125. file_out <<pass2;
  126. file_out.close();
  127. cout <<"Password made.\n";
  128. system("PAUSE");
  129. return 1;
  130. }
  131.  
  132. cout <<"Please type your password: ";
  133. cin.getline(pass1, sizeof(pass1));
  134. while( file_in.getline(input_line, 80) )
  135. {
  136. if(strcmp(input_line, pass1))
  137. {
  138. break;
  139. }
  140. }
  141.  
  142. //Problems End Here
  143. //<------------------------------------------------------------------------------>
  144.  
  145. system("CLS");
  146. cout <<"*-----------------------------------*\n"
  147. <<"| Welcome to Crypto! |\n"
  148. <<"*-----------------------------------*\n";
  149. cout <<"Please type a command.\n"
  150. <<"Type \"m\" for the command menu.\n";
  151. while(true)
  152. {
  153. cin >> comd;
  154. switch(comd)
  155. {
  156. case 'e' :
  157. system("CLS");
  158. cout <<"Please type name of input file(.txt): ";
  159. cin >> einput;
  160. doCrypt(einput);
  161. break;
  162.  
  163. case 'd' :
  164. system("CLS");
  165. cout <<"Please type name of input file(.txt): ";
  166. cin >> einput;
  167. doDcrypt(einput);
  168. break;
  169.  
  170. case 'm' :
  171. cout <<"\n<COMMAND MENU>\n"
  172. <<"*--------------------*\n"
  173. <<"| e = 'Encrypt' |\n"
  174. <<"| d = 'Decrypt' |\n"
  175. <<"| m = 'Command Menu' |\n"
  176. <<"| q = 'Quit' |\n"
  177. <<"*--------------------*\n";
  178. break;
  179.  
  180. case 'q' :
  181. cout <<"\nThanks For Using 'Crypto'\n";
  182. system("PAUSE");
  183. return 0;
  184. break;
  185.  
  186. default :
  187. break;
  188. }
  189. }
  190. }
Last edited by Ancient Dragon; Sep 9th, 2009 at 1:04 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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

Re: Why does this not work?

 
1
  #8
Sep 9th, 2009
Originally Posted by Kioti16 View Post
Thanks but it still has the same problem.
I have tried everything I can think of.
Of course it has the same problems. The compiler couldn't care less about indentation or descriptive names or an organized approach to the problem. I'm talking about human readability, both yours and ours.


Originally Posted by Ancient Dragon View Post
1) Learn to format code in a readable style.

2) Remove all those unnecessary header files. You can always add some back when you need them.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. string str;
  6.  
  7. bool password();
  8.  
  9. void doCrypt(char *einput)
  10. {
  11. system("CLS");
  12. ifstream in;
  13. ofstream out;
  14. char c;
  15. string fileContent;
  16.  
  17. in.open(einput);
  18.  
  19. if(in.fail()) {
  20. in.close();
  21. cout <<"Could not find file: "<<einput<<endl;
  22. system("PAUSE");
  23. system("CLS");
  24. cout <<"*-----------------------------------*\n"
  25. <<"| Welcome to Crypto! |\n"
  26. <<"*-----------------------------------*\n";
  27. cout <<"Please type a command.\n"
  28. <<"Type \"m\" for the command menu.\n";
  29. return;
  30. }
  31.  
  32. while(in.get(c))
  33. {
  34. fileContent += c;
  35. }
  36. in.close();
  37. for(size_t i = 0; i < fileContent.size(); i++)
  38. {
  39. fileContent[i] += 0x4f;
  40. }
  41. out.open(einput, ios::trunc);
  42. out << fileContent;
  43. out.close();
  44.  
  45. cout <<"FILE ENCRYPTED!\n";
  46. system("PAUSE");
  47. system("CLS");
  48. cout <<"*-----------------------------------*\n"
  49. <<"| Welcome to Crypto! |\n"
  50. <<"*-----------------------------------*\n";
  51. cout <<"Please type a command.\n"
  52. <<"Type \"m\" for the command menu.\n";
  53. return;
  54. }
  55.  
  56. void doDcrypt(char *einput)
  57. {
  58. system("CLS");
  59. ifstream in;
  60. ofstream out;
  61. char c;
  62. string fileContent;
  63.  
  64. in.open(einput);
  65.  
  66. if(in.fail())
  67. {
  68. in.close();
  69. cout <<"Could not find file: "<<einput<<endl;
  70. system("PAUSE");
  71. system("CLS");
  72. cout <<"*-----------------------------------*\n"
  73. <<"| Welcome to Crypto! |\n"
  74. <<"*-----------------------------------*\n";
  75. cout <<"Please type a command.\n"
  76. <<"Type \"m\" for the command menu.\n";
  77. return;
  78. }
  79.  
  80. while(in.get(c))
  81. {
  82. fileContent += c;
  83. for(size_t i = 0; i < fileContent.size(); i++)
  84. {
  85. fileContent[i] -= 0x4f;
  86. }
  87. }
  88.  
  89. in.close();
  90. out.open(einput, ios::trunc);
  91. out << fileContent;
  92. out.close();
  93.  
  94. cout <<"FILE DECRYPTED!\n";
  95. system("PAUSE");
  96. system("CLS");
  97. cout <<"*-----------------------------------*\n"
  98. <<"| Welcome to Crypto! |\n"
  99. <<"*-----------------------------------*\n";
  100. cout <<"Please type a command.\n"
  101. <<"Type \"m\" for the command menu.\n";
  102. return;
  103. }
  104.  
  105.  
  106. int main(int argc, char* argv[])
  107. {
  108. char einput[20];
  109.  
  110. //Problems Start Here
  111. //<------------------------------------------------------------------------------>
  112.  
  113. char input_line[81];
  114. char pass1[50];
  115. char pass2[50];
  116. char comd;
  117.  
  118. ifstream file_in("setpCS4.dll");
  119.  
  120. if (! file_in) {
  121. file_in.close();
  122. cout <<"Please make a password: ";
  123. cin >> pass2;
  124. ofstream file_out("setpCS4.dll");
  125. file_out <<pass2;
  126. file_out.close();
  127. cout <<"Password made.\n";
  128. system("PAUSE");
  129. return 1;
  130. }
  131.  
  132. cout <<"Please type your password: ";
  133. cin.getline(pass1, sizeof(pass1));
  134. while( file_in.getline(input_line, 80) )
  135. {
  136. if(strcmp(input_line, pass1))
  137. {
  138. break;
  139. }
  140. }
  141.  
  142. //Problems End Here
  143. //<------------------------------------------------------------------------------>
  144.  
  145. system("CLS");
  146. cout <<"*-----------------------------------*\n"
  147. <<"| Welcome to Crypto! |\n"
  148. <<"*-----------------------------------*\n";
  149. cout <<"Please type a command.\n"
  150. <<"Type \"m\" for the command menu.\n";
  151. while(true)
  152. {
  153. cin >> comd;
  154. switch(comd)
  155. {
  156. case 'e' :
  157. system("CLS");
  158. cout <<"Please type name of input file(.txt): ";
  159. cin >> einput;
  160. doCrypt(einput);
  161. break;
  162.  
  163. case 'd' :
  164. system("CLS");
  165. cout <<"Please type name of input file(.txt): ";
  166. cin >> einput;
  167. doDcrypt(einput);
  168. break;
  169.  
  170. case 'm' :
  171. cout <<"\n<COMMAND MENU>\n"
  172. <<"*--------------------*\n"
  173. <<"| e = 'Encrypt' |\n"
  174. <<"| d = 'Decrypt' |\n"
  175. <<"| m = 'Command Menu' |\n"
  176. <<"| q = 'Quit' |\n"
  177. <<"*--------------------*\n";
  178. break;
  179.  
  180. case 'q' :
  181. cout <<"\nThanks For Using 'Crypto'\n";
  182. system("PAUSE");
  183. return 0;
  184. break;
  185.  
  186. default :
  187. break;
  188. }
  189. }
  190. }


Line 9 - Call the function Encrypt. That's what people expect it to be called, or something very similar. Call the parameter plainText for the same reason.

Line 56 - Call the function Decrypt. That's what people expect it to be called, or something very similar. Call the parameter encriptedText or cipherText. Make it extremely obvious what it is.

Lines 45 - 52 - This shows up over and over, so stick it in it's own function called DisplayWelcomeMessage () or something similar.

Line 118 - If this the file that contains a password (password for what, by the way? Is this the encryption key?), call it "password.txt" or something.

Lines 118 - 140 - What's the point of all this? It has nothing to do with encryption or decryption. It's just a password that I have enter in order to run your program?

Line 85 - If this is the key, it probably shouldn't be hard-coded. Have a variable called key. And there are all sorts of algorithms out there for encryption/decryption. Add some comments to explain which one your program uses.


I mentioned a lot of this before, but it bears repeating.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 27
Reputation: Kioti16 is an unknown quantity at this point 
Solved Threads: 0
Kioti16 Kioti16 is offline Offline
Light Poster

Re: Why does this not work?

 
0
  #9
Sep 9th, 2009
I did everything you said and now stuff is really screwed up(Not because of you just because I must have done it wrong) But anyway I am sorry to take so much of your time I will probably figure it out some time.
But if you are interested here is the new source.
Thanks.
Attached Files
File Type: cpp test.cpp (3.9 KB, 3 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

Re: Why does this not work?

 
0
  #10
Sep 9th, 2009
Originally Posted by Kioti16 View Post
I did everything you said and now stuff is really screwed up(Not because of you just because I must have done it wrong) But anyway I am sorry to take so much of your time I will probably figure it out some time.
But if you are interested here is the new source.
Thanks.

You did some of the things I said, but mostly you kept your old program. You still hard-code the key, you still call have a key that makes things impossible to check (I changed it to 0x01 - much easier - I mentioned that in your previous thread), you still don't call the key a key, and you still don't have comments as to what you are doing. Here's some revised code.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. string str;
  6.  
  7. void Message() {
  8. cout <<"*-----------------------------------*\n"
  9. <<"| Welcome to Crypto! |\n"
  10. <<"*-----------------------------------*\n";
  11. cout <<"Please type a command.\n"
  12. <<"Type \"m\" for the command menu.\n";
  13. return;
  14. }
  15.  
  16. void Encrypt(char *filename, int key)
  17. {
  18. system("CLS");
  19. ifstream in;
  20. ofstream out;
  21. char c;
  22. string fileContent;
  23.  
  24. in.open(filename);
  25.  
  26. if(in.fail()) {
  27. in.close();
  28. cout <<"Could not find file: "<<filename<<endl;
  29. system("PAUSE");
  30. system("CLS");
  31. Message();
  32. return;
  33. }
  34.  
  35. while(in.get(c))
  36. {
  37. fileContent += c;
  38. }
  39. in.close();
  40. for(size_t i = 0; i < fileContent.size(); i++)
  41. {
  42. fileContent[i] += key;
  43. }
  44. out.open(filename, ios::trunc);
  45. out << fileContent;
  46. out.close();
  47.  
  48. cout <<"FILE ENCRYPTED!\n";
  49. system("PAUSE");
  50. system("CLS");
  51. Message();
  52. }
  53.  
  54. void Decrypt(char *filename, int key)
  55. {
  56. system("CLS");
  57. ifstream in;
  58. ofstream out;
  59. char c;
  60. string fileContent;
  61.  
  62. in.open(filename);
  63.  
  64. if(in.fail())
  65. {
  66. in.close();
  67. cout <<"Could not find file: "<<filename<<endl;
  68. system("PAUSE");
  69. system("CLS");
  70. Message();
  71. return;
  72. }
  73.  
  74.  
  75.  
  76. while(in.get(c))
  77. {
  78. fileContent += c;
  79. for(size_t i = 0; i < fileContent.size(); i++)
  80. {
  81. fileContent[i] -= key;
  82. }
  83. }
  84.  
  85. in.close();
  86. out.open(filename, ios::trunc);
  87. out << fileContent;
  88. out.close();
  89.  
  90. cout <<"FILE DECRYPTED!\n";
  91. system("PAUSE");
  92. system("CLS");
  93. Message();
  94. }
  95.  
  96.  
  97. bool PasswordCheck ()
  98. {
  99. /*
  100.   ifstream file_in("setpCS4.dll");
  101.  
  102.   if (! file_in) {
  103.   file_in.close();
  104.   cout <<"Please make a password: ";
  105.   cin >> pass2;
  106.   ofstream file_out("setpCS4.dll");
  107.   file_out <<pass2;
  108.   file_out.close();
  109.   cout <<"Password made.\n";
  110.   system("PAUSE");
  111.   return 1;
  112.   }
  113.  
  114.   cout <<"Please type your password: ";
  115.   cin.getline(pass1, sizeof(pass1));
  116.   while( file_in.getline(input_line, 80) )
  117.   {
  118.   if(strcmp(input_line, pass1))
  119.   {
  120.   break;
  121.   }
  122.   else
  123.   {
  124.   return 0;
  125.   }
  126.   } */
  127.  
  128. return true;
  129. }
  130.  
  131.  
  132. int main(int argc, char* argv[])
  133. {
  134. char filename[20];
  135. char input_line[81];
  136. char pass1[50];
  137. char pass2[50];
  138. char comd;
  139.  
  140. int key = 0x01;
  141.  
  142.  
  143. if (!PasswordCheck ())
  144. {
  145. cout << "Bad password. Exiting program.\n";
  146. return 0;
  147. }
  148.  
  149.  
  150. system("CLS");
  151. cout <<"*-----------------------------------*\n"
  152. <<"| Welcome to Crypto! |\n"
  153. <<"*-----------------------------------*\n";
  154. cout <<"Please type a command.\n"
  155. <<"Type \"m\" for the command menu.\n";
  156.  
  157.  
  158. while(true)
  159. {
  160. cin >> comd;
  161. switch(comd)
  162. {
  163. case 'e' :
  164. system("CLS");
  165. cout <<"Please type name of file to encrypt (.txt): ";
  166. cin >> filename;
  167. Encrypt(filename, key);
  168. break;
  169.  
  170. case 'd' :
  171. system("CLS");
  172. cout <<"Please type name of file to encrypt (.txt): ";
  173. cin >> filename;
  174. Decrypt(filename, key);
  175. break;
  176.  
  177. case 'm' :
  178. cout <<"\n<COMMAND MENU>\n"
  179. <<"*--------------------*\n"
  180. <<"| e = 'Encrypt' |\n"
  181. <<"| d = 'Decrypt' |\n"
  182. <<"| m = 'Command Menu' |\n"
  183. <<"| q = 'Quit' |\n"
  184. <<"*--------------------*\n";
  185. break;
  186.  
  187. case 'q' :
  188. cout <<"\nThanks For Using 'Crypto'\n";
  189. system("PAUSE");
  190. return 0;
  191. break;
  192.  
  193. default :
  194. break;
  195. }
  196. }
  197. }

Set up a text file with a single line with "uvwxy" in it. Run it, encrypt it, and you get "vwxyz", which is correct. Now decrypt it and see what you get. You have a problem in your Decrypt function, which I have left. Otherwise I have cleaned up your program a little.

I also took the PasswordCheck out and put it in its own function, though I commented it out. Put it back in if you like, but at least now it's well named and it's out of the way.
Reply With Quote Quick reply to this message  
Reply


Message:



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



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

©2003 - 2009 DaniWeb® LLC