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. qt openssl AES decrypting what already encrypted problem
Forum Updated to NodeBB v4.3 + New Features

qt openssl AES decrypting what already encrypted problem

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 549 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.
  • P Offline
    P Offline
    Proton Phoenix
    wrote on 10 May 2022, 04:51 last edited by Proton Phoenix 5 Oct 2022, 08:41
    #1

    Hi ~~ I Have a problem ... if i encrypt a data then directly decrypt it ... it works and i get the result i want ...
    but if i get a base64 aes256 encrypted message then trying to decrypt the same message it won't work ... even with the same password
    i knew it's salt or IV problem ... but how can i fix those functions and many thanks <3

    QByteArray Cipher::encrypt(const QByteArray &data, const QByteArray &password)
    {
    
        int outLen=0;
        QByteArray dataBuff;dataBuff.resize(data.size()+AES_BLOCK_SIZE);
        EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
        EVP_CIPHER_CTX_init(ctx);
    
        EVP_CIPHER_CTX_init(ctx);
        if(!EVP_EncryptInit(ctx,EVP_aes_256_cbc(),(const unsigned char*)sha256(password).data(),(const unsigned char*)sha256("tryingAES"+password).data())){
            qCritical() << "EVP_EncryptInit_ex() failed " << ERR_error_string(ERR_get_error(), NULL);
    
            return QByteArray();
        }
        if(!EVP_EncryptUpdate(ctx,(unsigned char*)dataBuff.data(),&outLen,(const unsigned char*)data.data(),data.size())){
            qCritical() << "EVP_EncryptUpdate() failed " << ERR_error_string(ERR_get_error(), NULL);
            return QByteArray();
        }
        int tempLen=outLen;
        if(!EVP_EncryptFinal(ctx,(unsigned char*)dataBuff.data()+tempLen,&outLen)){
            qCritical() << "EVP_EncryptFinal_ex() failed "  << ERR_error_string(ERR_get_error(), NULL);
            return QByteArray();
        }
        tempLen+=outLen;
        EVP_CIPHER_CTX_cleanup(ctx);
        dataBuff.resize(tempLen);
        return dataBuff;
    
        EVP_CIPHER_CTX_free(ctx);
    
    }
    

    ^^

    QByteArray Cipher::decrypt(const QByteArray &data, const QByteArray &password)
    {
        int outLen=0;
        QByteArray dataBuff;dataBuff.resize(data.size()+AES_BLOCK_SIZE);
        EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
        EVP_CIPHER_CTX_init(ctx);
        EVP_DecryptInit(ctx,EVP_aes_256_cbc(),(const unsigned char*)sha256(password).data(),(const unsigned char*)sha256("tryingAES"+password).data());
        EVP_DecryptUpdate(ctx,(unsigned char*)dataBuff.data(),&outLen,(const unsigned char*)data.data(),data.size());
        int tempLen=outLen;
        EVP_DecryptFinal(ctx,(unsigned char*)dataBuff.data()+tempLen,&outLen);
        tempLen+=outLen;
        EVP_CIPHER_CTX_cleanup(ctx);
        dataBuff.resize(tempLen);
        return dataBuff;
    }
    
    ^^
    QByteArray Cipher::sha256(const QByteArray &text)
    {
            unsigned int outLen=0;
            QByteArray dataBuff; dataBuff.resize(EVP_MAX_MD_SIZE);
    
            EVP_MD_CTX *evpMdCtx=EVP_MD_CTX_new();
            EVP_DigestInit(evpMdCtx, EVP_sha256());
            EVP_DigestUpdate(evpMdCtx, text.data(), text.size());
            EVP_DigestFinal_ex(evpMdCtx, (unsigned char *)dataBuff.data(), &outLen);
    
            dataBuff.resize(outLen);
            qDebug()<<"databuffer"<<dataBuff.toHex();
           //   EVP_MD_CTX_cleanup(evpMdCtx);
    
            return dataBuff;
    }
    

    and this is the calling function ..

    Cipher wrapper;
      QByteArray pass="passwordtrying";
      QByteArray data =ui->encrypttextedit->toPlainText().toUtf8();
    
      QByteArray decrypt=wrapper.decrypt(data,pass);
      ui->decrypttextedit->setText(decrypt);
    

    0e38ab82-0d0e-47a1-a246-2a24800efff4-image.png
    The Result Should be a MAC ADDRESS like : E3:53:24:H3 ... Just As example
    decrypting works only if i encrypt and decrypt in the same function .. but i want to decrypt what already encrypted from the software

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 10 May 2022, 09:16 last edited by
      #5

      Hi, I don't see any key and initialization vector computation, it's done with the EVP_BytesToKey() function, for more see here

      Two calls to EVP_CIPHER_CTX_Init is correct but you need two different contexts I think not the same context twice :-(

      ...
       EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
       EVP_CIPHER_CTX_init(ctx);
      
       EVP_CIPHER_CTX_init(ctx);
      ...
      

      AES is more than 20 years old so there are lots of examples out there, I wrote a Qt demo console app couple of years ago in the forum

      1 Reply Last reply
      2
      • H Offline
        H Offline
        hskoglund
        wrote on 10 May 2022, 08:34 last edited by
        #2

        Hi, that base64-encoded string that begins with "GFdws...." does not look like valid base64, because the length must be a multiple of 4, and it looks like it's 38 characters :-(

        P 1 Reply Last reply 10 May 2022, 08:44
        0
        • P Offline
          P Offline
          Proton Phoenix
          wrote on 10 May 2022, 08:39 last edited by Proton Phoenix 5 Oct 2022, 08:43
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • H hskoglund
            10 May 2022, 08:34

            Hi, that base64-encoded string that begins with "GFdws...." does not look like valid base64, because the length must be a multiple of 4, and it looks like it's 38 characters :-(

            P Offline
            P Offline
            Proton Phoenix
            wrote on 10 May 2022, 08:44 last edited by
            #4

            @hskoglund Thank you bro i just modified it for privacy reasons just a moment i will edit the topic

            ~~ Now i encrypted just example mac E4:5C:2F:1A:43

            if i encrypt it and decrypt it in the same function it decrypt it successfully but if i try to decrypt what already encrypted ... it doesn't

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hskoglund
              wrote on 10 May 2022, 09:16 last edited by
              #5

              Hi, I don't see any key and initialization vector computation, it's done with the EVP_BytesToKey() function, for more see here

              Two calls to EVP_CIPHER_CTX_Init is correct but you need two different contexts I think not the same context twice :-(

              ...
               EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
               EVP_CIPHER_CTX_init(ctx);
              
               EVP_CIPHER_CTX_init(ctx);
              ...
              

              AES is more than 20 years old so there are lots of examples out there, I wrote a Qt demo console app couple of years ago in the forum

              1 Reply Last reply
              2
              • P Offline
                P Offline
                Proton Phoenix
                wrote on 10 May 2022, 09:38 last edited by
                #6

                Really Thank you bro <3 you helped me a lot ... for me i was trying to get some examples from github i didn't look so much in openssl documentation a
                thank you the idea now is very clear because of your help
                i wish happy life for you bro <3 many thanks

                1 Reply Last reply
                0

                1/6

                10 May 2022, 04:51

                • Login

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