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. Encrypt and Decrypt function in JS to Qt
Forum Updated to NodeBB v4.3 + New Features

Encrypt and Decrypt function in JS to Qt

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 679 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    Damian7546
    wrote on 29 Jan 2025, 08:08 last edited by aha_1980
    #1

    Hi,
    I need help to convert below function to qt:

    // Encrypt
    const encrypt = (key, message) => {
      const cipher = crypto.createCipheriv('aes-128-ecb', key, null)
      cipher.setAutoPadding(false)
      const encryptedData = Buffer.concat([cipher.update(message), cipher.final()])
    
      return encryptedData
    }
    
    // Decrypt
    const decrypt = (key, data) => {
      const decipher = crypto.createDecipheriv('aes-128-ecb', key, null)
      decipher.setAutoPadding(false)
      const decryptedData = Buffer.concat([decipher.update(data), decipher.final()])
    
      return decryptedData
    }
    
    
    J 1 Reply Last reply 29 Jan 2025, 08:21
    0
    • D Damian7546
      29 Jan 2025, 08:08

      Hi,
      I need help to convert below function to qt:

      // Encrypt
      const encrypt = (key, message) => {
        const cipher = crypto.createCipheriv('aes-128-ecb', key, null)
        cipher.setAutoPadding(false)
        const encryptedData = Buffer.concat([cipher.update(message), cipher.final()])
      
        return encryptedData
      }
      
      // Decrypt
      const decrypt = (key, data) => {
        const decipher = crypto.createDecipheriv('aes-128-ecb', key, null)
        decipher.setAutoPadding(false)
        const decryptedData = Buffer.concat([decipher.update(data), decipher.final()])
      
        return decryptedData
      }
      
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 29 Jan 2025, 08:21 last edited by
      #2

      @Damian7546 Take a look at https://github.com/bricke/Qt-AES

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 2 Replies Last reply 30 Jan 2025, 08:51
      2
      • J jsulm
        29 Jan 2025, 08:21

        @Damian7546 Take a look at https://github.com/bricke/Qt-AES

        D Offline
        D Offline
        Damian7546
        wrote on 30 Jan 2025, 08:51 last edited by Damian7546
        #3

        @jsulm looks good.

        QByteArray crypt(QByteArray key, QByteArray message)
        {
            QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
            return encryption.encode(message, key);
        
        }
        QByteArray decrypt(QByteArray key, QByteArray data)
        {
            QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
            return encryption.decode(data, key);
        }
        
        1 Reply Last reply
        1
        • D Damian7546 has marked this topic as solved on 30 Jan 2025, 08:52
        • J jsulm
          29 Jan 2025, 08:21

          @Damian7546 Take a look at https://github.com/bricke/Qt-AES

          D Offline
          D Offline
          Damian7546
          wrote on 26 Feb 2025, 18:45 last edited by Damian7546
          #4

          @jsulm I still problem with encrypt message in qt.

          I preapred simple function based on Qt-AES

          QByteArray encrypt(QByteArray key, QByteArray message)
          {
              QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
              return encryption.encode(message, key);
          }
          

          And run example:

          qDebug() << encrypt("test","test").toHex();
          

          And I get empty output.

          The same key and message I encrypt in JS example like below :

          var crypto = require('crypto');
          var mykey = crypto.createCipher('aes-128-ecb', 'test');
          var mystr = mykey.update('test')
          mystr += mykey.final('hex');
          console.log(mystr);
          

          And the result is :
          35e7a77ebef24d71581bc4f6a7fb77f0

          Why in qt doesnt work ?

          B 1 Reply Last reply 27 Feb 2025, 07:46
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 26 Feb 2025, 19:34 last edited by
            #5

            Hi,

            Did you first try the Qt-AES examples to ensure it's working properly ?
            If so, then modify the example to match your current setup. Is it still working ?

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

            D 1 Reply Last reply 27 Feb 2025, 06:06
            0
            • S SGaist
              26 Feb 2025, 19:34

              Hi,

              Did you first try the Qt-AES examples to ensure it's working properly ?
              If so, then modify the example to match your current setup. Is it still working ?

              D Offline
              D Offline
              Damian7546
              wrote on 27 Feb 2025, 06:06 last edited by Damian7546
              #6

              @SGaist I use calling from the example described in Qt-AES -> Example -> Sample code using a 128bit key in ECB mode .

              1 Reply Last reply
              0
              • D Damian7546
                26 Feb 2025, 18:45

                @jsulm I still problem with encrypt message in qt.

                I preapred simple function based on Qt-AES

                QByteArray encrypt(QByteArray key, QByteArray message)
                {
                    QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
                    return encryption.encode(message, key);
                }
                

                And run example:

                qDebug() << encrypt("test","test").toHex();
                

                And I get empty output.

                The same key and message I encrypt in JS example like below :

                var crypto = require('crypto');
                var mykey = crypto.createCipher('aes-128-ecb', 'test');
                var mystr = mykey.update('test')
                mystr += mykey.final('hex');
                console.log(mystr);
                

                And the result is :
                35e7a77ebef24d71581bc4f6a7fb77f0

                Why in qt doesnt work ?

                B Offline
                B Offline
                Bonnie
                wrote on 27 Feb 2025, 07:46 last edited by Bonnie
                #7

                @Damian7546

                1. The output is empty because your key is not valid. It needs to be 16 bytes in your case (AES128).
                2. You are using the deprecated crypto.createCipher(algorithm, password) in Node.js which is different from the current crypto.createCipheriv(algorithm, key, iv).
                  From the Node.js doc, you can find that

                The implementation of crypto.createCipher() derives keys using the OpenSSL function EVP_BytesToKey with the digest algorithm set to MD5, one iteration, and no salt.

                As I can see this Qt-AES project only supports key/iv mode, so you either change your js code to use crypto.createCipheriv with a valid key, or you write a function to do the same as EVP_BytesToKey. You can also link openssl and call EVP_BytesToKey, actually if you do that you can try learning openssl APIs to do all the encrytion and decryption parts you need.

                1. Another thing need to change is to set the padding mode to PKCS7
                QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::PKCS7);
                
                D 1 Reply Last reply 27 Feb 2025, 08:36
                2
                • B Bonnie
                  27 Feb 2025, 07:46

                  @Damian7546

                  1. The output is empty because your key is not valid. It needs to be 16 bytes in your case (AES128).
                  2. You are using the deprecated crypto.createCipher(algorithm, password) in Node.js which is different from the current crypto.createCipheriv(algorithm, key, iv).
                    From the Node.js doc, you can find that

                  The implementation of crypto.createCipher() derives keys using the OpenSSL function EVP_BytesToKey with the digest algorithm set to MD5, one iteration, and no salt.

                  As I can see this Qt-AES project only supports key/iv mode, so you either change your js code to use crypto.createCipheriv with a valid key, or you write a function to do the same as EVP_BytesToKey. You can also link openssl and call EVP_BytesToKey, actually if you do that you can try learning openssl APIs to do all the encrytion and decryption parts you need.

                  1. Another thing need to change is to set the padding mode to PKCS7
                  QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::PKCS7);
                  
                  D Offline
                  D Offline
                  Damian7546
                  wrote on 27 Feb 2025, 08:36 last edited by Damian7546
                  #8

                  @Bonnie Thank for response.

                  skipping the above test,
                  I try send encrypted data to my slave device.
                  The device need 128 bit key, so prepared key in qt is :
                  QByteArray key= "67452301674523014cdc0c0000000000"

                  but something is wrong with encrypted.

                  Above I mentioned about JS function because it is works :

                  // Encrypt
                  const encrypt = (key, message) => {
                    const cipher = crypto.createCipheriv('aes-128-ecb', key, null)
                    cipher.setAutoPadding(false)
                    const encryptedData = Buffer.concat([cipher.update(message), cipher.final()])
                    return encryptedData
                  }
                  

                  So, what can I do to encrypt my data in the same way in JS example ?
                  Maybe the problem is that my key in QByteArray represent 32 bytes , an no 16 bytes ?

                  B 1 Reply Last reply 27 Feb 2025, 08:54
                  0
                  • D Damian7546
                    27 Feb 2025, 08:36

                    @Bonnie Thank for response.

                    skipping the above test,
                    I try send encrypted data to my slave device.
                    The device need 128 bit key, so prepared key in qt is :
                    QByteArray key= "67452301674523014cdc0c0000000000"

                    but something is wrong with encrypted.

                    Above I mentioned about JS function because it is works :

                    // Encrypt
                    const encrypt = (key, message) => {
                      const cipher = crypto.createCipheriv('aes-128-ecb', key, null)
                      cipher.setAutoPadding(false)
                      const encryptedData = Buffer.concat([cipher.update(message), cipher.final()])
                      return encryptedData
                    }
                    

                    So, what can I do to encrypt my data in the same way in JS example ?
                    Maybe the problem is that my key in QByteArray represent 32 bytes , an no 16 bytes ?

                    B Offline
                    B Offline
                    Bonnie
                    wrote on 27 Feb 2025, 08:54 last edited by Bonnie
                    #9

                    @Damian7546 said in Encrytp and Decrypt function in JS to Qt:

                    Maybe the problem is that my key in QByteArray represent 32 bytes , an no 16 bytes ?

                    Obviously this is a problem, if I try your above js code with the key you provide, I'll receive "RangeError: Invalid key length".
                    If you want to use a 32 bytes key, then you should use aes-256-ecb.
                    Also is setAutoPadding(false) really necessary? With this, your message data's length "must be a multiple of the cipher's block size".
                    Of course if there's no padding, then you don't need to change QAESEncryption's padding mode.

                    D 1 Reply Last reply 27 Feb 2025, 09:19
                    0
                    • B Bonnie
                      27 Feb 2025, 08:54

                      @Damian7546 said in Encrytp and Decrypt function in JS to Qt:

                      Maybe the problem is that my key in QByteArray represent 32 bytes , an no 16 bytes ?

                      Obviously this is a problem, if I try your above js code with the key you provide, I'll receive "RangeError: Invalid key length".
                      If you want to use a 32 bytes key, then you should use aes-256-ecb.
                      Also is setAutoPadding(false) really necessary? With this, your message data's length "must be a multiple of the cipher's block size".
                      Of course if there's no padding, then you don't need to change QAESEncryption's padding mode.

                      D Offline
                      D Offline
                      Damian7546
                      wrote on 27 Feb 2025, 09:19 last edited by Damian7546
                      #10

                      @Bonnie @SGaist , I tested encrypt function according with below key and data in Qt , but solution is different with JS example.
                      I prepared message data length which one be multiple of the cipher's block size.

                      Below example in Qt:

                      QByteArray encrypt(QByteArray key, QByteArray message)
                      {
                          QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
                          return encryption.encode(message, key);
                      }
                      
                      qDebug() << "eCommandLine: " << eCommandLine.toHex()<< "  Bytes: " << eCommandLine.length();
                      qDebug() << "encryptKey: " << keys.encryptKey.toHex() << "  Bytes: " << keys.encryptKey.length();
                      QByteArray DATAcrypt =  encrypt(keys.encryptKey, eCommandLine);
                      qDebug() << "DATAcrypt:  " << DATAcrypt.toHex()<<"  Bytes: " << DATAcrypt.length();
                      

                      And result:

                      eCommandLine:         "01000000000754e486bf6c74161e64c8"       Size in bytes:  16
                      EncryptKey:           "67452301674523010c2f910100000000"       Size in bytes:  16
                      DATAcrypt:            "f3693ed674eb3933039a61fdf7a35003"       Size in bytes:  16
                      

                      Below example in JS:

                      var crypto = require('crypto');
                      // Encrypt
                      const encrypt = (key, message) => {
                        const cipher = crypto.createCipheriv('aes-128-ecb', key, null)
                        cipher.setAutoPadding(false)
                        const encryptedData = Buffer.concat([cipher.update(message), cipher.final()])
                        return encryptedData
                      }
                      
                      console.log(eCommandLine);
                      console.log(encryptKey);
                      console.log(encrypt(encryptKey, eCommandLine ));
                      

                      And result:

                      01000000000754e486bf6c74161e64c8
                      <Buffer 67 45 23 01 67 45 23 01 0c 2f 91 01 00 00 00 00>
                      <Buffer 2d 46 9e 27 e4 63 b8 78 81 80 0f 3b 29 2b e1 92 ef b9 62 35 5d dc c7 71 6a 7d 11 a4 72 d7 72 26>
                      

                      @Bonnie Like you can see the encrypted buffers in both solutions are different.
                      Which one is good ?

                      B 1 Reply Last reply 28 Feb 2025, 01:40
                      0
                      • D Damian7546
                        27 Feb 2025, 09:19

                        @Bonnie @SGaist , I tested encrypt function according with below key and data in Qt , but solution is different with JS example.
                        I prepared message data length which one be multiple of the cipher's block size.

                        Below example in Qt:

                        QByteArray encrypt(QByteArray key, QByteArray message)
                        {
                            QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB, QAESEncryption::ZERO);
                            return encryption.encode(message, key);
                        }
                        
                        qDebug() << "eCommandLine: " << eCommandLine.toHex()<< "  Bytes: " << eCommandLine.length();
                        qDebug() << "encryptKey: " << keys.encryptKey.toHex() << "  Bytes: " << keys.encryptKey.length();
                        QByteArray DATAcrypt =  encrypt(keys.encryptKey, eCommandLine);
                        qDebug() << "DATAcrypt:  " << DATAcrypt.toHex()<<"  Bytes: " << DATAcrypt.length();
                        

                        And result:

                        eCommandLine:         "01000000000754e486bf6c74161e64c8"       Size in bytes:  16
                        EncryptKey:           "67452301674523010c2f910100000000"       Size in bytes:  16
                        DATAcrypt:            "f3693ed674eb3933039a61fdf7a35003"       Size in bytes:  16
                        

                        Below example in JS:

                        var crypto = require('crypto');
                        // Encrypt
                        const encrypt = (key, message) => {
                          const cipher = crypto.createCipheriv('aes-128-ecb', key, null)
                          cipher.setAutoPadding(false)
                          const encryptedData = Buffer.concat([cipher.update(message), cipher.final()])
                          return encryptedData
                        }
                        
                        console.log(eCommandLine);
                        console.log(encryptKey);
                        console.log(encrypt(encryptKey, eCommandLine ));
                        

                        And result:

                        01000000000754e486bf6c74161e64c8
                        <Buffer 67 45 23 01 67 45 23 01 0c 2f 91 01 00 00 00 00>
                        <Buffer 2d 46 9e 27 e4 63 b8 78 81 80 0f 3b 29 2b e1 92 ef b9 62 35 5d dc c7 71 6a 7d 11 a4 72 d7 72 26>
                        

                        @Bonnie Like you can see the encrypted buffers in both solutions are different.
                        Which one is good ?

                        B Offline
                        B Offline
                        Bonnie
                        wrote on 28 Feb 2025, 01:40 last edited by Bonnie
                        #11

                        @Damian7546 Your input data is different according to the output results.
                        In Qt/C++ code, "eCommandLine" is 16-bytes binary raw data whose hex string is "01000000000754e486bf6c74161e64c8".
                        In JS code, "eCommandLine" is a 32-bytes string whose content is plain "01000000000754e486bf6c74161e64c8".

                        D 1 Reply Last reply 28 Feb 2025, 13:21
                        0
                        • B Bonnie
                          28 Feb 2025, 01:40

                          @Damian7546 Your input data is different according to the output results.
                          In Qt/C++ code, "eCommandLine" is 16-bytes binary raw data whose hex string is "01000000000754e486bf6c74161e64c8".
                          In JS code, "eCommandLine" is a 32-bytes string whose content is plain "01000000000754e486bf6c74161e64c8".

                          D Offline
                          D Offline
                          Damian7546
                          wrote on 28 Feb 2025, 13:21 last edited by Damian7546
                          #12

                          @Bonnie I fixed buffer in JS and solutions are the same.
                          JS result:

                          <Buffer 01 00 00 00 00 07 54 e4 86 bf 6c 74 16 1e 64 c8>
                          <Buffer 67 45 23 01 67 45 23 01 0c 2f 91 01 00 00 00 00>
                          <Buffer f3 69 3e d6 74 eb 39 33 03 9a 61 fd f7 a3 50 03>
                          

                          Qt result:

                          eCommandLine:         "01000000000754e486bf6c74161e64c8"       Size in bytes:  16
                          EncryptKey:           "67452301674523010c2f910100000000"       Size in bytes:  16
                          DATAcrypt:            "f3693ed674eb3933039a61fdf7a35003"       Size in bytes:  16
                          
                          1 Reply Last reply
                          0

                          6/12

                          27 Feb 2025, 06:06

                          • Login

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