-8

I have been trying to create a custom cipher it will encrypt a password in a following manner

  1. generates two random numbers using rand()
  2. adds one number to single character of string
  3. if ascii value becomes greater than 126 then it subtracts second number from that character then it stores it in character queue along with index of it in other queue
  4. if ascii is lower than 126 it goes straight to stack for reverse order

but I'm having problem with encryption, an if statement is not working code is given below

void encrypt(string data) {
        srand(time(0));
        fstream myFile("difference.dat", ios::binary);
        fstream myfile("difference2.dat", ios::binary);
        int difference = rand() % 20;
        int secondDifference = 20 + (rand() % 20);

        cout << difference << "  " << secondDifference <<endl;
        myFile.write(reinterpret_cast<char*>(&difference), sizeof(difference));
        myfile.write(reinterpret_cast<char*>(&secondDifference), sizeof(secondDifference));
        for (int i = 0; i < data.length(); i++) {
            char var1 = data[i];
            if (var1 > 32 && var1 < 127) {
                char var2 = var1 + difference;
                if (var2 > 126) {
                    char var3 = var1 - secondDifference;
                    cout << var3;
                    charQ.push(var3);
                    intQ.push(i);
                }
                stack1.push(var2);
            }
        }


        fstream binaryFile("encrypted.dat", ios::binary);
        while (!stack1.isEmpty()) {
            char c = stack1.top();
            binaryFile.write(reinterpret_cast<char*>(&c), sizeof(c));
            stack1.pop();
        }

        binaryFile.close();

        cout << "Encrypted password: ";
        ifstream encryptedFile("encrypted.dat", ios::binary);
        char encryptedChar;
        while (encryptedFile.read(reinterpret_cast<char*>(&encryptedChar), sizeof(encryptedChar))) {
            cout << encryptedChar;
        }
        encryptedFile.close();
        cout << endl;

        myFile.close();
    }

I have been testing and fragment below is not generating an output

if (var2 > 126) {
                    char var3 = var1 - secondDifference;
                    cout << var3;
                    charQ.push(var3);
                    intQ.push(i);
                }
                stack1.push(var2);

I have tried to put to move this fragment up and down but I really can't find any error myself nor does the ChatGPT

LordVader
  • 15
  • 5
  • 2
    Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Richard Critten Jun 10 '23 at 20:00
  • This `char var3 = var1 - secondDifference;` should be this `char var3 = var2 - secondDifference;` – Richard Critten Jun 10 '23 at 20:02
  • that doesn't solve the question – LordVader Jun 10 '23 at 20:14
  • *if statement is not working* - I have no idea what it means. [What does MCVE mean?](https://meta.stackoverflow.com/questions/366988/what-does-mcve-mean) – Evg Jun 10 '23 at 20:17
  • 2
    `char` is often a signed type. Adding two large values (say, `(char)(100 + 100)`) technically exhibits undefined behavior, but in practice it'll wrap around to a negative value (in my example, `-56`). Then a condition like `var2 > 126` will be false, because a negative value is not in fact greater than 126. – Igor Tandetnik Jun 10 '23 at 20:23
  • "generates two random numbers using rand()" - `rand()` has really poor properties, like very limited range, poor randomness, I'd advise avoiding it. See also: [rand() Considered Harmful](https://learn.microsoft.com/en-us/events/goingnative-2013/rand-considered-harmful) - really good talk. – Jesper Juhl Jun 10 '23 at 23:17
  • 2
    *"I have been testing and fragment below is not generating an output"* -- This is progress, but there is more to do. You should focus on this fragment, determine the values when it fails, simplify some more, then expand it to be complete enough for someone to copy-compile-run it to reproduce your result. For example: `int main() { char var1 = 'a'; int secondDifference = 22; char var3 = var1 - secondDifference; std::cout << var3; }` (assume the `if` conditional is true, and no need to go on after the statement that fails). Replace the hardcoded values with values that demonstrate the problem. – JaMiT Jun 11 '23 at 08:33

1 Answers1

4

The logical flaw is very simple to explain:

char var1 = data[i];

Let's say that var1 is 126 now, at this point. And the difference was produced by a random number generator, and its value is now 10. Let's see what happens.

if (var1 > 32 && var1 < 127)

This is true, hooray!

char var2 = var1 + difference;

You might be surprised to learn that adding 10 to 126, here, does not result in var2 being set to 136. Can you explain how you think this might be possible, since signed 8-bit chars, in C++, can only hold values in the range -128 to 127?

if (var2 > 126) {

var2 is not bigger than 126 here, sorry. This is the obvious logical flaw in the shown code.

You will need to detect overflow in some other way -- either by using int values instead of chars, or in some other way. It's somewhat unclear from the shown code, and its description, the best fix, but this answers why the "if statement is not working".

I really can't find any error myself nor does the ChatGPT

It's very unlikely that a dumb chatbot can figure out the error in even a trivial C++ program. ChatGPT is just a dumb chatbot, it is not a C++ code analysis tool.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148