Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] QCA decode method

    General and Desktop
    2
    5
    1907
    Loading More Posts
    • 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.
    • S
      shav last edited by

      Hi everyone!

      I need to use AES256 encode in my project. I've found the QCA library and integrate it to my project. All works fine but when I'm trying decode the string which I've encode using QCA I've get empty string.
      @
      QString MdlLogParser::encode(QString source, QString key, QString iv)
      {
      QCA::Initializer init = QCA::Initializer();
      QCA::SymmetricKey keyData = QCA::SymmetricKey(key.toUtf8());
      QCA::InitializationVector ivData = QCA::InitializationVector(iv.toUtf8());

      QCA::Cipher cipher = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC,
                                       QCA::Cipher::DefaultPadding, QCA::Encode,
                                       keyData, ivData);
      if (!QCA::isSupported("aes256-cbc-pkcs7"))
      {
          qDebug() << "AES256 CBC PKCS7 not supported - "
                      "please check if qca-ossl plugin"
                      "installed correctly !";
      
          return "";
      }
      
      QCA::SecureArray secureData = source.toUtf8();
      QCA::SecureArray encryptedData = cipher.process(secureData);
      if (!cipher.ok())
      {
          qDebug() << "Encryption failed !";
          return "";
      }
      
      return QString(qPrintable(QCA::arrayToHex(encryptedData.toByteArray())));
      

      }

      QString MdlLogParser::decode(QString encodeString, QString key, QString iv)
      {
      QCA::Initializer init = QCA::Initializer();
      QCA::SymmetricKey keyData = QCA::SymmetricKey(key.toUtf8());
      QCA::InitializationVector ivData = QCA::InitializationVector(iv.toUtf8());

      QCA::Cipher cipher = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC,
                                       QCA::Cipher::DefaultPadding, QCA::Encode,
                                       keyData, ivData);
      
      QCA::SecureArray encryptedData = encodeString.toUtf8();
      
      cipher.setup(QCA::Decode, keyData, ivData);
      QCA::SecureArray decryptedData = cipher.process(encryptedData);
      
      if (!cipher.ok())
      {
          qDebug() << "Decryption failed !  ";
          return "";
      }
      
      return QString(decryptedData.data());
      

      }
      @

      Use
      @
      QString encodeString = encode("Hello World!", "aaaaaa", "bbbbb");
      qDebug()<<"encode: "<<encodeString;
      qDebug()<<"decode: "<<decode("d84e7cc6a90ba17c4e4195309ac2f87f", "aaaaaa", "bbbbb");
      @

      Consol log:
      @
      encode: "d84e7cc6a90ba17c4e4195309ac2f87f"
      Decryption failed !
      decode: ""
      @

      What I've do wrong? Thanks for the any help!

      Mac OS and iOS Developer

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        the setup in decode:

        @
        QCA::Cipher cipher = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC,
        QCA::Cipher::DefaultPadding,
        QCA::Encode, << Shouldn't that be Decode ?
        keyData, ivData);@

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • S
          shav last edited by

          Hi,

          Thanks for the reply! I've tried this it's doesn't help. I've found the "example":http://www.essentialunix.org/index.php?option=com_content&view=article&id=48:qcatutorial&catid=34:qttutorials&Itemid=53. And it's works if encode and decode in one function. But when I tried to create two methods decode doesn't work.

          Mac OS and iOS Developer

          1 Reply Last reply Reply Quote 0
          • S
            shav last edited by

            Ok, I've found solution.

            As usually the solution is a simple. All I need is convert encoded string from hex to byte array using function QCA::hexToArray().
            @
            QCA::Initializer init = QCA::Initializer();
            QCA::SymmetricKey keyData = QCA::SymmetricKey(key.toUtf8());
            QCA::InitializationVector ivData = QCA::InitializationVector(iv.toUtf8());
            QCA::Cipher cipherRes = QCA::Cipher(QString("aes256"), QCA::Cipher::CBC,
            QCA::Cipher::PKCS7, QCA::Decode,
            keyData, ivData);

            QCA::SecureArray decryptedData = cipherRes.process(QCA::hexToArray(encodeString));
            
            if (!cipherRes.ok())
            {
                qDebug() << "Decryption failed !  ";
                return "";
            }
            
            return QString(decryptedData.data());
            

            @

            Now it's work!

            Mac OS and iOS Developer

            1 Reply Last reply Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              Good !

              Then don't forget to update the thread title prepending [solved] so other forum users may know a solution has been found :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply Reply Quote 0
              • First post
                Last post