[SOLVED] Encryption/Decryption issue while displaying contents of a text file in Qt
-
Okay so I wrote a very basic Encrpyt/Decrypt function in c++ mainly to see how it would go in QT I'm still new here so I'm a bit overwhelmed at the moment. My issue is this, The Encrypt and Decrypt functions work I've tested them outside of Qt how ever when I add them to my Qt project the file contents I'm attempting to Decrypt wont. I know the issue is how I'm calling Decrypt() I just can't figure out how to make it compatible here, so I'm going to drop some code snipets below so you guys can see what's going on!
std::string Encrypt(std::string Encrypted) // Encrypt
{std::string output = Encrypted; for (int i = 0; i < Encrypted.size(); i++) { output[i] = (output[i] -8 ); } return output;
}
std::string Decrypt(std::string Decrypted) // Decrypt
{std::string output = Decrypted; for (int i = 0; i < Decrypted.size(); i++) { output[i] = (output[i] +8); } return output;
}
Yes this is basically useless in terms of security but I'm just trying to get the hang of Qt so I'm keeping things simple! Any way the above is contained in a header file that I added to my project. And the following is where my issue comes into play.
void PassGuard::on_lineEdit_2_returnPressed() // Example of Encryption The below code works as intended
{std::string Name (ui->lineEdit_2->text().toStdString()); std::string Listing = "Listing: "; std::ofstream myfile; myfile.open("C:\\Passguard\\Passguard.txt", std::ios::app); ui->lineEdit_2->text().toStdString(); myfile << Encrypt(Listing) << Encrypt(Name) << std::endl; myfile << "\n" << "\n" << std::endl; myfile.close();
}
void PassGuard::on_pushButton_3_clicked() // This is where I would call Decrypt()
{
/*
but I'm at a loss as to where or how to call it, I've tried quite a few different ways and I get no errors it just wont Decrypt.
If I have to re-write Encrypt() and Decrypt() I will, I was just hoping I could use it here so I could get better accumulated to using Qt.
The code below does display the contents of the file, if I encrypt it it will show the encrypted data it just wont decrypt it also works
(Naturally ) with out the encryption.
*/std::string GetContent; QString Str = QString::fromStdString(Decrypt(GetContent)); QFile ReadMyFile("C://Passguard//Passguard.txt"); if(!ReadMyFile.open(QIODevice::ReadOnly)); QTextStream in(&ReadMyFile); ui->textBrowser->setText(in.readAll());
}
Any suggestions or advice on this would be amazing, like I said I can re-write it but I can't believe this wouldn't work I must be missing some thing :D
-
Decided to re-write it and I'm still having the same issue, here is what I can and can not do.
-
I can Encrypt the contents of the file
-
I can display the contents of the file inside of the QTextBrowser If I encrypt the information it will display it if I don't encrypt it , it will also still display inside of the QTextBrowser
-
I can Decrypt the file while using the QT Console with both the new code and the original.
-
I can not decrypt the encrypted Text inside of the QTextBrowser which is my ultimate goal.
QString Encrypt(QString Encrypted) // Encrypt, does what it's supposed to { QString ReadAll = Encrypted; for (int i = 0; i < Encrypted.size(); i++) { ReadAll[i].unicode() -=8; } return ReadAll; } QString Decrypt(QString Decrypted) // Decryption, will not decrypt { QString ReadAll = Decrypted; for (int i = 0; i < Decrypted.size(); i++) { ReadAll[i].unicode() +=8; } return ReadAll; } void PassGuard::on_pushButton_3_clicked() { /* Display the contents of a file */ QString GetContent; QFile ReadMyFile("C://Passguard//Passguard.txt"); if(! ReadMyFile.open(QIODevice::ReadOnly)); QTextStream in(&ReadMyFile); ui->textBrowser->setText(in.readAll()),Decrypt(GetContent); }
I've tried calling Decrypt(); in various ways
For example if I were to use the Encrypt/Decrypt functions in straight c++ I would do this ( better ways of doing this is just an example).
string GetContent = ""; ifstream openfile("C:\\Passguard\\Passguard.txt", ios::in); if (openfile.is_open()) { while(! openfile.eof()) // or .good etc.. { getline(openfile, GetContent); cout << Decrypt(GetContent) << endl; } }
Is there some thing I can do that is similar to the above code, while still making it display inside of the QTextBrowser?
(NOTE: If I run either function with out trying to display them inside of the text broswer Ie, QT console output the file does Decrypt)
-
-
@Suroh6 said:
std::string GetContent;
QString Str = QString::fromStdString(Decrypt(GetContent));
QFile ReadMyFile("C://Passguard//Passguard.txt");
if(!ReadMyFile.open(QIODevice::ReadOnly));
QTextStream in(&ReadMyFile);
ui->textBrowser->setText(in.readAll());Hi,
GetContent is uninitialized, then you create Str with the decrypted content of GetContent which would be empty or containing garbage. Next step, you open and read a file then put its content on your textBrowser. So it seems you decrypt an uninitialized string that you don't use and don't decrypt the data you read from a file and put it in the text browser.
-
@SGaist Yes that's why I re-wrote it, that's also part of my issue that posted in the new code ! :)
Consider the following code snipets.
/*
This Takes the user supplied text from the line edit, stores it in Usr, and then writes it to the text file. This works as intended!
Now if I add, Encrypt(Usr) , it encrypts that variable and writes it, this also works!
*/void PassGuard::on_lineEdit_3_returnPressed() { QString UserName = "User name: "; QString Usr = ui->lineEdit_3->text(); QFile file; file.setFileName("C:\\Passguard\\Passguard.txt"); file.open(QIODevice::Append); ui->lineEdit_3->text(); QTextStream OutStream(&file); OutStream << UserName << Encrypt(Usr); OutStream << "\n" << "\n"; file.close(); } // Now take a look at the new code I wrote to replace the old display function! void PassGuard::on_pushButton_3_clicked() { /* Display All function */ QString GetContent = ""; QFile ReadMyFile("C://Passguard//Passguard.txt"); if(! ReadMyFile.open(QIODevice::ReadOnly)); QTextStream in(&ReadMyFile); ui->textBrowser->setText(in.readAll()),Decrypt(GetContent);
}
// As you pointed out at this time QString GetContent is all but useless!
Which leads me to my question, please consider the following code!
string GetContent = "": ifstream openfile("C:\\Passguard\\Passguard.txt", ios::in); if (openfile.is_open()) { while(! openfile.eof()) { getline(openfile, GetContent); cout << Decrypt(GetContent) << endl; /* This code snipet will decrypt the contents of a text file, the reason being getline(openfile, GetContent); cout << Decrypt(GetContent); */ } } My Question is, can I do some thing similar to this in QT using QTextBrowser, some thing like following QTextStream in(GetContent, &ReadMyFile) ui->textBrowser->setText(Decrypt(GetContent)); I understand why the code doesn't work, I just can't figure out how to make it work lol. I'm still not very familiar with Qt and this is my attempt at better understanding how to work with it, is there some other way I can call Decrypt() and feed it GetContent so that it will display the Decypted text file inside of the QTextBrowser?
-
Why not use:
QString fileContent = file.readAll(); ui->textBrowser->setText(Decrypt(fileContent));
?