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. [SOLVED] QCA decode method

[SOLVED] QCA decode method

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.2k 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.
  • shavS Offline
    shavS Offline
    shav
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • shavS Offline
        shavS Offline
        shav
        wrote on last edited by
        #3

        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
        0
        • shavS Offline
          shavS Offline
          shav
          wrote on last edited by
          #4

          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
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            0

            • Login

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