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.
kernel>panic 3 Light Poster
#include <stdio.h>
#include <cstdio>
#include <cstdlib>
#include <process.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <string.h>
#include <windows.h>
using namespace std;
string str;
bool password();
void doCrypt(char *einput) {
system("CLS");
ifstream in;
ofstream out;
char c;
string fileContent;
in.open(einput);
if(in.fail()) {
in.close();
cout <<"Could not find file: "<<einput<<endl;
system("PAUSE");
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return; }
while(in.get(c)) {
fileContent += c;
for(int i = 0; i < fileContent.size(); i++) {
fileContent[i] += 0x4f; }
}
in.close();
out.open(einput, ios::trunc);
out << fileContent;
out.close();
cout <<"FILE ENCRYPTED!\n";
system("PAUSE");
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return;
}
void doDcrypt(char *einput) {
system("CLS");
ifstream in;
ofstream out;
char c;
string fileContent;
in.open(einput);
if(in.fail()) {
in.close();
cout <<"Could not find file: "<<einput<<endl;
system("PAUSE");
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return; }
while(in.get(c)) {
fileContent += c;
for(int i = 0; i < fileContent.size(); i++) {
fileContent[i] -= 0x4f;
}}
in.close();
out.open(einput, ios::trunc);
out << fileContent;
out.close();
cout <<"FILE DECRYPTED!\n";
system("PAUSE");
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return;
}
int main(int check) {
char comd;
char *einput=new char[20];
//Problems Start Here
//<------------------------------------------------------------------------------>
int c;
int i;
char input_line[81];
char pass1[50];
char pass2[50];
ifstream file_in("setpCS4.dll");
if (! file_in) {
file_in.close();
cout <<"Please make a password: ";
cin >> pass2;
ofstream file_out("setpCS4.dll");
file_out <<pass2;
file_out.close();
cout <<"Password made.\n";
system("PAUSE");
goto over;
}
cout <<"Please type your password: ";
gets(pass1);
while (1) {
for (i = 1; i <= 24 && ! file_in.eof(); i++) {
file_in.getline(input_line, 80);
if(strcmp(input_line, pass1)) goto over; file_in.close();
return 1;
}
}
//Problems End Here
//<------------------------------------------------------------------------------>
over:
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
over2:
while(true) {
cin >> comd;
switch(comd) {
case 'e' :
system("CLS");
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doCrypt(einput);
break;
break;
case 'd' :
system("CLS");
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doDcrypt(einput);
break;
break;
case 'm' :
cout <<"\n<COMMAND MENU>\n"
<<"*--------------------*\n"
<<"| e = 'Encrypt' |\n"
<<"| d = 'Decrypt' |\n"
<<"| m = 'Command Menu' |\n"
<<"| q = 'Quit' |\n"
<<"*--------------------*\n";
goto over2;
break;
break;
case 'q' :
cout <<"\nThanks For Using 'Crypto'\n";
system("PAUSE");
return 0;
break;
break;
default :
goto over;
break;
break;
}
}
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
Your program makes zero sense. Why are you using ofstream to write to a dll ?????:icon_eek:
kernel>panic 3 Light Poster
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.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
int main(int check) {
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.
Edited by Ancient Dragon because: n/a
VernonDozier 2,218 Posting Expert Featured Poster
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.
over:
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
over2:
while(true) {
cin >> comd;
switch(comd) {
case 'e' :
system("CLS");
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doCrypt(einput);
break;
break;
case 'd' :
system("CLS");
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doDcrypt(einput);
break;
break;
case 'm' :
cout <<"\n<COMMAND MENU>\n"
<<"*--------------------*\n"
<<"| e = 'Encrypt' |\n"
<<"| d = 'Decrypt' |\n"
<<"| m = 'Command Menu' |\n"
<<"| q = 'Quit' |\n"
<<"*--------------------*\n";
goto over2;
break;
break;
case 'q' :
cout <<"\nThanks For Using 'Crypto'\n";
system("PAUSE");
return 0;
break;
break;
default :
goto over;
break;
break;
}
}
}
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:
string decrypt (string encryptedMessage, string key);
string encrypt (string plainText, string key);
Sticking code like this:
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.
kernel>panic 3 Light Poster
Thanks but it still has the same problem.
I have tried everything I can think of.
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
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.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string str;
bool password();
void doCrypt(char *einput)
{
system("CLS");
ifstream in;
ofstream out;
char c;
string fileContent;
in.open(einput);
if(in.fail()) {
in.close();
cout <<"Could not find file: "<<einput<<endl;
system("PAUSE");
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return;
}
while(in.get(c))
{
fileContent += c;
}
in.close();
for(size_t i = 0; i < fileContent.size(); i++)
{
fileContent[i] += 0x4f;
}
out.open(einput, ios::trunc);
out << fileContent;
out.close();
cout <<"FILE ENCRYPTED!\n";
system("PAUSE");
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return;
}
void doDcrypt(char *einput)
{
system("CLS");
ifstream in;
ofstream out;
char c;
string fileContent;
in.open(einput);
if(in.fail())
{
in.close();
cout <<"Could not find file: "<<einput<<endl;
system("PAUSE");
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return;
}
while(in.get(c))
{
fileContent += c;
for(size_t i = 0; i < fileContent.size(); i++)
{
fileContent[i] -= 0x4f;
}
}
in.close();
out.open(einput, ios::trunc);
out << fileContent;
out.close();
cout <<"FILE DECRYPTED!\n";
system("PAUSE");
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return;
}
int main(int argc, char* argv[])
{
char einput[20];
//Problems Start Here
//<------------------------------------------------------------------------------>
char input_line[81];
char pass1[50];
char pass2[50];
char comd;
ifstream file_in("setpCS4.dll");
if (! file_in) {
file_in.close();
cout <<"Please make a password: ";
cin >> pass2;
ofstream file_out("setpCS4.dll");
file_out <<pass2;
file_out.close();
cout <<"Password made.\n";
system("PAUSE");
return 1;
}
cout <<"Please type your password: ";
cin.getline(pass1, sizeof(pass1));
while( file_in.getline(input_line, 80) )
{
if(strcmp(input_line, pass1))
{
break;
}
}
//Problems End Here
//<------------------------------------------------------------------------------>
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
while(true)
{
cin >> comd;
switch(comd)
{
case 'e' :
system("CLS");
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doCrypt(einput);
break;
case 'd' :
system("CLS");
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doDcrypt(einput);
break;
case 'm' :
cout <<"\n<COMMAND MENU>\n"
<<"*--------------------*\n"
<<"| e = 'Encrypt' |\n"
<<"| d = 'Decrypt' |\n"
<<"| m = 'Command Menu' |\n"
<<"| q = 'Quit' |\n"
<<"*--------------------*\n";
break;
case 'q' :
cout <<"\nThanks For Using 'Crypto'\n";
system("PAUSE");
return 0;
break;
default :
break;
}
}
}
Edited by Ancient Dragon because: n/a
VernonDozier 2,218 Posting Expert Featured Poster
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.
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.
#include <iostream> #include <fstream> #include <string> using namespace std; string str; bool password(); void doCrypt(char *einput) { system("CLS"); ifstream in; ofstream out; char c; string fileContent; in.open(einput); if(in.fail()) { in.close(); cout <<"Could not find file: "<<einput<<endl; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } while(in.get(c)) { fileContent += c; } in.close(); for(size_t i = 0; i < fileContent.size(); i++) { fileContent[i] += 0x4f; } out.open(einput, ios::trunc); out << fileContent; out.close(); cout <<"FILE ENCRYPTED!\n"; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } void doDcrypt(char *einput) { system("CLS"); ifstream in; ofstream out; char c; string fileContent; in.open(einput); if(in.fail()) { in.close(); cout <<"Could not find file: "<<einput<<endl; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } while(in.get(c)) { fileContent += c; for(size_t i = 0; i < fileContent.size(); i++) { fileContent[i] -= 0x4f; } } in.close(); out.open(einput, ios::trunc); out << fileContent; out.close(); cout <<"FILE DECRYPTED!\n"; system("PAUSE"); system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; return; } int main(int argc, char* argv[]) { char einput[20]; //Problems Start Here //<------------------------------------------------------------------------------> char input_line[81]; char pass1[50]; char pass2[50]; char comd; ifstream file_in("setpCS4.dll"); if (! file_in) { file_in.close(); cout <<"Please make a password: "; cin >> pass2; ofstream file_out("setpCS4.dll"); file_out <<pass2; file_out.close(); cout <<"Password made.\n"; system("PAUSE"); return 1; } cout <<"Please type your password: "; cin.getline(pass1, sizeof(pass1)); while( file_in.getline(input_line, 80) ) { if(strcmp(input_line, pass1)) { break; } } //Problems End Here //<------------------------------------------------------------------------------> system("CLS"); cout <<"*-----------------------------------*\n" <<"| Welcome to Crypto! |\n" <<"*-----------------------------------*\n"; cout <<"Please type a command.\n" <<"Type \"m\" for the command menu.\n"; while(true) { cin >> comd; switch(comd) { case 'e' : system("CLS"); cout <<"Please type name of input file(.txt): "; cin >> einput; doCrypt(einput); break; case 'd' : system("CLS"); cout <<"Please type name of input file(.txt): "; cin >> einput; doDcrypt(einput); break; case 'm' : cout <<"\n<COMMAND MENU>\n" <<"*--------------------*\n" <<"| e = 'Encrypt' |\n" <<"| d = 'Decrypt' |\n" <<"| m = 'Command Menu' |\n" <<"| q = 'Quit' |\n" <<"*--------------------*\n"; break; case 'q' : cout <<"\nThanks For Using 'Crypto'\n"; system("PAUSE"); return 0; break; default : break; } } }
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.
Nick Evan commented: It's like pop-music, repeat repeat and repeat again :) +29
kernel>panic 3 Light Poster
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.
VernonDozier 2,218 Posting Expert Featured Poster
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.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string str;
void Message() {
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return;
}
void Encrypt(char *filename, int key)
{
system("CLS");
ifstream in;
ofstream out;
char c;
string fileContent;
in.open(filename);
if(in.fail()) {
in.close();
cout <<"Could not find file: "<<filename<<endl;
system("PAUSE");
system("CLS");
Message();
return;
}
while(in.get(c))
{
fileContent += c;
}
in.close();
for(size_t i = 0; i < fileContent.size(); i++)
{
fileContent[i] += key;
}
out.open(filename, ios::trunc);
out << fileContent;
out.close();
cout <<"FILE ENCRYPTED!\n";
system("PAUSE");
system("CLS");
Message();
}
void Decrypt(char *filename, int key)
{
system("CLS");
ifstream in;
ofstream out;
char c;
string fileContent;
in.open(filename);
if(in.fail())
{
in.close();
cout <<"Could not find file: "<<filename<<endl;
system("PAUSE");
system("CLS");
Message();
return;
}
while(in.get(c))
{
fileContent += c;
for(size_t i = 0; i < fileContent.size(); i++)
{
fileContent[i] -= key;
}
}
in.close();
out.open(filename, ios::trunc);
out << fileContent;
out.close();
cout <<"FILE DECRYPTED!\n";
system("PAUSE");
system("CLS");
Message();
}
bool PasswordCheck ()
{
/*
ifstream file_in("setpCS4.dll");
if (! file_in) {
file_in.close();
cout <<"Please make a password: ";
cin >> pass2;
ofstream file_out("setpCS4.dll");
file_out <<pass2;
file_out.close();
cout <<"Password made.\n";
system("PAUSE");
return 1;
}
cout <<"Please type your password: ";
cin.getline(pass1, sizeof(pass1));
while( file_in.getline(input_line, 80) )
{
if(strcmp(input_line, pass1))
{
break;
}
else
{
return 0;
}
} */
return true;
}
int main(int argc, char* argv[])
{
char filename[20];
char input_line[81];
char pass1[50];
char pass2[50];
char comd;
int key = 0x01;
if (!PasswordCheck ())
{
cout << "Bad password. Exiting program.\n";
return 0;
}
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
while(true)
{
cin >> comd;
switch(comd)
{
case 'e' :
system("CLS");
cout <<"Please type name of file to encrypt (.txt): ";
cin >> filename;
Encrypt(filename, key);
break;
case 'd' :
system("CLS");
cout <<"Please type name of file to encrypt (.txt): ";
cin >> filename;
Decrypt(filename, key);
break;
case 'm' :
cout <<"\n<COMMAND MENU>\n"
<<"*--------------------*\n"
<<"| e = 'Encrypt' |\n"
<<"| d = 'Decrypt' |\n"
<<"| m = 'Command Menu' |\n"
<<"| q = 'Quit' |\n"
<<"*--------------------*\n";
break;
case 'q' :
cout <<"\nThanks For Using 'Crypto'\n";
system("PAUSE");
return 0;
break;
default :
break;
}
}
}
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.
kernel>panic 3 Light Poster
Thanks for your help.
I was wondering how I could use a algorithm library with this and which one to use.
Also the password part still does not work with my compiler I don't know it it worked with yours or not.
VernonDozier 2,218 Posting Expert Featured Poster
Thanks for your help.
I was wondering how I could use a algorithm library with this and which one to use.Also the password part still does not work with my compiler I don't know it it worked with yours or not.
It has nothing to do with the compiler. I commented your password code out and stuck it to the side since it was hard to follow and had nothing to do with the encryption as far as I could tell. It's in its own function. It's a trivial function now since its commented out. Uncomment it out if you like and rewrite it or whatever, but don't name a text file a dll file because all it does is confuses people.
Regarding algorithm, I don't know where you are thinking about using it, so I have no idea how to answer your question. There are all sorts of ways to improve the program. I suppose you could use algorithm possibly. I can't see, off the top of my head, where you would use it here. This program just replaces each character with a different character using a Caesar Cipher.
kernel>panic 3 Light Poster
Ok I need to get a few things straight.
1 The reason there is a password is because it is a encryption program and if it didn't have a password someone could just use it and get my algorithm.
2 The reason it is a .dll file is for the exact reason you said to confuse people so they don't think it is a password file, and I am hoping once I get the password part working to make the password get encrypted.
3 I removed the comment tags already(I am not that stupid).
So what I need to do is:
1 Get the password protection working.
2 Make the password get encrypted, and set up the program so the password file cant just be deleted to make a new password.
3 Add an algirithum like acr4 to the encrypting and decrypting part.
Using a .h header file or some .lib file.
Sorry if I sound rude but I have only been doing c++ 3 years and am only a beginner getting frustrated.
Thank you very much.
VernonDozier 2,218 Posting Expert Featured Poster
Ok I need to get a few things straight.
1 The reason there is a password is because it is a encryption program and if it didn't have a password someone could just use it and get my algorithm.2 The reason it is a .dll file is for the exact reason you said to confuse people so they don't think it is a password file, and I am hoping once I get the password part working to make the password get encrypted.
3 I removed the comment tags already(I am not that stupid).
So what I need to do is:
1 Get the password protection working.
2 Make the password get encrypted, and set up the program so the password file cant just be deleted to make a new password.
3 Add an algirithum like acr4 to the encrypting and decrypting part.
Using a .h header file or some .lib file.Sorry if I sound rude but I have only been doing c++ 3 years and am only a beginner getting frustrated.
Thank you very much.
There's a difference between confusing people who are trying to debug your code and confusing people who are trying to break your encryption. Get the program correct first, then you can obfuscate the code all you want. The obfuscation comes later. Anyone who had the source code could immediately see that you aren't loading a dll, despite the name. It's not going to trick anyone. Anyone who had the source code will also see that your key is hard-coded into the code. Don't confuse your "key" with the "password" stored in your text file. The password stored in the file has nothing to do with the encryption and decryption. It simply aborts the program if you don't have the right program. You also have the password file hard-coded in the file, so anyone with the source code knows immediately where to look. If you want security, you need to have the key and/or password read in as command line arguments. That way you can have the source code and you still won't know what the key is and where the pssword is stored. Anyone with the source code immediately can bypass the password anyway, so all you're left with is the key.
Regarding comments and names, normally the bad guy trying to revese engineer your program won't have the source code. They have the executable. You keep the source code close to the vest. All comments and names are stripped out of the executable anyway by the compiler. Comment all you want, name everything whatever you want and it's all irrelevant because you only release the executable.
A good encryption/decryption program/algorithm allows you to be completely transparent with the source code. Use an algorithm like Triple DES or AES and I can show you my source code and you still can't break it without the key. I was assuming that since this is a Caesar Cipher, this project was just for fun since no one actually uses that since it's way too easy to break.
Anyway, we have no way of knowing where you're coming from when you post if you don't tell us in the beginning. We're assuming that you're trying to create good, well-organized easy-to-follow code. As mentioned, if you want security, don't hard-code the key, password, or password file. Read them in as command line arguments or ask them to type it in. Or I suppose you could have a hash function and hash a password and hard-code the hash value into the program. A good hash formula won't allow you to go backwards from the hash to the password.
kernel>panic 3 Light Poster
That sounds like great stuff.
But there is a problem I know what your talking about but I do not know how to put it in to code.
I know you don't have time to code this for me but could you give me some help?
My friend and I are competing we each have a encryption program and mine when we started was way ahead of his but now his is using all the stuff you are talking about he's using hash to store the password and no hard coded stuff.
But he has it so much easier, he is using Python.
What can I do?
Thanks.
VernonDozier 2,218 Posting Expert Featured Poster
Well, here's the program I posted earlier using command line arguments and adding the password stuff back in.
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
string str;
void Message() {
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
return;
}
void Encrypt(char *filename, int key)
{
system("CLS");
ifstream in;
ofstream out;
char c;
string fileContent;
in.open(filename);
if(in.fail()) {
in.close();
cout <<"Could not find file: "<<filename<<endl;
system("PAUSE");
system("CLS");
Message();
return;
}
while(in.get(c))
{
fileContent += c;
}
in.close();
for(size_t i = 0; i < fileContent.size(); i++)
{
fileContent[i] += key;
}
out.open(filename, ios::trunc);
out << fileContent;
out.close();
cout <<"FILE ENCRYPTED!\n";
system("PAUSE");
system("CLS");
Message();
}
void Decrypt(char *filename, int key)
{
system("CLS");
ifstream in;
ofstream out;
char c;
string fileContent;
in.open(filename);
if(in.fail())
{
in.close();
cout <<"Could not find file: "<<filename<<endl;
system("PAUSE");
system("CLS");
Message();
return;
}
while(in.get(c))
{
fileContent += c;
for(size_t i = 0; i < fileContent.size(); i++)
{
fileContent[i] -= key;
}
}
in.close();
out.open(filename, ios::trunc);
out << fileContent;
out.close();
cout <<"FILE DECRYPTED!\n";
system("PAUSE");
system("CLS");
Message();
}
bool PasswordCheck (char* filename)
{
ifstream file_in(filename);
char pass[80];
char input_line[80];
if (! file_in)
{
file_in.close();
return false;
}
cout <<"Please type your password: ";
cin.getline(pass, 80);
while( file_in.getline(input_line, 80) )
{
if(strcmp(input_line, pass) == 0)
return true;
}
return false;
}
int main(int argc, char* argv[])
{
char filename[20];
char comd;
int key = atoi (argv[1]);
if (!PasswordCheck (argv[2]))
{
cout << "Bad password. Exiting program.\n";
return 0;
}
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command.\n"
<<"Type \"m\" for the command menu.\n";
while(true)
{
cin >> comd;
switch(comd)
{
case 'e' :
system("CLS");
cout <<"Please type name of file to encrypt (.txt): ";
cin >> filename;
Encrypt(filename, key);
break;
case 'd' :
system("CLS");
cout <<"Please type name of file to encrypt (.txt): ";
cin >> filename;
Decrypt(filename, key);
break;
case 'm' :
cout <<"\n<COMMAND MENU>\n"
<<"*--------------------*\n"
<<"| e = 'Encrypt' |\n"
<<"| d = 'Decrypt' |\n"
<<"| m = 'Command Menu' |\n"
<<"| q = 'Quit' |\n"
<<"*--------------------*\n";
break;
case 'q' :
cout <<"\nThanks For Using 'Crypto'\n";
system("PAUSE");
return 0;
break;
default :
break;
}
}
}
Compile it. Think of a key like 1, 2, 3, 4, whatever. Create a password file in a text editor called ""setpCS4.dll" or whatever you want to call it, then run it like this (assuming your program is called "encryptionProgram.exe" and your password file is "setupCS4.dll" and your key is 5): encryptionProgram 5 setupCS4.dll
I left the decryption bug in there that I told you about earlier. Again, pick an easy file like "jklm" and an easy key like 1, then encypt it and see what it looks like. Now decrypt it and note the problem and what is happening. Line 127 and 130 are where the program grabs the command line arguments.
mrnutty commented: I applaud you VernonDozier for all your long post. You get the longest consistent post award from me. +4
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.