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. failed decryption issue when not including encryption
Forum Updated to NodeBB v4.3 + New Features

failed decryption issue when not including encryption

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 176 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.
  • B Offline
    B Offline
    Blackzero
    wrote on last edited by
    #1

    I made encryption and decryption of json files, the problem is that when I include encryption along with decryption, the decryption results are successful, but when I only use decryption and do not include encryption, the decryption results fail and sometimes succeed, so the decryption results are uncertain whether it succeeds or fails.

    13:53:06: Starting D:\Myproject\Desktop_Qt_5_15_2_MSVC2019_64bit-Release\release\Database.exe...
    Decryption failed: StreamTransformationFilter: invalid PKCS #7 block padding found
    Decryption failed.
    13:53:10: D:\Myproject\Desktop_Qt_5_15_2_MSVC2019_64bit-Release\release\Database.exe... exited with code 0
    
    13:53:11: Starting D:\Myproject\Desktop_Qt_5_15_2_MSVC2019_64bit-Release\release\Database.exe...
    Decryption successful.
    
    #include "encryptionfile.h"
    #include <string>
    #include <cryptlib.h>
    #include <aes.h>
    #include <ccm.h>
    #include <filters.h>
    #include <files.h>
    #include <hex.h>
    #include <secblock.h>
    #include <QDebug>
    #include <QCoreApplication>
    
    using namespace CryptoPP;
    using namespace std;
    
    EncryptionFile::EncryptionFile(QObject *parent) : QObject(parent) {}
    
    SecByteBlock StringToSecByteBlock(const std::string &str, size_t size)
    {
        SecByteBlock block(size);
        memcpy(block, str.data(), std::min(size, str.size()));
        return block;
    }
    
    bool EncryptionFile::EncryptFile(const QString &inputFile, const QString &outputFile, const SecByteBlock &key, const SecByteBlock &iv)
    {
        try
        {
            CBC_Mode<AES>::Encryption encryptor(key, key.size(), iv);
            FileSource(inputFile.toStdString().c_str(), true,
                       new StreamTransformationFilter(encryptor,
                                                      new FileSink(outputFile.toStdString().c_str())));
            return true;
        }
        catch (const CryptoPP::Exception &e)
        {
            qCritical() << "Encryption failed:" << e.what();
            return false;
        }
    }
    
    bool EncryptionFile::DecryptFileToBuffer(const QString &inputFile, QByteArray &outputBuffer, const SecByteBlock &key, const SecByteBlock &iv)
    {
        try
        {
            CBC_Mode<AES>::Decryption decryptor(key, key.size(), iv);
            std::string decryptedData;
            FileSource(inputFile.toStdString().c_str(), true,
                       new StreamTransformationFilter(decryptor,
                                                      new StringSink(decryptedData)));
            outputBuffer = QByteArray::fromStdString(decryptedData);
    
            // Debugging output
            // qDebug() << "Decrypted buffer size:" << outputBuffer.size();
            // qDebug() << "Decrypted buffer contents (string):" << QString::fromUtf8(outputBuffer);
    
            return true;
        }
        catch (const CryptoPP::Exception &e)
        {
            qCritical() << "Decryption failed:" << e.what();
            return false;
        }
    }
    
    bool EncryptionFile::ReadJsonData_STRUCT(QJsonObject &jsonObject)
    {
        std::string keyStr = "Database12345#@";
        std::string ivStr = "data12345#@";
    
        SecByteBlock key = StringToSecByteBlock(keyStr, AES::DEFAULT_KEYLENGTH); // 16 byte
        SecByteBlock iv = StringToSecByteBlock(ivStr, AES::BLOCKSIZE); // 16 byte
    
        QString jsonFile = QCoreApplication::applicationDirPath() + "/db.json";
        QString encryptedFile = QCoreApplication::applicationDirPath() + "/data0.dat";
        // if (EncryptFile(jsonFile, encryptedFile, key, iv)) {
        //     qDebug() << "Encryption successful.";
        // } else {
        //     qDebug() << "Encryption failed.";
        // }
    
        QByteArray decryptedBuffer;
        if (DecryptFileToBuffer(encryptedFile, decryptedBuffer, key, iv)) {
            qDebug() << "Decryption successful.";
        }
        else {
            qDebug() << "Decryption failed.";
            return false;
        }
    
        // Ensure buffer is not empty
        if (decryptedBuffer.isEmpty()) {
            qDebug() << "Decrypted buffer is empty.";
            return false;
        }
    
        QJsonParseError parseError;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(decryptedBuffer, &parseError);
        if (jsonDoc.isNull()) {
            qDebug() << "Failed to parse JSON:" << parseError.errorString();
            return false;
        }
    
        if (!jsonDoc.isObject()) {
            qDebug() << "JSON document is not an object";
            return false;
        }
    
        jsonObject = jsonDoc.object();
        return true;
    }
    
    
    C 1 Reply Last reply
    0
    • B Blackzero

      I made encryption and decryption of json files, the problem is that when I include encryption along with decryption, the decryption results are successful, but when I only use decryption and do not include encryption, the decryption results fail and sometimes succeed, so the decryption results are uncertain whether it succeeds or fails.

      13:53:06: Starting D:\Myproject\Desktop_Qt_5_15_2_MSVC2019_64bit-Release\release\Database.exe...
      Decryption failed: StreamTransformationFilter: invalid PKCS #7 block padding found
      Decryption failed.
      13:53:10: D:\Myproject\Desktop_Qt_5_15_2_MSVC2019_64bit-Release\release\Database.exe... exited with code 0
      
      13:53:11: Starting D:\Myproject\Desktop_Qt_5_15_2_MSVC2019_64bit-Release\release\Database.exe...
      Decryption successful.
      
      #include "encryptionfile.h"
      #include <string>
      #include <cryptlib.h>
      #include <aes.h>
      #include <ccm.h>
      #include <filters.h>
      #include <files.h>
      #include <hex.h>
      #include <secblock.h>
      #include <QDebug>
      #include <QCoreApplication>
      
      using namespace CryptoPP;
      using namespace std;
      
      EncryptionFile::EncryptionFile(QObject *parent) : QObject(parent) {}
      
      SecByteBlock StringToSecByteBlock(const std::string &str, size_t size)
      {
          SecByteBlock block(size);
          memcpy(block, str.data(), std::min(size, str.size()));
          return block;
      }
      
      bool EncryptionFile::EncryptFile(const QString &inputFile, const QString &outputFile, const SecByteBlock &key, const SecByteBlock &iv)
      {
          try
          {
              CBC_Mode<AES>::Encryption encryptor(key, key.size(), iv);
              FileSource(inputFile.toStdString().c_str(), true,
                         new StreamTransformationFilter(encryptor,
                                                        new FileSink(outputFile.toStdString().c_str())));
              return true;
          }
          catch (const CryptoPP::Exception &e)
          {
              qCritical() << "Encryption failed:" << e.what();
              return false;
          }
      }
      
      bool EncryptionFile::DecryptFileToBuffer(const QString &inputFile, QByteArray &outputBuffer, const SecByteBlock &key, const SecByteBlock &iv)
      {
          try
          {
              CBC_Mode<AES>::Decryption decryptor(key, key.size(), iv);
              std::string decryptedData;
              FileSource(inputFile.toStdString().c_str(), true,
                         new StreamTransformationFilter(decryptor,
                                                        new StringSink(decryptedData)));
              outputBuffer = QByteArray::fromStdString(decryptedData);
      
              // Debugging output
              // qDebug() << "Decrypted buffer size:" << outputBuffer.size();
              // qDebug() << "Decrypted buffer contents (string):" << QString::fromUtf8(outputBuffer);
      
              return true;
          }
          catch (const CryptoPP::Exception &e)
          {
              qCritical() << "Decryption failed:" << e.what();
              return false;
          }
      }
      
      bool EncryptionFile::ReadJsonData_STRUCT(QJsonObject &jsonObject)
      {
          std::string keyStr = "Database12345#@";
          std::string ivStr = "data12345#@";
      
          SecByteBlock key = StringToSecByteBlock(keyStr, AES::DEFAULT_KEYLENGTH); // 16 byte
          SecByteBlock iv = StringToSecByteBlock(ivStr, AES::BLOCKSIZE); // 16 byte
      
          QString jsonFile = QCoreApplication::applicationDirPath() + "/db.json";
          QString encryptedFile = QCoreApplication::applicationDirPath() + "/data0.dat";
          // if (EncryptFile(jsonFile, encryptedFile, key, iv)) {
          //     qDebug() << "Encryption successful.";
          // } else {
          //     qDebug() << "Encryption failed.";
          // }
      
          QByteArray decryptedBuffer;
          if (DecryptFileToBuffer(encryptedFile, decryptedBuffer, key, iv)) {
              qDebug() << "Decryption successful.";
          }
          else {
              qDebug() << "Decryption failed.";
              return false;
          }
      
          // Ensure buffer is not empty
          if (decryptedBuffer.isEmpty()) {
              qDebug() << "Decrypted buffer is empty.";
              return false;
          }
      
          QJsonParseError parseError;
          QJsonDocument jsonDoc = QJsonDocument::fromJson(decryptedBuffer, &parseError);
          if (jsonDoc.isNull()) {
              qDebug() << "Failed to parse JSON:" << parseError.errorString();
              return false;
          }
      
          if (!jsonDoc.isObject()) {
              qDebug() << "JSON document is not an object";
              return false;
          }
      
          jsonObject = jsonDoc.object();
          return true;
      }
      
      
      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      What does this have to do with Qt?

      This is unlikely to be reliable as a writeable location:

      QString encryptedFile = QCoreApplication::applicationDirPath() + "/data0.dat";
      

      What happens to bytes beyond the string here when (as in this example) the str is shorter than an encryption block?

      SecByteBlock block(size);
      memcpy(block, str.data(), std::min(size, str.size()));
      
      1 Reply Last reply
      2
      • B Blackzero has marked this topic as solved on

      • Login

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