Results 1 to 17 of 17

Thread: QT and OpenSSL EVP.h

  1. #1
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Hi,

    I've been looking around for C++ or better QT examples of AES encryption using OpenSSL EVP but I've been messing around with some code with no luck.

    I think this is a lot to ask, but can someone provide me an example of AES encryption using the EVP in QT?

    I've tried to implement the code here without any luck

    Currently my code is:

    Qt Code:
    1. void qkCrypto::AES_CBC(const unsigned char *string, const unsigned char *key, const unsigned char *iv)
    2. {
    3.  
    4. int outlen;
    5. unsigned char *out;
    6.  
    7. EVP_CIPHER_CTX ctx;
    8. EVP_CIPHER_CTX_init(&ctx);
    9. const EVP_CIPHER *cipher = EVP_aes_128_cbc();
    10.  
    11. EVP_EncryptInit(&ctx,cipher,key,iv);
    12. EVP_EncryptUpdate(&ctx,out,&outlen,string,strlen(string));
    13.  
    14. EVP_EncryptFinal(&ctx,out,&outlen);
    15.  
    16. EVP_CIPHER_CTX_cleanup(&ctx);
    17.  
    18. qDebug() << out;
    19.  
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    But when I try to compile I get this:

    [...]/qkcrypto.cpp: In member function 'void qkCrypto::AES_CBC(const unsigned char*, const unsigned char*, const unsigned char*)':
    [...]/qkcrypto.cpp:39: error: invalid conversion from 'const unsigned char*' to 'const char*'
    [...]/qkcrypto.cpp:39: error: initializing argument 1 of 'size_t strlen(const char*)'
    How should I solve this... and BTW, how can I get rid of things like "const unsigned char" and make this a little bit more QT friendly using QByteArray instead?

    Thank you all
    Last edited by TCB13; 31st August 2011 at 01:16.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT and OpenSSL EVP.h

    "man EVP_EncryptInit" will give you an example of using EVP. If you're on Windows then you can probably find the manual on OpenSSL webpage too.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Quote Originally Posted by wysota View Post
    "man EVP_EncryptInit" will give you an example of using EVP. If you're on Windows then you can probably find the manual on OpenSSL webpage too.
    Hi, the website I was reading seems to take a bit from the man.
    Anyway, my errors are not related to the usage itself but conversions and casts, can you help me solve the compiler error above?

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT and OpenSSL EVP.h

    Just force the cast by explicit casting to the proper type.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Quote Originally Posted by wysota View Post
    Just force the cast by explicit casting to the proper type.
    Hi, it seems to work with:
    Qt Code:
    1. EVP_EncryptUpdate(&ctx,out,&outlen,string,strlen((const char*)string));
    To copy to clipboard, switch view to plain text mode 

    Anyway, how can I convert this guys here
    Qt Code:
    1. (const unsigned char *string, const unsigned char *key, const unsigned char *iv)
    To copy to clipboard, switch view to plain text mode 
    to QByteArrays?


    Added after 13 minutes:


    I could use something like:

    Qt Code:
    1. QByteArray somedata;
    2. somedata.constData();
    To copy to clipboard, switch view to plain text mode 

    In order to convert them but I'll get an error anyway because the they should be unsigned. --> SOLVED!

    [...] (Old code not really needed, removed, check the new one bellow)

    Well, now I can compile it after changing some things:

    Qt Code:
    1. void qkCrypto::AES_CBC(QByteArray string, QByteArray key, QByteArray iv)
    2. {
    3. int outlen;
    4.  
    5. EVP_CIPHER_CTX ctx;
    6. EVP_CIPHER_CTX_init(&ctx);
    7. const EVP_CIPHER *cipher = EVP_aes_128_cbc();
    8. EVP_EncryptInit(&ctx,cipher, (const unsigned char*)key.constData() , (const unsigned char*)iv.constData() );
    9. EVP_EncryptUpdate(&ctx, (unsigned char*)out.data() ,&outlen, (const unsigned char*)string.constData() ,strlen( string.constData() ));
    10. EVP_EncryptFinal(&ctx, (unsigned char*)out.data() ,&outlen);
    11. EVP_CIPHER_CTX_cleanup(&ctx);
    12.  
    13. qDebug() << "OUT: "<< out;
    14. }
    To copy to clipboard, switch view to plain text mode 

    But the QByteArray out returns nothing. :S

    If I replace the QByteArray by a normal unsigned char qDebug outputs "0x7fff5fbff950" and then the program crashes.

    Thanks
    Last edited by TCB13; 31st August 2011 at 16:07.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT and OpenSSL EVP.h

    If you use QByteArray::data() then you need to ensure there is enough space reserved for the byte array. it seems you are trying to pass an empty byte array to a function that intends to write to the array. This won't work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Quote Originally Posted by wysota View Post
    If you use QByteArray::data() then you need to ensure there is enough space reserved for the byte array. it seems you are trying to pass an empty byte array to a function that intends to write to the array. This won't work.
    How should I proceed here:
    Qt Code:
    1. EVP_EncryptFinal(&ctx, (unsigned char*)out.data() ,&outlen);
    To copy to clipboard, switch view to plain text mode 
    then?

    The second parameter is an output.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT and OpenSSL EVP.h

    Probably something like:
    Qt Code:
    1. out.resize(1024);
    2. outlen = 1024;
    3. EVP_EncryptFinal(&ctx, (unsigned char*)out.data(), &outlen);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    TCB13 (2nd September 2011)

  10. #9
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Quote Originally Posted by wysota View Post
    Probably something like:
    Qt Code:
    1. out.resize(1024);
    2. outlen = 1024;
    3. EVP_EncryptFinal(&ctx, (unsigned char*)out.data(), &outlen);
    To copy to clipboard, switch view to plain text mode 
    Hi,
    Giving a value to "outlen" is not needed but now with the resize I get an output like this:
    "¢ ViÎ ð–«u (`T
    Thanks.

  11. #10
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    I think I wasn't really clear in my last post, the output value is not what it should be, If I try to decode with the key and IV it's wrong. I'll be diving in OpenSSL documentation to make this work and post some code later.

    Thank you all specially wysota.
    (Who ends up saving my code multiple times :P)

  12. #11
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Hi everyone,
    I've new code now almost perfect.

    QByteArray qkCrypto::encrypt_AES_CBC(QByteArray string, QByteArray key, QByteArray ivv)
    {

    char mykey[EVP_MAX_KEY_LENGTH] = { 0 };
    strcpy(mykey,key.constData());

    char iv[EVP_MAX_IV_LENGTH] = { 0 };
    strcpy(iv,ivv.constData());

    char ciphertext[1024];

    const EVP_CIPHER *cipher = EVP_aes_256_cbc();
    int in_len;
    int out_len=0;

    in_len = (string.length());

    qDebug() << "Before encrypt: " << string << "\r\n";

    EVP_EncryptInit(&ctx, cipher, (const unsigned char*)mykey, (const unsigned char*)iv);
    EVP_EncryptUpdate(&ctx, ( unsigned char*)ciphertext, &out_len, (const unsigned char*)string.data(), in_len);
    EVP_EncryptFinal(&ctx, ( unsigned char*)&ciphertext[out_len], &out_len);
    EVP_CIPHER_CTX_cleanup(&ctx);
    qDebug() << "Encrypted: " << ciphertext << "\r\n";

    return ciphertext;

    }
    By some reason the output is only right some times... other times the output is wrong and I can't decrypt it later with OpenSSL.
    What's wrong about this? Some help will be great. :S

    Thanks.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT and OpenSSL EVP.h

    I can give you the right code if you want, it seems I made a complete OpenSSL EVP implementation some time ago
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Quote Originally Posted by wysota View Post
    I can give you the right code if you want, it seems I made a complete OpenSSL EVP implementation some time ago
    Hi,
    My implementation seems to be right but somehow sometimes some extras chars are added to the end of the encrypted string.
    I would really like to understand what's happening there... but If you have a working implementation I would love to see it maybe compare mine and yours.

    Thanks.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT and OpenSSL EVP.h

    My implementation looks like this (at least part of it does):
    Qt Code:
    1. QByteArray qEncrypt(const QString &algorithm, const QByteArray &inba, const QByteArray &key, const QByteArray &iv) {
    2. OpenSSL_add_all_ciphers();
    3. EVP_CIPHER_CTX ctx;
    4. EVP_CIPHER_CTX_init(&ctx);
    5. const EVP_CIPHER *cipher = EVP_get_cipherbyname(qPrintable(algorithm));
    6. if (!cipher) {
    7. EVP_cleanup();
    8. return QByteArray();
    9. }
    10. EVP_EncryptInit_ex(&ctx, cipher, NULL, (const unsigned char*)key.constData(), (const unsigned char*)iv.constData());
    11. int outlen, inlen, tmplen;
    12. inlen = inba.count();
    13. QByteArray outbuf(inlen+EVP_MAX_BLOCK_LENGTH, 0);
    14. // outbuf = inlen+cipher_block-1
    15.  
    16. if (!EVP_EncryptUpdate(&ctx, (unsigned char*)outbuf.data(), &outlen, (const unsigned char*)inba.constData(), inlen)) {
    17. return QByteArray();
    18. }
    19. if (!EVP_EncryptFinal_ex(&ctx, ((unsigned char*)outbuf.data())+outlen, &tmplen)) {
    20. return QByteArray();
    21. }
    22. outlen += tmplen;
    23. EVP_CIPHER_CTX_cleanup(&ctx);
    24. EVP_cleanup();
    25. outbuf.resize(outlen);
    26. return outbuf;
    27. }
    To copy to clipboard, switch view to plain text mode 

    I also have a class that does the encryption that can be called like so:
    Qt Code:
    1. QwwCipher ciph("aes-128-cbc");
    2. ciph.setKey("0123456789ABCDEF");
    3. ciph.setIv("12345678");
    4. QByteArray inba("text to be encrypted");
    5. QByteArray outba;
    6. ciph.setInput(&inba);
    7. ciph.setOutput(&outba);
    8. ciph.encryptAll();
    9. qDebug() << outba.toBase64().constData();
    To copy to clipboard, switch view to plain text mode 

    or so (asynchronous encryption):
    Qt Code:
    1. QCoreApplication app(argc, argv);
    2. ...
    3. QwwCipher ciph("des-ofb");
    4. ciph.setKey("01234567");
    5. ciph.setIv("12345678");
    6. QByteArray inba("text to be encrypted");
    7. ciph.setInput(&inba);
    8. QFile file("cipher.bin");
    9. ciph.setOutput(&file);
    10. ciph.encrypt();
    11. QObject::connect(&ciph, SIGNAL(done()), &app, SLOT(quit()));
    12. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    Your implementation is incorrectly assuming that the ciphertext is readable. You can't just dump it to stderr, first \0 byte will terminate the printout and a lack of \0 at the end will append garbage to printout. Also you are returning a pointer to a local stack based variable.
    Last edited by wysota; 3rd September 2011 at 14:52.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    TCB13 (3rd September 2011)

  17. #15
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Your code works fine

    I'm still having some problems related to base64 in my program because I'm posting the encrypted data over http and sometimes the decrypted string on the PHP side is wrong. -> Any tips?
    I'm still working on it

    Thanks for all.
    Last edited by TCB13; 3rd September 2011 at 22:41.

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT and OpenSSL EVP.h

    Base64 is not encryption, it is merely encoding. Try retracing your steps on both sides and see where the difference pops up.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #17
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QT and OpenSSL EVP.h

    Quote Originally Posted by wysota View Post
    Base64 is not encryption, it is merely encoding. Try retracing your steps on both sides and see where the difference pops up.
    Yes.. I was looking into AES-CBC because of that. I'm doing now:

    "Some text" --> toAES_CBC() --> toBase64() ----- http post ------ > base64_decode() --> aes->decrypt()

    But sometimes after the base64 is decoded by the PHP the message different. But I'll look seriously into it very soon.

    Yesterday I changed the encoding to hex and I saw that some "0" were missing if I encode the string again to hex after decoding in the PHP side. So maybe PHP is not dealing so well with the AES encrypted ( possible not readable ) chars.
    But I'll check it soon I'm working on my TCP client code now...

    Thanks.

Similar Threads

  1. Help! Qt 4.6.0+Symbian SDK 5 does not support OPENSSL
    By coco in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 8th October 2010, 16:13
  2. Replies: 0
    Last Post: 31st May 2010, 15:10
  3. OpenSSL and RSA :: digital signature
    By josecarlosmissias in forum Qt Programming
    Replies: 5
    Last Post: 7th May 2010, 14:23
  4. Qt 4.5 Openssl problem
    By srikanth_trulyit in forum Installation and Deployment
    Replies: 1
    Last Post: 10th June 2009, 09:54
  5. Get ml.exe to build OpenSSL on VS 2005
    By captchaq in forum General Programming
    Replies: 1
    Last Post: 27th September 2008, 07:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.