I have this encryption program that I spend hours on.
I thought I had it all worked out but no.
What could be my problem can anyone help me.
You can ignor the void doSet() I will ad that later.
Thanks.
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;
void doCrypt(char *einput) {
ifstream in;
ofstream out;
char buf;
char a;
in.open(einput);
if(in.fail()) {
cout <<"Could not find file: "<<einput<<endl;
system("PAUSE");
return; }
if(in.is_open()) {
while(!in.eof()) {
in.get(buf); } }
in.close();
out.open(einput, ios::trunc);
a=buf-0xFACA5;
out << a;
out.close();
cout <<"File Saved.\n";
system("PAUSE");
return;
}
void doDcrypt(char *dinput) {
ifstream in;
ofstream out;
char buf;
char a;
in.open(dinput);
if(in.fail()) {
cout <<"Could not find file: "<<dinput<<endl;
system("PAUSE");
return; }
if(in.is_open()) {
while(!in.eof()) {
in.get(buf); } }
in.close();
out.open(dinput, ios::trunc);
a=buf+0xFACA5;
out << a;
out.close();
cout <<"File Saved.\n";
system("PAUSE");
return;
}
void doSet() {
}
int main(void) {
//Coding starts here//
char *einput=new char[20];
char *dinput=new char[20];
ofstream out;
ifstream in;
char comd;
char buf[81];
char pass[81];
in.open("setpCS4.dll");
if(in.is_open()) {
cout <<"Please type program password: ";
cin >> pass;
while(!in.eof()) {
in.getline(buf, 80);
if(buf==pass) {
goto logedIn; }
else {
return 0; }
} }
else {
cout <<"*--------------------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"| Please make a password. |\n"
<<"| Password must not contain any spaces. |\n"
<<"*--------------------------------------------*\n";
cout <<"Password: ";
cin >> pass;
system("CLS");
out.open("setpCS4.dll");
out << pass;
out.close();
cout <<"Password made!\n";
system("PAUSE");
goto over;
}
logedIn:
cout <<"Password Accepted.\n";
system("PAUSE");
over:
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command,\n"
<<"or type 'm' for the menu.\n";
over2:
cout <<"Command: ";
while(true) {
cin >> comd;
switch(comd) {
case 'e' :
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doCrypt(einput);
goto over;
break;
case 'd' :
cout <<"Please type name of input file(.txt): ";
cin >> dinput;
doDcrypt(dinput);
goto over;
break;
case 's' :
doSet();
goto over;
break;
case 'm' :
cout <<"\n[ COMMAND MENU>>>>>\n"
<<"($++++++++++++++++$)\n"
<<"(| e = 'Encrypt' |)\n"
<<"(| d = 'Decrypt' |)\n"
<<"(| s = 'Settings' |)\n"
<<"(| m = 'Menu' |)\n"
<<"(| q = 'quit' |)\n"
<<"($++++++++++++++++$)\n";
goto over2;
break;
case 'q' :
cout <<"Thanks for using 'Crypto'.\n";
system("PAUSE");
return 0;
break;
default :
goto over;
break; }
}
}
VernonDozier 2,218 Posting Expert Featured Poster
I have this encryption program that I spend hours on.
I thought I had it all worked out but no.
What could be my problem can anyone help me.
You can ignor the void doSet() I will ad that later.
Thanks.
What is this file?
in.open("setpCS4.dll");
kernel>panic 3 Light Poster
Oh I am sorry, that is the file I use for password protection.
It is a .dll file because that is more safe before I can have the password encrypted. So all you have to do is make a text file with that name, or just use a plain text file.
Thanks for your help.
VernonDozier 2,218 Posting Expert Featured Poster
I have no idea what you are trying to do with this program or with this function. I think the code needs some comments in it, and you need to explain in the program what it does and and what it requires, because it's definitely not self explanatory. What should the input file look like?
void doCrypt(char *einput)
{
ifstream in;
ofstream out;
char buf;
char a;
in.open(einput);
if (in.fail())
{
cout << "Could not find file: " << einput << endl;
system("PAUSE");
return;
}
if (in.is_open())
{
while (!in.eof())
{
in.get(buf);
}
}
in.close();
out.open(einput, ios::trunc);
a = buf - 0xFACA5;
out << a;
out.close();
cout << "File Saved.\n";
system("PAUSE");
return;
}
Line 21 - You keep overwriting buf. Why?
Line 28
a = buf - 0xFACA5;
a and buf are both chars, so they are 8 bits, right? Where does 0xFACA5 come from? I assume it's the encryption key, but why have such a large encryption key when a ranges from -128 to 127 and you're subtracting? And why are you only encrypting one single character?
I think you need to explain the logic of the program, what the goal is, the input, output, etc.
mrnutty 761 Senior Poster
Basic encryption, i.e weak but encrypted.
1) Read file content into string
2) Have a key number, say 5.
3) Shift all letter in the string by adding the key number
for(int i =0; i < string.size(); i+=) string[i] +=key_number
4) Now its encrypted.
5) For decryption, just using the same for loop but instead
do string -=key_number.
kernel>panic 3 Light Poster
I have no idea what you are trying to do with this program or with this function. I think the code needs some comments in it, and you need to explain in the program what it does and and what it requires, because it's definitely not self explanatory. What should the input file look like?
void doCrypt(char *einput) { ifstream in; ofstream out; char buf; char a; in.open(einput); if (in.fail()) { cout << "Could not find file: " << einput << endl; system("PAUSE"); return; } if (in.is_open()) { while (!in.eof()) { in.get(buf); } } in.close(); out.open(einput, ios::trunc); a = buf - 0xFACA5; out << a; out.close(); cout << "File Saved.\n"; system("PAUSE"); return; }
Line 21 - You keep overwriting buf. Why?
Line 28
a = buf - 0xFACA5;
a and buf are both chars, so they are 8 bits, right? Where does 0xFACA5 come from? I assume it's the encryption key, but why have such a large encryption key when a ranges from -128 to 127 and you're subtracting? And why are you only encrypting one single character?
I think you need to explain the logic of the program, what the goal is, the input, output, etc.
Okay I put together the .cpp file again with comments.
Everything is explained.
Thanks.
#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;
void doCrypt(char *einput) { //Encryption part of the program//
ifstream in; //Input//
ofstream out; //Output//
char buf; //Buffer for the input//
char a; //Collects data//
in.open(einput); //Opens the input from user//
if(in.fail()) { //If input fails do this//
cout <<"Could not find file: "<<einput<<endl; //Could not find input file of user//
system("PAUSE"); //Pause Program//
return; } //Return to menu//
if(in.is_open()) { //If file did open do this//
while(!in.eof()) { //I think my errors start here//
in.get(buf); } } //Supposed to get the whole text file and save it to buf//
in.close(); //Then close the input stream//
out.open(einput, ios::trunc); //Then open the output stream//
while(!out.eof()) { //Write while not end of file//
a=buf-0xFACA5; //Take one letter at a time, encrypt it using this encryption//
out << a; } //Write it out, and do this over, but it does not work//
out.close(); //Close the output stream//
cout <<"File Saved.\n"; //Tell user file was saved//
system("PAUSE"); //Pause program//
return; //Return to menu//
}
void doDcrypt(char *dinput) { //Do everything completely the same but backwards//
ifstream in;
ofstream out;
char buf;
char a;
in.open(dinput);
if(in.fail()) {
cout <<"Could not find file: "<<dinput<<endl;
system("PAUSE");
return; }
if(in.is_open()) {
while(!in.eof()) {
in.get(buf); } }
in.close();
out.open(dinput, ios::trunc);
a=buf+0xFACA5;
out << a;
out.close();
cout <<"File Saved.\n";
system("PAUSE");
return;
}
void doSet() { //This will be for settings, you can pass this by//
}
int main(void) { //Main part of program//
char *einput=new char[20]; //Input char for doCrypt//
char *dinput=new char[20]; //Input char for doDcrypt//
ofstream out; //Input for password//
ifstream in; //Output for password//
char comd; //Command Collector//
char buf[81]; //Buffer for password//
char pass[81]; //Collect password//
in.open("setpCS4.dll"); //Open the password file//
if(in.is_open()) { //If file is found ask for password//
cout <<"Please type program password: "; //Password command//
cin >> pass; //Input password//
while(!in.eof()) { //Problems start here//
in.getline(buf, 80); //Get password//
if(buf==pass) { //If password is what user input//
goto logedIn; } //goto the program//
else { //Else quit//
return 0; } //Quit//
} }
else { //Else if the file was not found, do this//
cout <<"*--------------------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"| Please make a password. |\n"
<<"| Password must not contain any spaces. |\n"
<<"*--------------------------------------------*\n";
cout <<"Password: "; //Ask for password//
cin >> pass; //Password Input//
system("CLS"); //Clear the screen//
out.open("setpCS4.dll"); //Open out file//
out << pass; //Out with the password//
out.close(); //Close out file//
cout <<"Password made!\n"; //Tell user password was made//
system("PAUSE"); //Pause program//
goto over; //goto program//
}
logedIn: //Program starts here//
cout <<"Password Accepted.\n"; //Password from user was right//
system("PAUSE");//Pause program//
over: //goto function//
system("CLS"); //Clear the screen//
cout <<"*-----------------------------------*\n" //Welcome message//
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command,\n" //Ask user for a command//
<<"or type 'm' for the menu.\n";
over2: //goto function//
cout <<"Command: "; //Ask for command//
while(true) { //Part of switch statement//
cin >> comd; //Get command//
switch(comd) { //Switch Menu//
case 'e' : //Encryption Part//
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doCrypt(einput);
goto over;
break;
case 'd' : //Decryption Part//
cout <<"Please type name of input file(.txt): ";
cin >> dinput;
doDcrypt(dinput);
goto over;
break;
case 's' : //Settings skip this//
doSet();
goto over;
break;
case 'm' : //Command Menu//
cout <<"\n[ COMMAND MENU>>>>>\n"
<<"($++++++++++++++++$)\n"
<<"(| e = 'Encrypt' |)\n"
<<"(| d = 'Decrypt' |)\n"
<<"(| s = 'Settings' |)\n"
<<"(| m = 'Menu' |)\n"
<<"(| q = 'quit' |)\n"
<<"($++++++++++++++++$)\n";
goto over2;
break;
case 'q' : //Quit Program//
cout <<"Thanks for using 'Crypto'.\n";
system("PAUSE");
return 0;
break;
default : //Error with input part//
goto over;
break; }
}
}
VernonDozier 2,218 Posting Expert Featured Poster
Well you definitely have some problems reading the file in:
void doCrypt(char *einput) { //Encryption part of the program//
ifstream in; //Input//
ofstream out; //Output//
char buf; //Buffer for the input//
char a; //Collects data//
in.open(einput); //Opens the input from user//
if(in.fail()) { //If input fails do this//
cout <<"Could not find file: "<<einput<<endl; //Could not find input file of user//
system("PAUSE"); //Pause Program//
return; } //Return to menu//
if(in.is_open()) { //If file did open do this//
while(!in.eof()) { //I think my errors start here//
in.get(buf); } } //Supposed to get the whole text file and save it to buf//
in.close(); //Then close the input stream//
out.open(einput, ios::trunc); //Then open the output stream//
while(!out.eof()) { //Write while not end of file//
a=buf-0xFACA5; //Take one letter at a time, encrypt it using this encryption//
out << a; } //Write it out, and do this over, but it does not work//
out.close(); //Close the output stream//
cout <<"File Saved.\n"; //Tell user file was saved//
system("PAUSE"); //Pause program//
return; //Return to menu//
}
Line 4 - You've set aside a single character for the "buffer". The whole file can't fit in a single character. Use an array.
const int MAX_FILE_SIZE = 10000;
char* buf = new char[MAX_FILE_SIZE + 1]; // add one for null terminator
int actualFileSize = 0;
Now read in the file:
if(in.is_open()) {
while(!in.eof()) {
in.get(buf[actualFileSize]);
actualFileSize++;
}
}
in.close(); //Then close the input stream//
buf[actualFileSize] = 0; // add NULL terminator just to be safe.
There are some good threads on the potential perils of using eof that you may want to check out, but I left it pretty much as is.
Now go through buf a character at a time and encrypt it and output it to the output file (not sure why you were checking eof ()):
for (int i = 0; i < actualFileSize; i++)
{
a = buf[i] - 0xFACA5; // I still don't understand why you are doing this with a char.
out << a;
}
kernel>panic 3 Light Poster
Okay, thanks so much I added everything you put.
Now it is almost working I only have one error.
When I encrypt it looks like it worked but when I decrypt everything is there but it adds some weird character from my algorithm.
Do you know why?
I attached the new script.
#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;
void doCrypt(char *einput) { //Encryption part of the program//
ifstream in; //Input//
ofstream out; //Output//
const int MAX_FILE_SIZE = 10000;
char* buf = new char[MAX_FILE_SIZE + 1];
int actualFileSize = 0;
char a;
in.open(einput); //Opens the input from user//
if(in.fail()) { //If input fails do this//
cout <<"Could not find file: "<<einput<<endl; //Could not find input file of user//
system("PAUSE"); //Pause Program//
return; } //Return to menu//
if(in.is_open()) {
while(!in.eof()) {
in.get(buf[actualFileSize]);
actualFileSize++;
}
}
in.close();
buf[actualFileSize] = 0;
out.open(einput, ios::trunc);
for (int i = 0; i < actualFileSize; i++)
{
a = buf[i] - 0xFACA5;
out << a;
}
out.close();
cout <<"File Saved.\n";
system("PAUSE");
return;
}
void doDcrypt(char *einput) {
ifstream in;
ofstream out;
const int MAX_FILE_SIZE = 10000;
char* buf = new char[MAX_FILE_SIZE + 1];
int actualFileSize = 0;
char a;
in.open(einput);
if(in.fail()) {
cout <<"Could not find file: "<<einput<<endl;
system("PAUSE");
return; }
if(in.is_open()) {
while(!in.eof()) {
in.get(buf[actualFileSize]);
actualFileSize++;
}
}
in.close();
buf[actualFileSize] = 0;
out.open(einput, ios::trunc);
for (int i = 0; i < actualFileSize; i++)
{
a = buf[i] + 0xFACA5;
out << a;
}
out.close();
cout <<"File Saved.\n";
system("PAUSE");
return;
}
void doSet() {
}
int main(void) {
char comd;
char *einput=new char[20];
over:
system("CLS");
cout <<"*-----------------------------------*\n"
<<"| Welcome to Crypto! |\n"
<<"*-----------------------------------*\n";
cout <<"Please type a command,\n"
<<"or type 'm' for the menu.\n";
over2:
cout <<"Command: ";
while(true) { //Part of switch statement//
cin >> comd; //Get command//
switch(comd) { //Switch Menu//
case 'e' : //Encryption Part//
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doCrypt(einput);
goto over;
break;
case 'd' : //Decryption Part//
cout <<"Please type name of input file(.txt): ";
cin >> einput;
doDcrypt(einput);
goto over;
break;
case 's' :
doSet();
goto over;
break;
case 'm' :
cout <<"\n[ COMMAND MENU>>>>>\n"
<<"($++++++++++++++++$)\n"
<<"(| e = 'Encrypt' |)\n"
<<"(| d = 'Decrypt' |)\n"
<<"(| s = 'Settings' |)\n"
<<"(| m = 'Menu' |)\n"
<<"(| q = 'quit' |)\n"
<<"($++++++++++++++++$)\n";
goto over2;
break;
case 'q' :
cout <<"Thanks for using 'Crypto'.\n";
system("PAUSE");
return 0;
break;
default :
goto over;
break; }
}
}
VernonDozier 2,218 Posting Expert Featured Poster
Okay, thanks so much I added everything you put.
Now it is almost working I only have one error.When I encrypt it looks like it worked
Check again. Display buf and see if there's an extra character. It almost certainly DOESN'T work.
It's part of the debugging process. You need to display buf, as well as the number of characters in buf, before encrypting, to make sure it read in correctly (i.e. no extra character).
It's part of the problem with using eof () if you aren't careful. It often makes you go through the loop one too many times, so check the flag AGAIN in the loop to make sure you haven't:
if(in.is_open()) {
while(!in.eof()) {
in.get(buf[actualFileSize]);
if (!in.eof ())
actualFileSize++;
}
}
Same thing with the decryption part.
but when I decrypt everything is there but it adds some weird character from my algorithm.
Do you know why?
I attached the new script.
It very likely adds the weird character in the encryption part too, but you don't realize it because they're ALL weird characters. Use a word like "dog" and have the program add and subtract 1 rather than 0xFACA5 and there should be no weird characters and it'll be more obvious if there is. You should get "cnf". 0xFACA5 is too hard to check.
kernel>panic 3 Light Poster
I feel like such an idiot, I am!
But anyway thanks so much for your help, I am learning as I go.
I started C++ about 2 years ago and I am only 14 so I am getting there.
Thanks.
kernel>panic 3 Light Poster
Is there any way this could be faster?
Like using a different algorithm or something.
Just curious because it took like 6 seconds or so to do a 15.2MB text file.
Thanks Dustin.
VernonDozier 2,218 Posting Expert Featured Poster
Is there any way this could be faster?
Like using a different algorithm or something.
Just curious because it took like 6 seconds or so to do a 15.2MB text file.
Thanks Dustin.
You might be able to shave some time off if you didn't input from/output to the file a character at a time. Using fseek, fread, and fwrite from cstdio and handling things a block at a time might be much faster.
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.