PDA

View Full Version : Qt openssl aes lib



zgulser
11th July 2012, 08:54
Hi,

I wrote the following enc/dec code;



AES_KEY enc_key, dec_key;

AES_set_encrypt_key((unsigned char*)p_key, 128, &enc_key);
AES_ecb_encrypt((unsigned char*)textManip, (unsigned char*)enc_out, &enc_key, 1);

AES_set_decrypt_key((unsigned char*)p_key, 128, &dec_key);
AES_decrypt((unsigned char*)enc_out, (unsigned char*)dec_out, &dec_key);


& to test it;




qDebug() << "Original: " << (char*)(text);
enc_out[strlen(p_Text)] = '\0';
qDebug() << "Encrypted: " <<(enc_out);
dec_out[strlen(p_Text)] = '\0';
qDebug() << "Decrypted: " << (dec_out);



the output is;

Original: WirofonPC010101
Encrypted: ??c?'r???)?A?
Decrypted: WirofonPC010101

which seems ecryption is working. But I somehow thought that my encrypted characters are no good. I mean Shouldn't they like

"31298sdf788123" or "122dfsdrwer324" ??

Thanks..

wysota
11th July 2012, 09:28
You're dumping binary data as text (and it is not text) to the console.

zgulser
11th July 2012, 09:57
Ok,



QString enc_str = QString::fromUtf8(enc_out);


should work then..

Added after 23 minutes:

Hi,

I couldn't figured out how to convert binary to txt.. Can you show me a way?

wysota
11th July 2012, 11:52
Ok,



QString enc_str = QString::fromUtf8(enc_out);


should work then..
Not really.

You can try this:

qDebug() << QByteArray(enc_out, size);
but you need to know the size of enc_out content, strlen() will not work here in a general case as the data can contain \0 characters.

zgulser
11th July 2012, 12:18
Hi,



qDebug() <<"Output: " << QByteArray(encryptedData, strlen(encryptedData));


gave actually the same result that is;

Encrypted: ??c?'r???)?A?

replacing strlen with a custom one do not affect the result I guess?

wysota
11th July 2012, 12:23
Try:


qDebug() << "Output:" << QByteArray(encryptedData, ...).toHex();

but still you can't use strlen to find the size of data. You need to read that using OpenSSL functions.