qt openssl AES decrypting what already encrypted problem
-
wrote on 10 May 2022, 04:51 last edited by Proton Phoenix 5 Oct 2022, 08:41
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 <3QByteArray 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);
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 -
wrote on 10 May 2022, 09:16 last edited by
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
-
wrote on 10 May 2022, 08:34 last edited by
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 :-(
-
wrote on 10 May 2022, 08:39 last edited by Proton Phoenix 5 Oct 2022, 08:43This post is deleted!
-
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 :-(
wrote on 10 May 2022, 08:44 last edited by@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
-
wrote on 10 May 2022, 09:16 last edited by
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
-
wrote on 10 May 2022, 09:38 last edited by
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/6