SimpleCrypt changing binary data after encryption/decryption
-
wrote on 14 Jul 2023, 15:42 last edited by
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
-
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
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.
-
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.
wrote on 14 Jul 2023, 15:52 last edited byThis post is deleted! -
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.
wrote on 14 Jul 2023, 16:03 last edited by@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?
-
@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?
@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.
-
@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.
wrote on 17 Jul 2023, 18:02 last edited by@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! -
-
@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!@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).
1/7