May 3, 2012

zerocool zerocool
Lab Rat
8 posts

[SOLVED] Can someone please help with QT C++ (How to check for file encryption)???

 

What I’m trying to do is check to see if the file is encrypted and if it is decrypt it.. not encrypted encrypt it. But I don’t know how to write the check because, I never done that before. I’ve tried several ways I thought would work but, no dice.. Below is the code that I have so far… that’s supposed to do that very check.

  1. bool SimpleLoginGui::checkEncryptionFunction(QString sFile)
  2. {
  3.     QFile inputFile(sFile);
  4.     bool result = inputFile.open(QIODevice::ReadOnly);
  5.  
  6.     if (!result)
  7.         return false;
  8.  
  9.     QTextStream in(&inputFile);
  10.     QString fileContents = in.readAll();
  11.     QString secretKey = theKey.toAscii();
  12.  
  13.     for (int i = 0, j = 0; i < fileContents.length(); i++, j++)
  14.     {
  15.         if (j == secretKey.length())
  16.             j = 0;
  17.  
  18.         if (fileContents.localeAwareCompare((fileContents.toAscii().at(i) ^ secretKey.toAscii().at(j))) == 0)
  19.             return true;
  20.     }
  21.  
  22.     return false;
  23. }

Please help thanks in advance for any help.. to solving my problem.
EDIT: Also I would like to ask how do I check to see if it is encrypted or not… hope I didn’t already ask that but I’m tired so please be gentle.

EDIT: I figured it out… never mind.

6 replies

May 3, 2012

sierdzio sierdzio
Area 51 Engineer
4152 posts

I’m not sure I understand what you want to achieve… why do you return true on first character match? Is it some special feature of this encryption method? I think (might be wrong, mind you) that you actually want to check whether every character in the file complies with the secretKey mask, so it would be better to use something like:

  1. if (fileContents.localeAwareCompare((fileContents.toAscii().at(i) ^ secretKey.toAscii().at(j))) == 0)
  2.     continue;
  3. else
  4.     return false;

Also, I’m not sure casting to ASCII is a good idea – what if the file itself contains UTF-8 characters? QString internally uses UTF-16, and has a lot of convenience methods like ::toUtf8() etc. Might be a good idea to look into those.

 Signature 

