Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. Write QCA Encrypt Decrypt to file
Forum Updated to NodeBB v4.3 + New Features

Write QCA Encrypt Decrypt to file

Scheduled Pinned Locked Moved Solved 3rd Party Software
3 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    knasan
    wrote on last edited by
    #1

    Hello everybody,

    I would like a file with AES locks, for this I use QCA.
    Closing seems to work, but I can not decode it anymore. Everything I've tried so far changes the file slightly (another MD5 comes out) or makes it completely useless.

    // code to encrypt and write to file.

     QCA::Initializer init = QCA::Initializer();
        if(QCA::isSupported("aes256-cbc-pkcs7"))
        {
            // input filename
            QTextStream streamin(stdin);
            printf("File: ");
            QString filename = streamin.readLine(0);
    
            QFile inputFile(filename);
            if(!inputFile.open(QIODevice::ReadOnly))
            {
                qDebug() << "problem while reading ";
                return -1;
            }
    
            const QByteArray ReadFileByteArray = inputFile.readAll();
            inputFile.close();
    
            qDebug() << "size of clear file" << ReadFileByteArray.size();
    
            QCA::SecureArray inputSA = ReadFileByteArray;
            qDebug() << "size of inputSA: " << inputSA.size();
    
            // Password input
            printf("Password: ");
            QString password = streamin.readLine(0);
    
            QString ki = password;
            QCA::SymmetricKey key = ki.toUtf8();
            QCA::InitializationVector iv = ki.toUtf8();
    
            QCA::Cipher cipher(QString("aes256"), QCA::Cipher::CBC, QCA::Cipher::DefaultPadding, QCA::Encode, key, iv);
    
            QCA::SecureArray encoded = cipher.update(inputSA);
            qDebug() << "size of encoded: " << encoded.size();
    
            if(!cipher.ok())
            {
                qCritical("update failed");
                return -1;
            }
    
            cipher.setup(QCA::Encode, key, iv);
            QCA::SecureArray original = cipher.process(encoded);
            qDebug() << "size of original" << original.size();
    
            if(!cipher.ok())
            {
                qCritical("Final failed");
                return -1;
            }
    
            QByteArray originaldata = original.toByteArray();
            qDebug() << "size of originaldata: " << originaldata.size();
    
            QString writeFileName = filename + ".e";
    
            QFile file1(writeFileName);
            if(!file1.open(QIODevice::WriteOnly))
            {
                qCritical("problem while writing ");
                return -1;
            } else {
    
                QTextStream streamout(&file1);
                streamout << encoded.toByteArray();
                streamout.flush();
            }
            file1.close();
    
        } else {
            qDebug() << "no";
            qCritical("aes256-cbc-pkcs7");
        }
    
        return 0;
    }
    

    // code to decrypt

     QCA::Initializer init = QCA::Initializer();
        if(QCA::isSupported("aes256-cbc-pkcs7"))
        {
    
            printf("File: ");
            QTextStream streamin(stdin);
            QString readFileName = streamin.readLine(0);
    
            printf("Password: ");
            QString password = streamin.readLine(0);
    
            QString ki = password;
            QCA::SymmetricKey key = ki.toUtf8();
            QCA::InitializationVector iv = ki.toUtf8();
            QCA::Cipher cipher(QString("aes256"), QCA::Cipher::CBC, QCA::Cipher::DefaultPadding, QCA::Encode, key, iv);
    
            QFile inputFile(readFileName);
            if(!inputFile.open(QIODevice::ReadOnly))
            {
                qCritical("problem while reading");
                return -1;
            }
    
            const QByteArray ReadFileByteArray = inputFile.readAll();
            inputFile.close();
    
            cipher.setup(QCA::Decode, key, iv);
            QCA::SecureArray decoded = cipher.update(ReadFileByteArray);
    
            if(!cipher.ok())
            {
                qCritical("Final failed");
                return -1;
            }
    
            // wie kann man das Kennwort überprüfen?
    
            int cutFile = readFileName.length() -2;
    
            QFile wFile(readFileName.left(cutFile));
            if(!wFile.open(QIODevice::WriteOnly)) {
                qCritical("problem reading file");
                return -1;
            } else {
                /*QTextStream streamout(&wFile);
                streamout << decoded.;
                streamout.flush();*/
    
                QByteArray original = decoded.toByteArray();
    
                QString filename = readFileName.left(cutFile);
                QFile wf(filename);
                wf.open(QIODevice::WriteOnly);
                QDataStream out(&wf);
                out << original;
                wf.close();
    
                wFile.close();
            }
    
        } else {
            qDebug() << "no";
            qCritical("aes256-cbc-pkcs7");
        }
    
        return 0;
    }
    

    QDataStream does not work at all.
    If I use QTextStream, the file can be opened, but not 100 percent the same. Unfortunately, a file.write (data.toByteArray ()) alters the file so that it does not fit 100 percent anymore.

    Does anybody know what I'm doing wrong?

    Thank you for your help.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not use QFile::write directly rather than the stream operators ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        knasan
        wrote on last edited by
        #3

        Thank you. Directly over QFile :: wirite as ByteArray works without problems. Now all I have to do is find out how to check the password. But for this I make another thread on as soon as I can deal with it.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved