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. How to encrypt and decrypt text content on QT?

How to encrypt and decrypt text content on QT?

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 18.6k 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.
  • V Offline
    V Offline
    VRonin
    wrote on 5 Sept 2017, 15:05 last edited by
    #9

    qmake: http://doc.qt.io/qt-5/third-party-libraries.html
    Qt Creator: http://doc.qt.io/qtcreator/creator-project-qmake-libraries.html

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    W 1 Reply Last reply 5 Sept 2017, 15:27
    0
    • V VRonin
      5 Sept 2017, 15:05

      qmake: http://doc.qt.io/qt-5/third-party-libraries.html
      Qt Creator: http://doc.qt.io/qtcreator/creator-project-qmake-libraries.html

      W Offline
      W Offline
      William.Tran
      wrote on 5 Sept 2017, 15:27 last edited by
      #10

      @VRonin

      I followed your links add this to .pro file but it does not help.

      INCLUDEPATH += /usr/local/Cellar/openssl/1.0.2k/lib/
      LIBS += -L/usr/local/Cellar/openssl/1.0.2k/lib/ -lcrypto

      I could not use : #include <openssl/evp.h>

      W 1 Reply Last reply 5 Sept 2017, 15:32
      0
      • W William.Tran
        5 Sept 2017, 15:27

        @VRonin

        I followed your links add this to .pro file but it does not help.

        INCLUDEPATH += /usr/local/Cellar/openssl/1.0.2k/lib/
        LIBS += -L/usr/local/Cellar/openssl/1.0.2k/lib/ -lcrypto

        I could not use : #include <openssl/evp.h>

        W Offline
        W Offline
        William.Tran
        wrote on 5 Sept 2017, 15:32 last edited by
        #11

        I could link openssl lib to my project by :

        INCLUDEPATH += /usr/local/Cellar/openssl/1.0.2k/include
        LIBS += -L/usr/local/Cellar/openssl/1.0.2k/lib -lcrypto

        W 1 Reply Last reply 5 Sept 2017, 16:27
        2
        • W William.Tran
          5 Sept 2017, 15:32

          I could link openssl lib to my project by :

          INCLUDEPATH += /usr/local/Cellar/openssl/1.0.2k/include
          LIBS += -L/usr/local/Cellar/openssl/1.0.2k/lib -lcrypto

          W Offline
          W Offline
          William.Tran
          wrote on 5 Sept 2017, 16:27 last edited by
          #12

          @VRonin

          in this lib : https://gist.github.com/mythosil/1292328

          I saw he used: EVP_aes_128_cbc and i got the error when my private key has length is 64. What EVP i should use?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 5 Sept 2017, 16:33 last edited by VRonin 9 May 2017, 16:36
            #13

            AES standards only supports 128, 192, and 256-bit key sizes. Are you maybe adding some padding to the key?

            EDIT:

            In fact, in your javascript code, you use CryptoJS.SHA256 so you are creating a 256 bit hash of your key. you can use QCryptographicHash to create such a thing

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            S 1 Reply Last reply 5 Sept 2017, 16:54
            0
            • V VRonin
              5 Sept 2017, 16:33

              AES standards only supports 128, 192, and 256-bit key sizes. Are you maybe adding some padding to the key?

              EDIT:

              In fact, in your javascript code, you use CryptoJS.SHA256 so you are creating a 256 bit hash of your key. you can use QCryptographicHash to create such a thing

              S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 5 Sept 2017, 16:54 last edited by
              #14

              @VRonin said in How to encrypt and decrypt text content on QT?:

              256 bit hash of your key. you can use QCryptographicHash to create such a thing

              Careful with that, though: Qt had a bug in SHA calculation. See this

              (Z(:^

              V 1 Reply Last reply 5 Sept 2017, 16:59
              1
              • S sierdzio
                5 Sept 2017, 16:54

                @VRonin said in How to encrypt and decrypt text content on QT?:

                256 bit hash of your key. you can use QCryptographicHash to create such a thing

                Careful with that, though: Qt had a bug in SHA calculation. See this

                V Offline
                V Offline
                VRonin
                wrote on 5 Sept 2017, 16:59 last edited by VRonin 9 May 2017, 17:01
                #15

                @sierdzio I was not aware of that, but that bug relates to SHA3 while OP apparently is using Sha2.

                Alternatively you can still use libcrypto for calculating the hash too, see https://stackoverflow.com/questions/2262386/generate-sha256-with-openssl-and-c

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                1
                • W Offline
                  W Offline
                  William.Tran
                  wrote on 6 Sept 2017, 02:47 last edited by William.Tran 9 Jun 2017, 04:46
                  #16

                  @VRonin @sierdzio

                  I tried to used key and iv key which is generated from my private key by javascript. but i still got the issue as i mentioned:

                  The key length required 16. But my key has length 64. I think it belongs to "EVP_aes_128_cbc" isn't it?

                  My code as below:

                    const char *key, *iv;
                    int key_length, iv_length, data_length;
                  
                    key = QByteArray::fromHex(strKey.toLatin1());
                    key_length = strlen(key);
                  
                    iv = strIV.toLatin1().data();
                    iv_length = strlen(iv);
                  
                    data_length = strlen(data);
                  
                    const EVP_CIPHER *cipher;
                    int cipher_key_length, cipher_iv_length;
                    cipher = EVP_aes_128_cbc();
                    cipher_key_length = EVP_CIPHER_key_length(cipher);
                    cipher_iv_length = EVP_CIPHER_iv_length(cipher);
                  
                    if (key_length != cipher_key_length) {
                      fprintf(stderr, "Error: key length must be %d\n", cipher_key_length);
                      fprintf(stderr, "current key length: %d\n", key_length);
                      exit(EXIT_FAILURE);
                    }
                    if (iv_length != cipher_iv_length) {
                      fprintf(stderr, "Error: iv length must be %d\n", cipher_iv_length);
                      exit(EXIT_FAILURE);
                    }
                  
                    const char *p;
                    char *datax;
                    int i, datax_length;
                  
                    datax = (char *)malloc(data_length);
                    for (p = data, i = 0; *p != '\0'; p += 2, i++) {
                      char buf[3];
                      buf[0] = *p;
                      buf[1] = *(p+1);
                      buf[2] = '\0';
                      datax[i] = strtol(buf, NULL, 16);
                    }
                    datax_length = i;
                  
                    EVP_CIPHER_CTX ctx;
                  
                    EVP_CIPHER_CTX_init(&ctx);
                    EVP_DecryptInit_ex(&ctx, cipher, NULL, (unsigned char *)key, (unsigned char *)iv);
                  
                    int plain_length, final_length;
                    unsigned char *plaintext;
                  
                    plain_length = datax_length;
                    plaintext = (unsigned char *)malloc(plain_length + 1);
                  
                    EVP_DecryptUpdate(&ctx, plaintext, &plain_length, (unsigned char *)datax, datax_length);
                    EVP_DecryptFinal_ex(&ctx, plaintext + plain_length, &final_length);
                  
                    plaintext[plain_length + final_length] = '\0';
                    printf("%s\n", plaintext);
                  
                    free(plaintext);
                    free(datax);
                  
                    EVP_CIPHER_CTX_cleanup(&ctx);
                  

                  The log informations:

                  Error: key length must be 16
                  current key length: 17

                  V 1 Reply Last reply 6 Sept 2017, 06:52
                  0
                  • W William.Tran
                    6 Sept 2017, 02:47

                    @VRonin @sierdzio

                    I tried to used key and iv key which is generated from my private key by javascript. but i still got the issue as i mentioned:

                    The key length required 16. But my key has length 64. I think it belongs to "EVP_aes_128_cbc" isn't it?

                    My code as below:

                      const char *key, *iv;
                      int key_length, iv_length, data_length;
                    
                      key = QByteArray::fromHex(strKey.toLatin1());
                      key_length = strlen(key);
                    
                      iv = strIV.toLatin1().data();
                      iv_length = strlen(iv);
                    
                      data_length = strlen(data);
                    
                      const EVP_CIPHER *cipher;
                      int cipher_key_length, cipher_iv_length;
                      cipher = EVP_aes_128_cbc();
                      cipher_key_length = EVP_CIPHER_key_length(cipher);
                      cipher_iv_length = EVP_CIPHER_iv_length(cipher);
                    
                      if (key_length != cipher_key_length) {
                        fprintf(stderr, "Error: key length must be %d\n", cipher_key_length);
                        fprintf(stderr, "current key length: %d\n", key_length);
                        exit(EXIT_FAILURE);
                      }
                      if (iv_length != cipher_iv_length) {
                        fprintf(stderr, "Error: iv length must be %d\n", cipher_iv_length);
                        exit(EXIT_FAILURE);
                      }
                    
                      const char *p;
                      char *datax;
                      int i, datax_length;
                    
                      datax = (char *)malloc(data_length);
                      for (p = data, i = 0; *p != '\0'; p += 2, i++) {
                        char buf[3];
                        buf[0] = *p;
                        buf[1] = *(p+1);
                        buf[2] = '\0';
                        datax[i] = strtol(buf, NULL, 16);
                      }
                      datax_length = i;
                    
                      EVP_CIPHER_CTX ctx;
                    
                      EVP_CIPHER_CTX_init(&ctx);
                      EVP_DecryptInit_ex(&ctx, cipher, NULL, (unsigned char *)key, (unsigned char *)iv);
                    
                      int plain_length, final_length;
                      unsigned char *plaintext;
                    
                      plain_length = datax_length;
                      plaintext = (unsigned char *)malloc(plain_length + 1);
                    
                      EVP_DecryptUpdate(&ctx, plaintext, &plain_length, (unsigned char *)datax, datax_length);
                      EVP_DecryptFinal_ex(&ctx, plaintext + plain_length, &final_length);
                    
                      plaintext[plain_length + final_length] = '\0';
                      printf("%s\n", plaintext);
                    
                      free(plaintext);
                      free(datax);
                    
                      EVP_CIPHER_CTX_cleanup(&ctx);
                    

                    The log informations:

                    Error: key length must be 16
                    current key length: 17

                    V Offline
                    V Offline
                    VRonin
                    wrote on 6 Sept 2017, 06:52 last edited by
                    #17

                    @William.Tran That's what we were discussing in the 3 posts above. In you javascript you don't use your key directly but you use a 256bit SHA2 hash of the key as input to the decrypter. To create that hash you can use key = QCryptographicHash::hash(strKey.toUtf8(),QCryptographicHash::Sha256);

                    P.S.

                    const char *key
                    key = QByteArray::fromHex(strKey.toLatin1());

                    Are you sure it compiles?

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    W 1 Reply Last reply 6 Sept 2017, 07:00
                    1
                    • V VRonin
                      6 Sept 2017, 06:52

                      @William.Tran That's what we were discussing in the 3 posts above. In you javascript you don't use your key directly but you use a 256bit SHA2 hash of the key as input to the decrypter. To create that hash you can use key = QCryptographicHash::hash(strKey.toUtf8(),QCryptographicHash::Sha256);

                      P.S.

                      const char *key
                      key = QByteArray::fromHex(strKey.toLatin1());

                      Are you sure it compiles?

                      W Offline
                      W Offline
                      William.Tran
                      wrote on 6 Sept 2017, 07:00 last edited by
                      #18

                      @VRonin said in How to encrypt and decrypt text content on QT?:

                      key = QCryptographicHash::hash(strKey.toUtf8(),QCryptographicHash::Sha256);

                      I already generated key by that code. But did not help at all.

                      I tried to use
                      key = QByteArray::fromHex(strKey.toLatin1());

                      Because the "strKey" is Hash Key which was generated by javascript.

                      1 Reply Last reply
                      0

                      18/18

                      6 Sept 2017, 07:00

                      • Login

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