Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Encryption/decryption issue with QString.
QtWS25 Last Chance

Encryption/decryption issue with QString.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aditya1309
    wrote on last edited by
    #1

    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 constructed

    I am not able to figure out the problem, but looks like issue while converting from std::String to QString.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @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:

      1. Work on QByteArrays, not QStrings.
      2. Encrypt, then enconde into base64 (QByteArray has special methods to do it).
      3. Then, when data is in base64, you can do whatever with it: convert to QString, XML, Json - anything.
      4. Now reversing the process should work.

      (Z(:^

      A 2 Replies Last reply
      5
      • sierdzioS sierdzio

        @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:

        1. Work on QByteArrays, not QStrings.
        2. Encrypt, then enconde into base64 (QByteArray has special methods to do it).
        3. Then, when data is in base64, you can do whatever with it: convert to QString, XML, Json - anything.
        4. Now reversing the process should work.
        A Offline
        A Offline
        Aditya1309
        wrote on last edited by
        #3

        @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 ?

        JonBJ 1 Reply Last reply
        0
        • A Aditya1309

          @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 ?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @Aditya1309
          Yes, (assuming std::string Encrypt() is your own code) you should have that return a QByteArray, or std::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.

          A 1 Reply Last reply
          2
          • sierdzioS sierdzio

            @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:

            1. Work on QByteArrays, not QStrings.
            2. Encrypt, then enconde into base64 (QByteArray has special methods to do it).
            3. Then, when data is in base64, you can do whatever with it: convert to QString, XML, Json - anything.
            4. Now reversing the process should work.
            A Offline
            A Offline
            Aditya1309
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • JonBJ JonB

              @Aditya1309
              Yes, (assuming std::string Encrypt() is your own code) you should have that return a QByteArray, or std::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.

              A Offline
              A Offline
              Aditya1309
              wrote on last edited by
              #6

              @JonB Thanks I used bytearray and it worked fine.

              JonBJ 1 Reply Last reply
              0
              • A Aditya1309

                @JonB Thanks I used bytearray and it worked fine.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #7

                @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....

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved