Encryption/decryption issue with QString.
-
Hi,
I am able to encrypt and decrypt using symmetric key using AES algo.
std::string Encrypt(const void * const rszBuffer)
std::string Decrypt(const void * const rszBuffer)This works fine with .txt file.
But while I am trying to save it in xml using Qstring decryption fails.I am converting string returned from Encrypt and converting to QString and then saving to XML.
While decrypting I am doing the reverse but I am unable to construct the plain text.// getting from xml
std::string szpasswd = Obj.getPassword().toStdString();
qDebug()<<"Password is:"<<QString::fromStdString(szpasswd);
std::string szciphertext = Encrypt(szpasswd.data());qDebug()<<"ciphertext value is:"<<QString(szciphertext.data());
qDebug()<<"Saving cipher text:"<<QString::fromStdString(szciphertext.data());std::string plaintext = Decrypt(szciphertext.data()); //cipher is correct from and plaintext is constructed
qDebug()<<"saving plaintext:"<<QString(plaintext.data()); // get the correct plaintext
qDebug()<<"Converting again to plaintext:";
plaintext = Decrypt(QString(szciphertext.data()).toStdString().data()); //cipher is wrong while using QString
qDebug()<<"saving plaintext using Qstring:"<<QString(plaintext.data()); // plaintext is not constructedI am not able to figure out the problem, but looks like issue while converting from std::String to QString.
-
@Aditya1309 said in Encryption/decryption issue with QString.:
I am converting string returned from Encrypt and converting to QString and then saving to XML
That moment looks suspicious. The encryption can produce any combination of bits. Converting them to UTF-16 (QString) and then to XML (which uses it's own escaping for special characters etc.) is likely introducing the issues here.
Instead, do this:
- Work on QByteArrays, not QStrings.
- Encrypt, then enconde into base64 (QByteArray has special methods to do it).
- Then, when data is in base64, you can do whatever with it: convert to QString, XML, Json - anything.
- Now reversing the process should work.
-
@Aditya1309 said in Encryption/decryption issue with QString.:
I am converting string returned from Encrypt and converting to QString and then saving to XML
That moment looks suspicious. The encryption can produce any combination of bits. Converting them to UTF-16 (QString) and then to XML (which uses it's own escaping for special characters etc.) is likely introducing the issues here.
Instead, do this:
- Work on QByteArrays, not QStrings.
- Encrypt, then enconde into base64 (QByteArray has special methods to do it).
- Then, when data is in base64, you can do whatever with it: convert to QString, XML, Json - anything.
- Now reversing the process should work.
@sierdzio said in Encryption/decryption issue with QString.:
base64
It would be helpful if you just elaborate then I can implement the same and revert back.
Currently my encrypt and decrypt returns std::string. Do you mean I should change them to return QbyteArray ? -
@sierdzio said in Encryption/decryption issue with QString.:
base64
It would be helpful if you just elaborate then I can implement the same and revert back.
Currently my encrypt and decrypt returns std::string. Do you mean I should change them to return QbyteArray ?@Aditya1309
Yes, (assumingstd::string Encrypt()
is your own code) you should have that return aQByteArray
, orstd::byte
or equivalent.http://doc.qt.io/qt-5/qbytearray.html#toBase64 & http://doc.qt.io/qt-5/qbytearray.html#fromBase64 convert to/from base64.
-
@Aditya1309 said in Encryption/decryption issue with QString.:
I am converting string returned from Encrypt and converting to QString and then saving to XML
That moment looks suspicious. The encryption can produce any combination of bits. Converting them to UTF-16 (QString) and then to XML (which uses it's own escaping for special characters etc.) is likely introducing the issues here.
Instead, do this:
- Work on QByteArrays, not QStrings.
- Encrypt, then enconde into base64 (QByteArray has special methods to do it).
- Then, when data is in base64, you can do whatever with it: convert to QString, XML, Json - anything.
- Now reversing the process should work.
@sierdzio Thanks for the help, I used bytearray for saving my QString and I did the following and it worked fine.
For saving ciphertext into XML.
QByteArray byteArray;
byteArray.append(Obj.getPassword().toLatin1());
// Converting to ciphertext
QByteArray ciphertext = Encrypt(byteArray);
newNodeText = m_Doc.createTextNode(QString::fromLatin1(ciphertext));For getting the ciphertext into plaintext and set the lineedit.
// Getting the encrypted password and setting the password
QByteArray plaintext = Decrypt(nodeValue.toLatin1());
Obj.setPassword(QString(plaintext));The above code is working as expected.
-
@Aditya1309
Yes, (assumingstd::string Encrypt()
is your own code) you should have that return aQByteArray
, orstd::byte
or equivalent.http://doc.qt.io/qt-5/qbytearray.html#toBase64 & http://doc.qt.io/qt-5/qbytearray.html#fromBase64 convert to/from base64.
@JonB Thanks I used bytearray and it worked fine.
-
@JonB Thanks I used bytearray and it worked fine.
@Aditya1309
Maybe it works fine on your particular password content, I don't know. Don't blame us if it goes wrong on other input. As @Aditya1309 & I have said, we would have thought you would be best/safest storing it as hex or base64, not as arbitrary, unknown bytes which you treat as a string. Saving as hex/base64 would require one extra line in your encryption, and one extra line in your decryption. It's up to you....