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. SimpleCrypt changing binary data after encryption/decryption
Forum Update on Monday, May 27th 2025

SimpleCrypt changing binary data after encryption/decryption

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 460 Views
  • 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.
  • I Offline
    I Offline
    imfp
    wrote on 14 Jul 2023, 15:42 last edited by
    #1

    I'm using SimpleCrypt in my job code 'cause it was easy to add it (just add a .h and a .cpp file to projetct). I need to encrypt a .bin file, write the cyphertext it in another file, read it and decrypt. I'm doing it with QDataStream and SympleCrypt, but when I visually compare the initial .bin file and the .bin file at the end of the process, the end .bin file got some new data at the ending and begining. I tried to debug every step of the process and discovered that SympleCrypt decryption is given a little bit different data then my inicial .bin file.
    I'd like to know if it's a real problem or my code is just wrong. My code is using the default key SympleCrypt documentation sugests just for tests and my SO is Windows 10.

    void Cryptography::encrypt_binary(QString bin_file)
    {
        //reading bin file
        QFile binary_file(bin_file); 
        if (!binary_file.open(QIODevice::ReadOnly))
        {
            qDebug() << "can't open bin file";
        }
        QByteArray bin_data = binary_file.readAll();
        binary_file.close();
    
        //config compression and protection
        SimpleCrypt crypto(Q_UINT64_C(0x0c2ad4a4acb9f023));
        crypto.setCompressionMode(SimpleCrypt::CompressionAlways);
        crypto.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);
        QBuffer buffer;
        buffer.open(QIODevice::WriteOnly);
        QDataStream s(&buffer);
        s.setVersion(QDataStream::Qt_4_7);
        s << bin_data;
    
        //encrypt bin file
        QByteArray myCypherText = crypto.encryptToByteArray(buffer.data());
        if (!crypto.lastError() == SimpleCrypt::ErrorNoError)
        {
             qDebug() << crypto.lastError();
        }
        buffer.close();
    
        //save cypher in a dat file
        QByteArray erro = "Error in encrypt function.";
        QString filename="C:/Users/test.bin";
        QFile cypher(filename);
        if (cypher.open(QIODevice::ReadWrite))
        {
            QDataStream stream(&cypher);
            stream << myCypherText;
        }
        else
        {
            qDebug(erro);
        }
        cypher.close();
    }
    
    QByteArray Cryptography::decrypt_binary(QString cypher)
    {
         //reading bin file
         QFile cypher_file("C:/Users/test.bin");
         if (!cypher_file.open(QIODevice::ReadOnly))
         {
             qDebug() << "can't open cypher file";
         }
         QByteArray cypher_f;
         QDataStream stream(&cypher_file);
         stream >> cypher_f;
         cypher_file.close();
    
         //decrypt cypher back to bin
         SimpleCrypt crypto(Q_UINT64_C(0x0c2ad4a4acb9f023));
         QByteArray plaintext = crypto.decryptToByteArray(cypher_f);
         if (!crypto.lastError() == SimpleCrypt::ErrorNoError)
         {
             qDebug() << crypto.lastError();
         }
    
         //save into a bin file
         QString filename="C:/Users/test2.bin";
         QFile filee(filename);
         if (filee.open(QIODevice::ReadWrite))
         {
             QDataStream stream(&filee);
             stream << plaintext;
         }
         return plaintext;
    }
    

    My original .bin file starts with

    0100 0000 ffff ffff ffff ffff ffff ffff
    

    and the final file I got starts like all my previous data was shifted to fit theese new data

    0000 2004 0000 2000 0100 0000 ffff ffff
    
    C 1 Reply Last reply 14 Jul 2023, 15:46
    0
    • I imfp
      14 Jul 2023, 15:42

      I'm using SimpleCrypt in my job code 'cause it was easy to add it (just add a .h and a .cpp file to projetct). I need to encrypt a .bin file, write the cyphertext it in another file, read it and decrypt. I'm doing it with QDataStream and SympleCrypt, but when I visually compare the initial .bin file and the .bin file at the end of the process, the end .bin file got some new data at the ending and begining. I tried to debug every step of the process and discovered that SympleCrypt decryption is given a little bit different data then my inicial .bin file.
      I'd like to know if it's a real problem or my code is just wrong. My code is using the default key SympleCrypt documentation sugests just for tests and my SO is Windows 10.

      void Cryptography::encrypt_binary(QString bin_file)
      {
          //reading bin file
          QFile binary_file(bin_file); 
          if (!binary_file.open(QIODevice::ReadOnly))
          {
              qDebug() << "can't open bin file";
          }
          QByteArray bin_data = binary_file.readAll();
          binary_file.close();
      
          //config compression and protection
          SimpleCrypt crypto(Q_UINT64_C(0x0c2ad4a4acb9f023));
          crypto.setCompressionMode(SimpleCrypt::CompressionAlways);
          crypto.setIntegrityProtectionMode(SimpleCrypt::ProtectionHash);
          QBuffer buffer;
          buffer.open(QIODevice::WriteOnly);
          QDataStream s(&buffer);
          s.setVersion(QDataStream::Qt_4_7);
          s << bin_data;
      
          //encrypt bin file
          QByteArray myCypherText = crypto.encryptToByteArray(buffer.data());
          if (!crypto.lastError() == SimpleCrypt::ErrorNoError)
          {
               qDebug() << crypto.lastError();
          }
          buffer.close();
      
          //save cypher in a dat file
          QByteArray erro = "Error in encrypt function.";
          QString filename="C:/Users/test.bin";
          QFile cypher(filename);
          if (cypher.open(QIODevice::ReadWrite))
          {
              QDataStream stream(&cypher);
              stream << myCypherText;
          }
          else
          {
              qDebug(erro);
          }
          cypher.close();
      }
      
      QByteArray Cryptography::decrypt_binary(QString cypher)
      {
           //reading bin file
           QFile cypher_file("C:/Users/test.bin");
           if (!cypher_file.open(QIODevice::ReadOnly))
           {
               qDebug() << "can't open cypher file";
           }
           QByteArray cypher_f;
           QDataStream stream(&cypher_file);
           stream >> cypher_f;
           cypher_file.close();
      
           //decrypt cypher back to bin
           SimpleCrypt crypto(Q_UINT64_C(0x0c2ad4a4acb9f023));
           QByteArray plaintext = crypto.decryptToByteArray(cypher_f);
           if (!crypto.lastError() == SimpleCrypt::ErrorNoError)
           {
               qDebug() << crypto.lastError();
           }
      
           //save into a bin file
           QString filename="C:/Users/test2.bin";
           QFile filee(filename);
           if (filee.open(QIODevice::ReadWrite))
           {
               QDataStream stream(&filee);
               stream << plaintext;
           }
           return plaintext;
      }
      

      My original .bin file starts with

      0100 0000 ffff ffff ffff ffff ffff ffff
      

      and the final file I got starts like all my previous data was shifted to fit theese new data

      0000 2004 0000 2000 0100 0000 ffff ffff
      
      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Jul 2023, 15:46 last edited by
      #2

      Please remove all the useless QDataStream stuff - you're pushing it two times through a QDataStream (for unknown reason) during writing but only once during reading.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      I 2 Replies Last reply 14 Jul 2023, 15:52
      1
      • C Christian Ehrlicher
        14 Jul 2023, 15:46

        Please remove all the useless QDataStream stuff - you're pushing it two times through a QDataStream (for unknown reason) during writing but only once during reading.

        I Offline
        I Offline
        imfp
        wrote on 14 Jul 2023, 15:52 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • C Christian Ehrlicher
          14 Jul 2023, 15:46

          Please remove all the useless QDataStream stuff - you're pushing it two times through a QDataStream (for unknown reason) during writing but only once during reading.

          I Offline
          I Offline
          imfp
          wrote on 14 Jul 2023, 16:03 last edited by
          #4

          @Christian-Ehrlicher Do u mean I'm declaring it 2x at decrypt_binary function and should do it just one time? I didn't realize it, sorry. Is it the cause of my error?

          C 1 Reply Last reply 14 Jul 2023, 19:30
          0
          • I imfp
            14 Jul 2023, 16:03

            @Christian-Ehrlicher Do u mean I'm declaring it 2x at decrypt_binary function and should do it just one time? I didn't realize it, sorry. Is it the cause of my error?

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 14 Jul 2023, 19:30 last edited by
            #5

            @imfp said in SimpleCrypt changing binary data after encryption/decryption:

            Is it the cause of my error?

            I don't know - cleanup your code to not use QDataStream at all (as it's not needed) and see what happens. Also post your new code here.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            I 1 Reply Last reply 17 Jul 2023, 18:02
            0
            • C Christian Ehrlicher
              14 Jul 2023, 19:30

              @imfp said in SimpleCrypt changing binary data after encryption/decryption:

              Is it the cause of my error?

              I don't know - cleanup your code to not use QDataStream at all (as it's not needed) and see what happens. Also post your new code here.

              I Offline
              I Offline
              imfp
              wrote on 17 Jul 2023, 18:02 last edited by
              #6

              @Christian-Ehrlicher It's working!
              I changed all QDataStream functions for QIODevice functions and my result .bin file is the same as my initial .bin file. I don't know why but seems like QDataStream add bits in file when used to write something.
              Thankyou!

              SGaistS 1 Reply Last reply 17 Jul 2023, 18:22
              0
              • I imfp has marked this topic as solved on 17 Jul 2023, 18:02
              • I imfp
                17 Jul 2023, 18:02

                @Christian-Ehrlicher It's working!
                I changed all QDataStream functions for QIODevice functions and my result .bin file is the same as my initial .bin file. I don't know why but seems like QDataStream add bits in file when used to write something.
                Thankyou!

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 17 Jul 2023, 18:22 last edited by
                #7

                @imfp hi,

                QDataStream does write "metadata" along whatever you send through it so you can easily read and write your custom types from it (see the corresponding operators you have to implement).

                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
                1

                1/7

                14 Jul 2023, 15:42

                • Login

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