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.
  • W Offline
    W Offline
    William.Tran
    wrote on 5 Sept 2017, 09:54 last edited by
    #1

    Hi All,

    I am looking forward a solution on QT to encrypt and decrypt text content which is stored in a text file.

    Our text content was encrypted with a private key. Any body know any library for this? I saw that we have crypto++ at this link
    https://stackoverflow.com/questions/12306956/example-of-aes-using-crypto

    But i could not link crypto++ to our project. Could you please help me on this?

    Thank you very much!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Sept 2017, 10:05 last edited by
      #2

      @William.Tran said in How to encrypt and decrypt text content on QT?:

      Our text content was encrypted with a private key.

      But then you should use the same API/tool/software to decrypt it again?

      W 1 Reply Last reply 5 Sept 2017, 10:07
      0
      • M mrjj
        5 Sept 2017, 10:05

        @William.Tran said in How to encrypt and decrypt text content on QT?:

        Our text content was encrypted with a private key.

        But then you should use the same API/tool/software to decrypt it again?

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

        @mrjj

        I would like our application can read that content only. that's why we encrypted content and stored it to file.

        M 1 Reply Last reply 5 Sept 2017, 10:10
        0
        • W William.Tran
          5 Sept 2017, 10:07

          @mrjj

          I would like our application can read that content only. that's why we encrypted content and stored it to file.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 5 Sept 2017, 10:10 last edited by
          #4

          @William.Tran

          But how did u encrypt it in first place?
          What software did use the private key ?

          So are u sure crypto++ can decrypt it ?

          W 1 Reply Last reply 5 Sept 2017, 10:17
          0
          • M mrjj
            5 Sept 2017, 10:10

            @William.Tran

            But how did u encrypt it in first place?
            What software did use the private key ?

            So are u sure crypto++ can decrypt it ?

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

            @mrjj

            Our server has encrypted the file with a private key. On our application download this content and stored to file.

            I could use javascript to decrypt it as below:

                var key = CryptoJS.SHA256(__CRYPTO_AES_PRIVATE_KEY);
                var iv = CryptoJS.enc.Hex.parse(bin2hex(key.toString().substring(0, 16)))
            
                var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv });
            
                return decrypted.toString(CryptoJS.enc.Utf8);
            

            I am not sure crypto++ can do that or not but i tried to build my app with that library however it is not successful.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 5 Sept 2017, 10:18 last edited by
              #6

              From that code alone I think you can use OpenSSL library to decrypt your data on Qt side.

              (Z(:^

              1 Reply Last reply
              1
              • V Offline
                V Offline
                VRonin
                wrote on 5 Sept 2017, 10:54 last edited by
                #7

                both libcrypto (part of OpenSSL) and cryptoc++ can handle AES encryption. Given you are talking about "server" I suppose you are using OpenSSL already so libcrypto is probably the easier way to go. You can see an example of how to do it here: https://gist.github.com/mythosil/1292328 (yes, it's in C as libcrypto is in 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

                W 1 Reply Last reply 5 Sept 2017, 15:01
                2
                • V VRonin
                  5 Sept 2017, 10:54

                  both libcrypto (part of OpenSSL) and cryptoc++ can handle AES encryption. Given you are talking about "server" I suppose you are using OpenSSL already so libcrypto is probably the easier way to go. You can see an example of how to do it here: https://gist.github.com/mythosil/1292328 (yes, it's in C as libcrypto is in C)

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

                  @VRonin, @sierdzio

                  Thank your for you answer. I am using QT 5.9 on MAC, could you tell me how to import openssl to project and how to integrate with your libs?

                  Many thanks.

                  1 Reply Last reply
                  0
                  • 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

                                      4/18

                                      5 Sept 2017, 10:10

                                      topic:navigator.unread, 14
                                      • Login

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