(Z(:^

May 3, 2012

Andre Andre
Robot Herder
6296 posts

The main problem is: you’re telling us nothing on how you distinguish an unencrypted file from an encrypted one. I don’t see how comparing with the secret key would help you in that respect either?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

May 3, 2012

zerocool zerocool
Lab Rat
8 posts

Okay here is the whole source in the SimpleLoginGui.cpp file…
What I’m trying to do is compare the file against the key or something to check and see if that file is encrypted. But I came up with errors or it just plain doesn’t work.

  1. // Project coded by Philip Simonson
  2.  
  3. #include "simplelogingui.h"
  4. #include "ui_simplelogingui.h"
  5.  
  6. SimpleLoginGui::SimpleLoginGui(QWidget *parent) :
  7.     QWidget(parent),
  8.     ui(new Ui::SimpleLoginGui)
  9. {
  10.     ui->setupUi(this);
  11.     this->theKey = "thekey_3987538";
  12.     bool bResult = loadUserFile(tr("data/usernames.dat"));
  13.  
  14.     if (!bResult)
  15.     {
  16.         QMessageBox::warning(this, tr("Login Screen"), tr("FATAL ERROR: The usernames.dat file doesn't exist!"));
  17.         exit(0);
  18.     }
  19. }
  20.  
  21. SimpleLoginGui::~SimpleLoginGui()
  22. {
  23.     delete ui;
  24. }
  25.  
  26. bool SimpleLoginGui::loadUserFile(QString sFile)
  27. {
  28.     // load and open file
  29.     QFile inputFile(sFile);
  30.     bool result = inputFile.open(QIODevice::ReadOnly);
  31.  
  32.     if (!result)
  33.         return false;
  34.  
  35.     // Create a text stream to read the file contents
  36.     QTextStream in(&inputFile);
  37.  
  38.     bool resultChkEnc = checkEncryptionFunction(tr("data/usernames.dat"));
  39.  
  40.     if (!resultChkEnc)
  41.     {
  42.         QMessageBox::warning(this, tr("Login Screen"), tr("File usernames.dat wasn't encrypted... encrypting!"));
  43.         bool resultEnc = encryptUserFile(tr("data/usernames.dat"));
  44.  
  45.         if (!resultEnc)
  46.         {
  47.             QMessageBox::warning(this, tr("Login Screen"), tr("File encryption failed (data/usernames.dat)!"));
  48.             return false;
  49.         }
  50.  
  51.         QMessageBox::information(this, tr("Login Screen"), tr("You must restart this application to use the newly\nencrypted file... goodbye."));
  52.         exit(0);
  53.     }
  54.  
  55.     bool resultDec = decryptUserFile(tr("data/usernames.dat"));
  56.  
  57.     if (!resultDec)
  58.     {
  59.         QMessageBox::warning(this, tr("Login Screen"), tr("Decrypting usernames.dat failed!"));
  60.         return false;
  61.     }
  62.  
  63.     inputFile.close();
  64.  
  65.     return true;
  66. }
  67.  
  68. bool SimpleLoginGui::encryptUserFile(QString sFile)
  69. {
  70.     // Open the usernames.dat file
  71.     QFile inputFile(sFile);
  72.     bool result = inputFile.open(QIODevice::ReadWrite);
  73.  
  74.     if (!result)
  75.         return false;
  76.  
  77.     QTextStream in(&inputFile);
  78.     QString fileContents = in.readAll();
  79.  
  80.     QString encData = calculateXor_for_encryption(fileContents.toAscii(), theKey.toAscii());
  81.     in.seek(0);
  82.     in.flush();
  83.     in << encData;
  84.     inputFile.close();
  85.  
  86.     return true;
  87. }
  88.  
  89. bool SimpleLoginGui::decryptUserFile(QString sFile)
  90. {
  91.     // Open the usernames.dat file
  92.     QFile inputFile(sFile);
  93.     bool result = inputFile.open(QIODevice::ReadOnly);
  94.  
  95.     if (!result)
  96.         return false;
  97.  
  98.     QTextStream in(&inputFile);
  99.     QString fileContents = in.readAll();
  100.  
  101.     QString decData = calculateXor_for_decryption(fileContents.toAscii(), theKey.toAscii());
  102.  
  103.     // rewrite the decrypted data to the same file
  104.     QStringList listUserInfo = decData.split('\n');
  105.  
  106.     for (int i = 0; i < 2; i++)
  107.         sUserInfo[i] = listUserInfo[i];
  108.  
  109.     // close the inputFile
  110.     inputFile.close();
  111.  
  112.     return true;
  113. }
  114.  
  115. bool SimpleLoginGui::checkEncryptionFunction(QString sFile)
  116. {
  117.     QFile inputFile(sFile);
  118.     bool result = inputFile.open(QIODevice::ReadOnly);
  119.  
  120.     if (!result)
  121.         return false;
  122.  
  123.     QTextStream in(&inputFile);
  124.     QByteArray fileContents = in.readAll().toAscii();
  125.     QByteArray secretKey = theKey.toAscii();
  126.  
  127.     QByteArray resultArray;
  128.  
  129.     for (int i = 0, j = 0; i < fileContents.length(); i++, j++)
  130.     {
  131.         if (j == secretKey.length())
  132.             j = 0;
  133.  
  134.         resultArray.append(fileContents.at(i) ^ secretKey.at(j));
  135.     }
  136.  
  137.     QString data = fileContents;
  138.     QString resultString = resultArray;
  139.  
  140.     return QString::compare(fileContents, resultString, Qt::CaseSensitive);
  141. }
  142.  
  143. QByteArray SimpleLoginGui::calculateXor_for_encryption(const QByteArray& data, const QByteArray& key)
  144. {
  145.     if (key.isEmpty())
  146.         return data;
  147.  
  148.     QByteArray result;
  149.     for (int i = 0, j = 0; i < data.length(); i++, j++)
  150.     {
  151.         if (j == key.length())
  152.             j = 0;
  153.         result.append(data.at(i) ^ key.at(j));
  154.     }
  155.  
  156.     return result;
  157. }
  158.  
  159. QByteArray SimpleLoginGui::calculateXor_for_decryption(const QByteArray& data, const QByteArray& key)
  160. {
  161.     if (key.isEmpty())
  162.         return data;
  163.  
  164.     QByteArray result;
  165.     for (int i = 0, j = 0; i < data.length(); i++, j++)
  166.     {
  167.         if (j == key.length())
  168.             j = 0;
  169.         result.append(key.at(j) ^ data.at(i));
  170.     }
  171.  
  172.     return result;
  173. }
  174.  
  175. void SimpleLoginGui::on_closeButton_clicked()
  176. {
  177.     // Exit the program after a message to the user
  178.     QMessageBox::information(this, tr("Login Screen"), tr("You have decided to cancel logon!\nGoodbye!"));
  179.     exit(0);
  180. }
  181.  
  182. void SimpleLoginGui::on_loginButton_clicked()
  183. {
  184.     // initialize the username and passcode variables
  185.     QString username = ui->nameEdit->text();
  186.     QString passcode = ui->codeEdit->text();
  187.  
  188.     if (username == "" && passcode == "")
  189.     {
  190.         // Enter something
  191.         QMessageBox::information(this, tr("Login Screen"), tr("You must enter something in both of the text boxes!"));
  192.     }
  193.     else if (username == "" || passcode == "")
  194.     {
  195.         // Enter something
  196.         QMessageBox::information(this, tr("Login Screen"), tr("Username or passcode field empty."));
  197.     }
  198.     else if (username == sUserInfo[0] && passcode == sUserInfo[1])
  199.     {
  200.         // You are successfully logged on to the program
  201.         ui->nameEdit->clear();
  202.         ui->codeEdit->clear();
  203.         QMessageBox::information(this, tr("Login Screen"), tr("You have successfully logged on to this program!"));
  204.     }
  205.     else
  206.     {
  207.         // You are not successfully logged on to the program
  208.         ui->nameEdit->clear();
  209.         ui->codeEdit->clear();
  210.         QMessageBox::information(this, tr("Login Screen"), tr("You are not authorized... contacting authorities!"));
  211.     }
  212. }

Please help guys, I know there is a way to accomplish this. Thanks in advance.

May 3, 2012

Tobias Hunger Tobias Hunger
Area 51 Engineer
3383 posts
  1. That is not encryption at all. Usually you save the user name in plaintext. Then you add some salt (a random value). Finally you hash “salt:password” a couple of hundred times (to make it more expensive to guess passwords) and store that. Way more secure than this ridiculous encryption scheme:-)
  2. You do not want the file names to be translatable. Otherwise the user will be able to log in or not, depending on the language he selected.
  3. How about prepending “ENCRYPTED” or some other magic to the encrypted file? Then you only need to check for the magic to decide whether the file is encrypted or not.

May 3, 2012

Andre Andre
Robot Herder
6296 posts

If it is a simple encryption scheme you’re looking for, perhaps my SimpleCrypt class can be of use? Note that it only implements the actual encryption & decryptio. It is up to you to store the details and retreive them. Also note that it is not extremely strong, though probably a lot stronger than your implementation.

I would indeed use some magic value in the begining of the binary file to mark it as an encrypted file, as Tobias suggests.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

May 3, 2012

zerocool zerocool
Lab Rat
8 posts

Thanks for the replies you guys.. I just wanted a very simple encryption (XOR). Just to try it out and see if I could do it because, this (mind you) is my first time working with encryption at all in QT.

 
  ‹‹ Help with setFieldWidth in QTextStream      readyRead signal of QUdpSocket never emit on windows ››

You must log in to post a reply. Not a member yet? Register here!