[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.

    [code]bool SimpleLoginGui::checkEncryptionFunction(QString sFile)
    {
    QFile inputFile(sFile);
    bool result = inputFile.open(QIODevice::ReadOnly);

    if (!result)
        return false;
    
    QTextStream in(&inputFile);
    QString fileContents = in.readAll();
    QString secretKey = theKey.toAscii();
    
    for (int i = 0, j = 0; i < fileContents.length(); i++, j++)
    {
        if (j == secretKey.length())
            j = 0;
    
        if (fileContents.localeAwareCompare((fileContents.toAscii().at(i) ^ secretKey.toAscii().at(j))) == 0)
            return true;
    }
    
    return false;
    

    }[/code]

    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.


  • Moderators

    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:
    @
    if (fileContents.localeAwareCompare((fileContents.toAscii().at(i) ^ secretKey.toAscii().at(j))) == 0)
    continue;
    else
    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.



  • 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?



  • 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.

    [code]// Project coded by Philip Simonson

    #include "simplelogingui.h"
    #include "ui_simplelogingui.h"

    SimpleLoginGui::SimpleLoginGui(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SimpleLoginGui)
    {
    ui->setupUi(this);
    this->theKey = "thekey_3987538";
    bool bResult = loadUserFile(tr("data/usernames.dat"));

    if (!bResult)
    {
        QMessageBox::warning(this, tr("Login Screen"), tr("FATAL ERROR: The usernames.dat file doesn't exist!"));
        exit(0);
    }
    

    }

    SimpleLoginGui::~SimpleLoginGui()
    {
    delete ui;
    }

    bool SimpleLoginGui::loadUserFile(QString sFile)
    {
    // load and open file
    QFile inputFile(sFile);
    bool result = inputFile.open(QIODevice::ReadOnly);

    if (!result)
        return false;
    
    // Create a text stream to read the file contents
    QTextStream in(&inputFile);
    
    bool resultChkEnc = checkEncryptionFunction(tr("data/usernames.dat"));
    
    if (!resultChkEnc)
    {
        QMessageBox::warning(this, tr("Login Screen"), tr("File usernames.dat wasn't encrypted... encrypting!"));
        bool resultEnc = encryptUserFile&#40;tr("data/usernames.dat"&#41;);
    
        if (!resultEnc)
        {
            QMessageBox::warning(this, tr("Login Screen"), tr("File encryption failed (data/usernames.dat)!"));
            return false;
        }
    
        QMessageBox::information(this, tr("Login Screen"), tr("You must restart this application to use the newly\nencrypted file... goodbye."));
        exit(0);
    }
    
    bool resultDec = decryptUserFile&#40;tr("data/usernames.dat"&#41;);
    
    if (!resultDec)
    {
        QMessageBox::warning(this, tr("Login Screen"), tr("Decrypting usernames.dat failed!"));
        return false;
    }
    
    inputFile.close();
    
    return true;
    

    }

    bool SimpleLoginGui::encryptUserFile(QString sFile)
    {
    // Open the usernames.dat file
    QFile inputFile(sFile);
    bool result = inputFile.open(QIODevice::ReadWrite);

    if (!result)
        return false;
    
    QTextStream in(&inputFile);
    QString fileContents = in.readAll();
    
    QString encData = calculateXor_for_encryption(fileContents.toAscii(), theKey.toAscii());
    in.seek(0);
    in.flush();
    in << encData;
    inputFile.close();
    
    return true;
    

    }

    bool SimpleLoginGui::decryptUserFile(QString sFile)
    {
    // Open the usernames.dat file
    QFile inputFile(sFile);
    bool result = inputFile.open(QIODevice::ReadOnly);

    if (!result)
        return false;
    
    QTextStream in(&inputFile);
    QString fileContents = in.readAll();
    
    QString decData = calculateXor_for_decryption(fileContents.toAscii(), theKey.toAscii());
    
    // rewrite the decrypted data to the same file
    QStringList listUserInfo = decData.split('\n');
    
    for (int i = 0; i < 2; i++)
        sUserInfo[i] = listUserInfo[i];
    
    // close the inputFile
    inputFile.close();
    
    return true;
    

    }

    bool SimpleLoginGui::checkEncryptionFunction(QString sFile)
    {
    QFile inputFile(sFile);
    bool result = inputFile.open(QIODevice::ReadOnly);

    if (!result)
        return false;
    
    QTextStream in(&inputFile);
    QByteArray fileContents = in.readAll().toAscii();
    QByteArray secretKey = theKey.toAscii();
    
    QByteArray resultArray;
    
    for (int i = 0, j = 0; i < fileContents.length(); i++, j++)
    {
        if (j == secretKey.length())
            j = 0;
    
        resultArray.append(fileContents.at(i) ^ secretKey.at(j));
    }
    
    QString data = fileContents;
    QString resultString = resultArray;
    
    return QString::compare(fileContents, resultString, Qt::CaseSensitive);
    

    }

    QByteArray SimpleLoginGui::calculateXor_for_encryption(const QByteArray& data, const QByteArray& key)
    {
    if (key.isEmpty())
    return data;

    QByteArray result;
    for (int i = 0, j = 0; i < data.length(); i++, j++)
    {
        if (j == key.length())
            j = 0;
        result.append(data.at(i) ^ key.at(j));
    }
    
    return result;
    

    }

    QByteArray SimpleLoginGui::calculateXor_for_decryption(const QByteArray& data, const QByteArray& key)
    {
    if (key.isEmpty())
    return data;

    QByteArray result;
    for (int i = 0, j = 0; i < data.length(); i++, j++)
    {
        if (j == key.length())
            j = 0;
        result.append(key.at(j) ^ data.at(i));
    }
    
    return result;
    

    }

    void SimpleLoginGui::on_closeButton_clicked()
    {
    // Exit the program after a message to the user
    QMessageBox::information(this, tr("Login Screen"), tr("You have decided to cancel logon!\nGoodbye!"));
    exit(0);
    }

    void SimpleLoginGui::on_loginButton_clicked()
    {
    // initialize the username and passcode variables
    QString username = ui->nameEdit->text();
    QString passcode = ui->codeEdit->text();

    if (username == "" && passcode == "")
    {
        // Enter something
        QMessageBox::information(this, tr("Login Screen"), tr("You must enter something in both of the text boxes!"));
    }
    else if (username == "" || passcode == "")
    {
        // Enter something
        QMessageBox::information(this, tr("Login Screen"), tr("Username or passcode field empty."));
    }
    else if (username == sUserInfo[0] && passcode == sUserInfo[1])
    {
        // You are successfully logged on to the program
        ui->nameEdit->clear();
        ui->codeEdit->clear();
        QMessageBox::information(this, tr("Login Screen"), tr("You have successfully logged on to this program!"));
    }
    else
    {
        // You are not successfully logged on to the program
        ui->nameEdit->clear();
        ui->codeEdit->clear();
        QMessageBox::information(this, tr("Login Screen"), tr("You are not authorized... contacting authorities!"));
    }
    

    }[/code]

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


  • Moderators

    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:-)

    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.

    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.



  • If it is a simple encryption scheme you're looking for, perhaps my "SimpleCrypt":/wiki/Simple_encryption 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.