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. Decrypt AES with OpenSSL & Qt 5.5.1 Win32 VS2013
Forum Update on Monday, May 27th 2025

Decrypt AES with OpenSSL & Qt 5.5.1 Win32 VS2013

Scheduled Pinned Locked Moved Solved General and Desktop
opensslqt 5.5.1windowsvs 2013decrypt
42 Posts 3 Posters 23.0k 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.
  • S Offline
    S Offline
    SGaist
    Lifetime Qt Champion
    wrote on 31 Mar 2016, 21:20 last edited by
    #14

    Your code doesn't match all the openssl line options. -a -A means that you must first decode your Base64 encoded string.

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

    Q 1 Reply Last reply 1 Apr 2016, 03:58
    1
    • S SGaist
      31 Mar 2016, 21:20

      Your code doesn't match all the openssl line options. -a -A means that you must first decode your Base64 encoded string.

      Q Offline
      Q Offline
      qDebug
      wrote on 1 Apr 2016, 03:58 last edited by qDebug 4 Jan 2016, 04:05
      #15

      @SGaist Thanks! I don't know why but i did forget about the base64 decode. After changing from toLatin1 to toUtf8 and decode the base64 string i don't get the final error anymore. But i won't get the expected plain text only

      o????:??????a?s?]??Iy?[W6???l??(f?$?I{??^ ????Cï5?
      

      same result if i use

      qDebug() << "process: " << QCA::SecureArray(cipher.process(decodedBase64.toUtf8())).data();
      

      Maybe the utf8 conversion is the problem but it won't take a QString. I get the same result if i append the string to a QByteArray. Btw. noting changes if i use QByteArray to decode base64 or QCA::Base64 decoder(QCA::Decode);

      At least the final error is gone, that's some progress! ;)

      Edit: And i added "md5" as crypto service provider because it was encoded this way. md5 is in the list of providers, so i hope i implemented this correctly.

      QCA::Cipher cipher(QString("aes128"), QCA::Cipher::CBC, QCA::Cipher::NoPadding, QCA::Decode, key, iv, "md5");
      

      But cipher.provider()->name(); returns "qca-ossl".

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 1 Apr 2016, 20:41 last edited by
        #16

        Do you mean that you passed your original string through md5 before encrypting it ?

        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
        • Q Offline
          Q Offline
          qDebug
          wrote on 2 Apr 2016, 03:16 last edited by qDebug 4 Feb 2016, 14:43
          #17

          The password / key was md5 hashed.

          key = md5(utf8(password));
          

          and the iv are the first 16 bytes from the base64 string.

          Edit: Not the key of course, the iv are the first 16 bytes from the base64 string.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 2 Apr 2016, 20:59 last edited by
            #18

            Can you show the complete procedure ?

            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
            • Q Offline
              Q Offline
              qDebug
              wrote on 3 Apr 2016, 19:14 last edited by
              #19

              Thanks for asking. This is the VB code to encrypt the string:

              Dim AES As New RijndaelManaged
              
              Dim md5 As New MD5CryptoServiceProvider
              Dim key() As Byte = md5.ComputeHash(Encoding.UTF8.GetBytes(password))
              
              md5.Clear()
              AES.Key = key
              AES.GenerateIV()
              Dim iv() As Byte = AES.IV
              Dim ms As New MemoryStream
              
              ms.Write(iv, 0, iv.Length)
              
              Dim cs As New CryptoStream(ms, AES.CreateEncryptor, CryptoStreamMode.Write)
              Dim data() As Byte = System.Text.Encoding.UTF8.GetBytes(string)
              
              cs.Write(data, 0, data.Length)
              cs.FlushFinalBlock()
              
              Dim encoded() As Byte = ms.ToArray()
              Return (Convert.ToBase64String(encoded))
              cs.Close()
              AES.Clear()
              

              As far as i can tell is the password and the string are utf8 bytes and the password is the md5 hash of it. I did try to pass it through

              QCA::SecureArray key = QCA::SecureArray::SecureArray(password);
              

              before and after utf8 and md5. So far no luck.

              Thank you!

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qDebug
                wrote on 5 Apr 2016, 20:08 last edited by
                #20

                And this is my attempt:

                QString MainWindow::decryptString(QString password, QString encodedString)
                {
                    // final decodec string
                    QString decodedString;
                
                    // get the iv
                    QByteArray array(encodedString.left(16).toStdString().c_str(), encodedString.left(16).size());
                    QCA::SecureArray iv = array.toHex();
                
                    // decode base64
                    QCA::Base64 decoder(QCA::Decode);
                    const char* decoded = decoder.decodeString(encodedString).toStdString().c_str();
                
                    QCA::SecureArray key = QByteArray(QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex());
                    QCA::SecureArray arg = decoded;
                
                    QCA::Cipher cipher(QString("aes128"), QCA::Cipher::CBC, QCA::Cipher::NoPadding, QCA::Decode, key, iv);
                    QCA::SecureArray plainText = cipher.update(arg);
                
                    if(!cipher.ok())
                    {
                        qDebug() << "update Fail";
                    }
                
                    cipher.final();
                    if(!cipher.ok())
                    {
                        qDebug() << "final fail";
                    }
                
                    qDebug() << "process: " << QCA::SecureArray(cipher.process(decoded)).data();
                
                    decodedString = plainText.data();
                    qDebug() << "Decoded: " << decodedString;
                
                    return decodedString;
                }
                

                I call it like:

                qDebug() << "decoded: " << decryptString("test", "PpUr+LMHvaKmf0q6J7Oyzo4jbFO5kfWyXl0d8nD3hyM=");
                

                The only step i masted was getting rid of the final fail, but i don't know if that was really the case. Not sure how, i changed this so often, i don't even know how much time i spend on this. I didn't think it can be that difficult to use it.

                So far i still did not fine one single example code using qca with aes128cbc and custom iv and key. Very, very strange.

                Any ideas?

                Thanks!

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 5 Apr 2016, 20:27 last edited by
                  #21

                  Do you mean this example ?

                  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
                  • Q Offline
                    Q Offline
                    qDebug
                    wrote on 5 Apr 2016, 20:32 last edited by
                    #22

                    Yes. It is the only one i found so far. But the iv and key are random generated, it does not show how to use an already existing key and iv correctly. I really don't know what i am missing here.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 5 Apr 2016, 20:34 last edited by
                      #23

                      Just replace the random key and iv by yours.

                      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
                      • Q Offline
                        Q Offline
                        qDebug
                        wrote on 5 Apr 2016, 20:51 last edited by
                        #24

                        I did. That was my first idea, did not work. Id did check the key and iv output, same as i use with openssl, does work in openssl, does not work in Qt / QCA - at least not the way i do it. I don't know, maybe i miss something in general or just just a tiny mistake, but after days and hours, i can confirm, it won't work for me.

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 5 Apr 2016, 21:22 last edited by
                          #25

                          I took the example as is, replaced key and iv by

                          QByteArray key("098f6bcd4621d373cade4e832627b4f6");
                          QByteArray iv("d8e8fca2dc0f896fd7cb4cb0031ba249");
                          

                          and it's working fine.

                          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
                          • Q Offline
                            Q Offline
                            qDebug
                            wrote on 6 Apr 2016, 05:40 last edited by qDebug 4 Jun 2016, 10:46
                            #26

                            If you can tell me why this code

                            QString decodedString;
                            QCA::Initializer init;
                            QByteArray array(encodedString.left(16).toStdString().c_str(), encodedString.left(16).size());
                            QCA::SecureArray iv = array.toHex();
                            QCA::Base64 decoder(QCA::Decode);
                            QCA::SecureArray decoded = decoder.decodeString(encodedString).toStdString().c_str();
                            QCA::SecureArray key = QByteArray(QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Md5).toHex());
                            QCA::Cipher cipher(QString("aes128"), QCA::Cipher::CBC, QCA::Cipher::NoPadding, QCA::Decode, key, iv);
                            QCA::SecureArray plainText = cipher.update(decoded);
                            if(!cipher.ok())
                            {
                            	qDebug() << "update Fail";
                            }
                            plainText  = cipher.final();
                            if(!cipher.ok())
                            {
                            	qDebug() << "final fail";
                            }
                            qDebug() << "process: " << QCA::SecureArray(cipher.process(decoded)).data();
                            QString decodedString = plainText.data();
                            qDebug() << "Decoded: " << decodedString;
                            

                            is not working, it may help me. If you tell me some code is working for you, it does not. This is btw. taken from the example, i did not change anything beside adding key and iv.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 6 Apr 2016, 20:49 last edited by
                              #27

                              The first thing that looks strange is your iv creation. You take 16 bytes of your encoded string and turn it to hexadecimal. Why ?

                              Also, why all the conversions ? Just use QByteArray.

                              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
                              • Q Offline
                                Q Offline
                                qDebug
                                wrote on 6 Apr 2016, 21:22 last edited by
                                #28

                                098f6bcd4621d373cade4e832627b4f6 is "test" in md5. all the strings i have to decode are encoded with a different iv, the iv is always the first 16 bytes in hex from the encoded string, d8e8fca2dc0f896fd7cb4cb0031ba249 in this case.

                                So i have to hash the key "test" and get the iv from the encoded string.

                                And of course, i did test the key and iv just as QByteArray, before and after you posted the example, but the decoding so far did always fail.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 6 Apr 2016, 21:47 last edited by
                                  #29

                                  That's something that is really not clear. You are telling me that you are using as iv for the decoding a part of the alrey encoded string ? So what did you use as iv for the encoding part ?

                                  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
                                  • Q Offline
                                    Q Offline
                                    qDebug
                                    wrote on 6 Apr 2016, 21:58 last edited by qDebug 4 Jun 2016, 22:07
                                    #30

                                    The iv is taken from the still encrypted and base64 encoded string, first 16 bytes hex. iv 507055722b4c4d4876614b6d66307136 is QByteArray array(encodedString.left(16).toStdString().c_str(), encodedString.left(16).size()); qDebug() << "iv: " << array.toHex();

                                    Proof:

                                    echo PpUr+LMHvaKmf0q6J7Oyzo4jbFO5kfWyXl0d8nD3hyM= | openssl enc -d -a -A -aes-128-cbc -iv 507055722b4c4d4876614b6d66307136 -K 098f6bcd4621d373cade4e832627b4f6
                                    8°&¦=YaÌ?{Äa+Dr. Test
                                    
                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 6 Apr 2016, 22:21 last edited by
                                      #31

                                      And what iv did you use to encrypt the string ?

                                      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
                                      • Q Offline
                                        Q Offline
                                        qDebug
                                        wrote on 6 Apr 2016, 22:34 last edited by
                                        #32

                                        If you scroll up a few posts, there is the source code.

                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on 7 Apr 2016, 19:56 last edited by
                                          #33

                                          Do you mean the VB code ? Then you generate an IV in there, and use a different one when decrypting your string or am I missing something there ?

                                          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

                                          23/42

                                          5 Apr 2016, 20:34

                                          • Login

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