Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Encrypt/Decrypt file in QT. How?
Forum Updated to NodeBB v4.3 + New Features

Encrypt/Decrypt file in QT. How?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 6.3k Views 3 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #1

    Hello all!
    Is there in-box solution in QT for encrypting/decrypting file in Qt? If no what is better to use in QT? I am trying to avoid to use plain C and OpenSSL directly. Is there any library or framework for this issue?

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

      Hi,

      QCA from the KDE project.

      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
      2
      • MucipM Offline
        MucipM Offline
        Mucip
        wrote on last edited by
        #3

        Hi,
        I use this and liked...

        https://wiki.qt.io/Simple_encryption_with_SimpleCrypt

        Regards,
        Mucip:)

        1 Reply Last reply
        0
        • O Offline
          O Offline
          OPit
          wrote on last edited by
          #4

          Hi,
          openssl is pretty easy to use :

          uchar const KEY_CRYPT[] = "-UKfdjdskf--**fsnx"; // What you want ...
          uchar const KEY_IV[] = "Compl3xES3quence"; // What you want ...
          
          #include <openssl/aes.h>
          #include <openssl/evp.h>
          #include <QFile>
          
          bool decryptOrEncrypt(bool encrypt)
          {
              QFile srcFile;
              QFile destFile;
          
              if(encrypt)
              {
                  srcFile.setFileName(m_destinationFile);
                  destFile.setFileName(m_encryptedFile);
              }
              else
              {
                  srcFile.setFileName(m_encryptedFile);
                  destFile.setFileName(m_destinationFile);
              }
          
              if(!srcFile.open(QIODevice::ReadOnly))
              {
                  return false;
              }
          
              if(!destFile.open(QIODevice::WriteOnly))
              {
                  return false;
              }
          
              const int MAX_BUFFER_SIZE = 1024;
              int out_len;
              EVP_CIPHER_CTX * ctx = EVP_CIPHER_CTX_new();
          
              EVP_CipherInit(ctx, EVP_aes_128_cbc(), KEY_CRYPT, KEY_IV , encrypt);
              int blocksize = EVP_CIPHER_CTX_block_size(ctx);
              QByteArray cipher_buf;
              cipher_buf.resize(MAX_BUFFER_SIZE + blocksize);
          
              qint64 size = 0;
          
              while(!srcFile.atEnd())
              {
                  // Read in data in blocks until EOF. Update the ciphering with each read.
                  QByteArray inRead = srcFile.read(MAX_BUFFER_SIZE);
                  EVP_CipherUpdate(ctx, reinterpret_cast<uchar *>(cipher_buf.data()), &out_len, reinterpret_cast<uchar *>(inRead.data()), inRead.count());
                  destFile.write(cipher_buf.data(), out_len);
                  size += out_len;
              }
          
              // Now cipher the final block and write it out.
              EVP_CipherFinal(ctx, reinterpret_cast<uchar *>(cipher_buf.data()), &out_len);
              destFile.write(cipher_buf.data(), out_len);
              size += out_len;
              destFile.close();
              srcFile.close();
          
              EVP_CIPHER_CTX_free(ctx);
          
              return true;
          }
          
          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            It should be a really strong coffee before that would look even remotely easy to me..:)
            But thank you for the sample.

             EVP_CipherUpdate(ctx, reinterpret_cast<uchar *>(cipher_buf.data()), &out_len, reinterpret_cast<uchar *>(inRead.data()), inRead.count());
            
            1 Reply Last reply
            1
            • B Offline
              B Offline
              bogong
              wrote on last edited by
              #6

              Solution found https://github.com/bricke/Qt-AES Issue closed.

              1 Reply Last reply
              1

              • Login

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