Convert QByteArray to QString
-
Hi everyone!
I want to encrypt the data of a database and to do this, I used AES_128 this link for encryption.The result of encryption is a QByteArray. The QByteArray is saved on the text file in the correct shape and I could decode it correctly, but I need to save it on the Sqlite DB. To do this, I should convert it to the QString, save it and do a reverse loop for reading from DB. I tried some options like
QByteArray encodedText; QString DataAsString = QString(encodedText);
and
string DataAsString1 = encodedText.toStdString();
and
QString DataAsString = QTextCodec::codecForName("UTF-8") >toUnicode(encodedText);
and
QString QString::fromUtf8(const QByteArray &str)
and other solutions I saw on the web, but the outputs of these options aren't correct. Because after casting, I couldn't convert the encoded text to decoded correctly.
For example, this is the input string of encoded text: "\x14r\xF7""6#\xFE\xDB\xF0""D\x1B\xB5\x10\xEDx\xE1""F"
and these are the outputs from the different ways I tried so far: \024r�6#���D\033�\020�x�F and \024r�6#���D\033�\020�x�FDoes anybody suggestion about the right conversion?
-
Hi everyone!
I want to encrypt the data of a database and to do this, I used AES_128 this link for encryption.The result of encryption is a QByteArray. The QByteArray is saved on the text file in the correct shape and I could decode it correctly, but I need to save it on the Sqlite DB. To do this, I should convert it to the QString, save it and do a reverse loop for reading from DB. I tried some options like
QByteArray encodedText; QString DataAsString = QString(encodedText);
and
string DataAsString1 = encodedText.toStdString();
and
QString DataAsString = QTextCodec::codecForName("UTF-8") >toUnicode(encodedText);
and
QString QString::fromUtf8(const QByteArray &str)
and other solutions I saw on the web, but the outputs of these options aren't correct. Because after casting, I couldn't convert the encoded text to decoded correctly.
For example, this is the input string of encoded text: "\x14r\xF7""6#\xFE\xDB\xF0""D\x1B\xB5\x10\xEDx\xE1""F"
and these are the outputs from the different ways I tried so far: \024r�6#���D\033�\020�x�F and \024r�6#���D\033�\020�x�FDoes anybody suggestion about the right conversion?
@javad_2012 said:
I need to save it on the Sqlite DB. To do this, I should convert it to the QString
That's wrong. After you encode the data it is no longer a string, it's a blob of binary data. Converting it to a QString takes random encrypted data and interprets it as a UTF-16 string. This will result in garbage.
You've got a blob of binary data, so store it as a blob of binary data, not string. SQLite has a BLOB type. Use that and pass your QByteArray to the query.
-
@Chris-Kawa is right, in that you should use SQLite's binary support if possible. However, just for completeness, if you had to convert to
QString
(eg if you can't control the database's schema), then you should choose some efficient binary-to-text encoding, Base64 being a pretty reasonable option, like:const QByteArray encodedTextInBinaryAes128Format = encode(...); const QByteArray encodedTextAsBase64Bytes = encodedTextInBinaryAes128Format.toBase64(); const QString encodedTextAsBase64String = QString::fromLatin1(encodedTextAsBase64Bytes); // Now both encodedTextAsBase64Bytes and encodedTextAsBase64String are safe to store as SQLite TEXT values // and encodedTextInBinaryAes128Format is safe to store as a SQLite BLOB value (recommended).
Cheers.
-
@Chris-Kawa and @PAUL COLBY.
Thanks alot.