Results 1 to 16 of 16

Thread: Howto: Qt and RSA crypto

  1. #1
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Howto: Qt and RSA crypto

    Hi,

    How do I add support for RSA to my Qt project? (MinGW compiler)
    I'm trying to read a public key by calling PEM_read_RSAPublicKey.

    This is what I've done so far, but it's not working for me:

    I have OpenSSL 0.9.8m installed (binary redistributable).
    I have added
    Qt Code:
    1. INCLUDEPATH += "C:/OpenSSL/include";
    2. LIBS += -L"C:/OpenSSL/lib/MinGW"
    3. LIBS += -llibeay32
    To copy to clipboard, switch view to plain text mode 
    to my project file

    I have the libeay32.dll from C:\OpenSSL\bin\ in my debug folder

    When I run the program it always crashes when it calls PEM_read_RSAPublicKey
    Last edited by jonks; 17th March 2010 at 15: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: Howto: Qt and RSA crypto

    Can we see the exact code? What are you passing to PEM_read_RSAPublicKey()?
    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
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Howto: Qt and RSA crypto

    Qt Code:
    1. #include <openssl/pem.h>
    2. #include <openssl/evp.h>
    3. #include <openssl/applink.c>
    4.  
    5. int LoadPublicKey( const char* keyFile )
    6. {
    7. FILE* f = fopen( keyFile, "r" );
    8. if (!f) return FALSE;
    9.  
    10. RSA* pRSA = PEM_read_RSAPublicKey( f, NULL, NULL, NULL );
    11. if (!pRSA)
    12. {
    13. fclose( f );
    14. return 0;
    15. }
    16. fclose( f );
    17. return (int)pRSA;
    18. }
    19.  
    20.  
    21. int main()
    22. {
    23. CRYPTO_malloc_init();
    24. EVP_add_cipher( EVP_aes_256_cbc() );
    25.  
    26. int keyPub = LoadPublicKey( "public.pem" );
    27. ....
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 

    The same code works fine when compiled by a VC6 compiler.
    Last edited by jonks; 18th March 2010 at 07:35.

  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: Howto: Qt and RSA crypto

    Do you have OpenSSL built for your compiler? Can you build and successfully run any of the examples or tests that come with OpenSSL?

    What happens if you changt the call to:

    Qt Code:
    1. RSA *rsa = new RSA;
    2. PEM_read_RSAPublicKey( f, &rsa, NULL, NULL );
    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.


  5. #5
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Howto: Qt and RSA crypto

    >> Do you have OpenSSL built for your compiler

    No - I installed OpenSSL using Win32OpenSSL-0_9_8m.msi.
    I assumed the last paragraph of this FAQ covered MinGW http://www.openssl.org/support/faq.html#PROG2
    As per 0.9.8 the above limitation is eliminated for .DLLs. OpenSSL .DLLs compiled with some specific run-time option [we insist on the default /MD] can be deployed with application compiled with different option or even different compiler.
    Using
    Qt Code:
    1. RSA *rsa = new RSA;
    2. PEM_read_RSAPublicKey( f, &rsa, NULL, NULL )
    To copy to clipboard, switch view to plain text mode 
    does not resolve the crash.

  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: Howto: Qt and RSA crypto

    Can we see the full backtrace of the crash?
    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
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Howto: Qt and RSA crypto

    There is no backtrace.
    The app just exits, gdb emits
    Program exited with code 030000002027
    I've seen this behaviour (crash no stack trace) before with VS compiled apps that use OpenSSL if the dll does not match the link time library.
    However, I'm confident that the only libeay32 dll on my computer is the one installed into c:\openssl\.

    I'm going to try using Crypto++ instead.
    Last edited by jonks; 18th March 2010 at 13:25.

  8. #8
    Join Date
    Mar 2010
    Posts
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Howto: Qt and RSA crypto

    To be sure, you can compile OpenSSL from source with the same compiler that you compile your qt app (mingw's g++ that comes with qt). The binary distribution that you use is probably compiled with msvc++ in which case you could also try to simply extract the import libraries directly from dlls.

  9. #9
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Howto: Qt and RSA crypto

    Well I just gave up on Crypto++ - it's too difficult to use and wheres the docs?!
    + I think that was designed by an abstraction astronaut!

    I rolled back to the original sources that use OpenSSL, recompiled and it worked first time!!! ???
    I didn't need to recompile the OpenSSL dll using Qt's MinGW either, the binary distro worked.

    Now I'm totally confused as to why it works today but not a few days ago.

  10. #10
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    16
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto: Qt and RSA crypto

    The problem seem solved but maybe this information could be interesting:
    There is a Qt Cryptographic architecture at the URL http://delta.affinix.com/qca/
    i've used it under windows but only for md5crypt so i'm not sure it could be used in this case.
    Bye.

  11. #11
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Howto: Qt and RSA crypto

    Quote Originally Posted by jonks View Post
    There is no backtrace.
    The app just exits, gdb emits
    > Program exited with code 030000002027
    Hi, i'm having the very same problem with OpenSSL 0.9.8-m and Qt 4.6 (windows). The problem seems to be related to operations involving a FILE*, while every other function is working fine. I'm going to digg that way, nothing began to suddenly work in my case

  12. #12
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Howto: Qt and RSA crypto

    Make sure you call OpenSSL_add_all_algorithms() before calling anything else.

  13. #13
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Howto: Qt and RSA crypto

    Quote Originally Posted by jonks View Post
    Make sure you call OpenSSL_add_all_algorithms() before calling anything else.
    sure i do. in the end, i had to rebuild OpenSSL from source (version 0.9.8-h, not -m) downloaded from http://gnuwin32.sourceforge.net/packages/openssl.htm, and everything worked fine.

  14. #14
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Howto: Qt and RSA crypto

    by the way, anybody knows which version of OpenSSL Qt 4.6 for Windows was built and linked against ?

  15. #15
    Join Date
    May 2009
    Posts
    63
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Howto: Qt and RSA crypto

    Quote Originally Posted by Ubik View Post
    by the way, anybody knows which version of OpenSSL Qt 4.6 for Windows was built and linked against ?
    I don't think it was.

  16. #16
    Join Date
    Apr 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Howto: Qt and RSA crypto

    look at qt\configure.cache:

    -debug-and-release
    -confirm-license
    -plugin-sql-sqlite
    -plugin-sql-odbc
    -qt-libpng
    -qt-libjpeg
    -openssl
    -opensource
    -no-incredibuild-xge
    -dont-process

Similar Threads

  1. Compiling & using Crypto++ with mingw version of Qt
    By FS Lover in forum Qt Programming
    Replies: 14
    Last Post: 16th January 2018, 12:29
  2. Problem in crypto :: RSA
    By erfanonline in forum Qt Programming
    Replies: 3
    Last Post: 21st March 2009, 22:40
  3. Replies: 5
    Last Post: 19th November 2008, 18:54
  4. Crypto++ and Qt
    By vermarajeev in forum Qt Programming
    Replies: 19
    Last Post: 1st March 2007, 04:41
  5. Crypto++ and Qt3.3.5
    By vermarajeev in forum General Programming
    Replies: 1
    Last Post: 9th February 2007, 08:26

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.