I have been trying to create a custom cipher it will encrypt a password in a following manner
- generates two random numbers using rand()
- adds one number to single character of string
- 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
- 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