encrypt decrypt json files using cryptopp
-
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
-
decryptedBuffer
, an emptyQByteArray
, is passed as a second argument toEncryptFile
. The method expects a file name as a second argument, which should probably beencryptedFile
. Since the second argument is empty, there is nowhere to output anything. -
decryptedBuffer
, an emptyQByteArray
, is passed as a second argument toEncryptFile
. The method expects a file name as a second argument, which should probably beencryptedFile
. Since the second argument is empty, there is nowhere to output anything. -
decryptedBuffer
, an emptyQByteArray
, is passed as a second argument toEncryptFile
. The method expects a file name as a second argument, which should probably beencryptedFile
. Since the second argument is empty, there is nowhere to output anything. -
-
A Axel Spoerl restored this topic on
-
Please don’t delete topics. Mark them solved if they are.
-