Write QCA Encrypt Decrypt to file
Solved
3rd Party Software
-
Hello everybody,
I would like a file with AES locks, for this I use QCA.
Closing seems to work, but I can not decode it anymore. Everything I've tried so far changes the file slightly (another MD5 comes out) or makes it completely useless.// code to encrypt and write to file.
QCA::Initializer init = QCA::Initializer(); if(QCA::isSupported("aes256-cbc-pkcs7")) { // input filename QTextStream streamin(stdin); printf("File: "); QString filename = streamin.readLine(0); QFile inputFile(filename); if(!inputFile.open(QIODevice::ReadOnly)) { qDebug() << "problem while reading "; return -1; } const QByteArray ReadFileByteArray = inputFile.readAll(); inputFile.close(); qDebug() << "size of clear file" << ReadFileByteArray.size(); QCA::SecureArray inputSA = ReadFileByteArray; qDebug() << "size of inputSA: " << inputSA.size(); // Password input printf("Password: "); QString password = streamin.readLine(0); QString ki = password; QCA::SymmetricKey key = ki.toUtf8(); QCA::InitializationVector iv = ki.toUtf8(); QCA::Cipher cipher(QString("aes256"), QCA::Cipher::CBC, QCA::Cipher::DefaultPadding, QCA::Encode, key, iv); QCA::SecureArray encoded = cipher.update(inputSA); qDebug() << "size of encoded: " << encoded.size(); if(!cipher.ok()) { qCritical("update failed"); return -1; } cipher.setup(QCA::Encode, key, iv); QCA::SecureArray original = cipher.process(encoded); qDebug() << "size of original" << original.size(); if(!cipher.ok()) { qCritical("Final failed"); return -1; } QByteArray originaldata = original.toByteArray(); qDebug() << "size of originaldata: " << originaldata.size(); QString writeFileName = filename + ".e"; QFile file1(writeFileName); if(!file1.open(QIODevice::WriteOnly)) { qCritical("problem while writing "); return -1; } else { QTextStream streamout(&file1); streamout << encoded.toByteArray(); streamout.flush(); } file1.close(); } else { qDebug() << "no"; qCritical("aes256-cbc-pkcs7"); } return 0; }
// code to decrypt
QCA::Initializer init = QCA::Initializer(); if(QCA::isSupported("aes256-cbc-pkcs7")) { printf("File: "); QTextStream streamin(stdin); QString readFileName = streamin.readLine(0); printf("Password: "); QString password = streamin.readLine(0); QString ki = password; QCA::SymmetricKey key = ki.toUtf8(); QCA::InitializationVector iv = ki.toUtf8(); QCA::Cipher cipher(QString("aes256"), QCA::Cipher::CBC, QCA::Cipher::DefaultPadding, QCA::Encode, key, iv); QFile inputFile(readFileName); if(!inputFile.open(QIODevice::ReadOnly)) { qCritical("problem while reading"); return -1; } const QByteArray ReadFileByteArray = inputFile.readAll(); inputFile.close(); cipher.setup(QCA::Decode, key, iv); QCA::SecureArray decoded = cipher.update(ReadFileByteArray); if(!cipher.ok()) { qCritical("Final failed"); return -1; } // wie kann man das Kennwort überprüfen? int cutFile = readFileName.length() -2; QFile wFile(readFileName.left(cutFile)); if(!wFile.open(QIODevice::WriteOnly)) { qCritical("problem reading file"); return -1; } else { /*QTextStream streamout(&wFile); streamout << decoded.; streamout.flush();*/ QByteArray original = decoded.toByteArray(); QString filename = readFileName.left(cutFile); QFile wf(filename); wf.open(QIODevice::WriteOnly); QDataStream out(&wf); out << original; wf.close(); wFile.close(); } } else { qDebug() << "no"; qCritical("aes256-cbc-pkcs7"); } return 0; }
QDataStream does not work at all.
If I use QTextStream, the file can be opened, but not 100 percent the same. Unfortunately, a file.write (data.toByteArray ()) alters the file so that it does not fit 100 percent anymore.Does anybody know what I'm doing wrong?
Thank you for your help.
-
Hi,
Why not use
QFile::write
directly rather than the stream operators ?