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 json files using cryptopp
Forum Updated to NodeBB v4.3 + New Features

encrypt decrypt json files using cryptopp

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

    Can someone help me with my problem.

    I'm trying to encrypt a file then the encrypted file is read and moved into a QbyteArray called buffer to be read by QJson but it always fails, the decrypt result always fails.

    #include <iostream>
    #include <string>
    #include <cryptlib.h>
    #include <aes.h>
    #include <ccm.h>
    #include <filters.h>
    #include <files.h>
    #include <osrng.h>
    #include <hex.h>
    #include <sha.h>
    #include <secblock.h>
    #include <QFile>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QCoreApplication>
    using namespace CryptoPP;
    using namespace std;
    
    bool 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 Exception &e)
        {
            cerr << "encrypt failed" << e.what() << endl;
            return false;
        }
    }
    
    bool 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::fromRawData(decryptedData.data(), decryptedData.size());
    
            return true;
        }
        catch (const Exception &e)
        {
            cerr << "decyrpt failed " << e.what() << endl;
            return false;
        }
    }
    
    bool loadJsonData(const QByteArray &jsonData, QJsonObject &jsonObject)
    {
        QJsonParseError parseError;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &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;
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        AutoSeededRandomPool prng;
        SecByteBlock key(AES::DEFAULT_KEYLENGTH);
        SecByteBlock iv(AES::BLOCKSIZE);
    
        prng.GenerateBlock(key, key.size());
        prng.GenerateBlock(iv, iv.size());
        
        QString jsonFile = "db.json";
        QString encryptedFile = "encrypted_db.bin";
    
        // Enkripsi file JSON
        if (EncryptFile(jsonFile, encryptedFile, key, iv)) {
            qDebug() << "encrypt succes";
        } else {
            qDebug() << "encrypt failed";
            return -1;
        }
    
        QByteArray decryptedBuffer;
        if (DecryptFileToBuffer(encryptedFile, decryptedBuffer, key, iv)) {
            qDebug() << "decyrpt succes";
        } else {
            qDebug() << "decrypt failed";
            return -1;
        }
    
        QJsonObject jsonObject;
        if (loadJsonData(decryptedBuffer, jsonObject)) {
            qDebug() << "JSON data:" << QJsonDocument(jsonObject).toJson();
        } else {
            qDebug() << "failed read buffer json";
            return -1;
        }
    
    
        return a.exec();
    }
    
    

    output console:

    encrypt succes
    decyrpt succes
    

    it does not output any data

    1 Reply Last reply
    0
    • Axel SpoerlA Online
      Axel SpoerlA Online
      Axel Spoerl
      Moderators
      wrote on last edited by Axel Spoerl
      #2

      decryptedBuffer, an empty QByteArray, is passed as a second argument to EncryptFile. The method expects a file name as a second argument, which should probably be encryptedFile. Since the second argument is empty, there is nowhere to output anything.

      Software Engineer
      The Qt Company, Oslo

      B 2 Replies Last reply
      0
      • Axel SpoerlA Axel Spoerl

        decryptedBuffer, an empty QByteArray, is passed as a second argument to EncryptFile. The method expects a file name as a second argument, which should probably be encryptedFile. Since the second argument is empty, there is nowhere to output anything.

        B Offline
        B Offline
        Blackzero
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • Axel SpoerlA Axel Spoerl

          decryptedBuffer, an empty QByteArray, is passed as a second argument to EncryptFile. The method expects a file name as a second argument, which should probably be encryptedFile. Since the second argument is empty, there is nowhere to output anything.

          B Offline
          B Offline
          Blackzero
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • B Blackzero deleted this topic on
          • Axel SpoerlA Axel Spoerl restored this topic on
          • Axel SpoerlA Online
            Axel SpoerlA Online
            Axel Spoerl
            Moderators
            wrote on last edited by
            #5

            Please don’t delete topics. Mark them solved if they are.

            Software Engineer
            The Qt Company, Oslo

            1 Reply Last reply
            1
            • 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