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.

Recommended Answers

All 11 Replies

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");

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.

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.

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.

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.

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;
}

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.

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.

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.

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.

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.