Results 1 to 15 of 15

Thread: Compiling & using Crypto++ with mingw version of Qt

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2009
    Posts
    147
    Thanks
    11
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Compiling & using Crypto++ with mingw version of Qt

    Hi pals!
    I personally had much trouble with these.
    apparently compiled version of crypto++ (cryptopp530win32win64.zip) is build using MSVC and does not work with mingw.
    fortunately I could get it to work finally.
    so I tell you too, step by step, how to do it.

    first download the cryptopp552.zip (crypto++ v5.5.2 sources)
    why cryptopp552.zip? apparently this is the latest version that is successfully compiled with mingw.

    extract the contents of the cryptopp552.zip to C:\cryptopp552

    edit the C:\cryptopp552\fipstest.cpp and replace every 'OutputDebugString' with 'OutputDebugStringA'. (3 replacements in total)
    don't forget to save it!

    delete the C:\cryptopp552\GNUmakefile

    open the Qt command prompt (I used that of the Qt SDK 2009.05)
    input the following commands at the Qt command line:
    c:
    cd \cryptopp552
    qmake -project

    open the cryptopp552.pro (that is now created in C:\cryptopp552)
    in it:
    change TEMPLATE = app to TEMPLATE = lib
    add a line containing LIBS += -lws2_32 at the end.

    type the following commands at the Qt command line:
    qmake
    mingw32-make all

    wait for the build process to finish (may take many minutes)

    now we should have files named libcryptopp552.a and cryptopp552.dll in directories C:\cryptopp552\release and C:\cryptopp552\debug

    copy the C:\cryptopp552\release\libcryptopp552.a to <Qt dir>\lib
    note that there is another directory named lib one level higher in the Qt SDK installation dir. So don't confuse them please.
    copy the C:\cryptopp552\release\cryptopp552.dll to <Qt dir>\bin
    note that there is another directory named bin one level higher in the Qt SDK installation dir. So don't confuse them please.

    create a directory named cryptopp in <Qt dir>\include.
    copy all header (.h) files from the C:\cryptopp552 to <Qt dir>\include\cryptopp.

    now we can test crypto++ and see how to use it in our Qt programs.

    first example is a program that computes an MD5 hash (of a hard coded string):

    main.cpp
    Qt Code:
    1. #include <iostream>
    2.  
    3. #define CRYPTOPP_DEFAULT_NO_DLL
    4. #include <cryptopp/dll.h>
    5. #ifdef CRYPTOPP_WIN32_AVAILABLE
    6. #include <windows.h>
    7. #endif
    8. #include <cryptopp/md5.h>
    9.  
    10. USING_NAMESPACE(CryptoPP)
    11. USING_NAMESPACE(std)
    12. const int MAX_PHRASE_LENGTH=250;
    13.  
    14. int main(int argc, char *argv[]) {
    15.  
    16. CryptoPP::MD5 hash;
    17. byte digest[ CryptoPP::MD5::DIGESTSIZE ];
    18. std::string message = "Hello World!";
    19.  
    20. hash.CalculateDigest( digest, (const byte*)message.c_str(), message.length());
    21.  
    22. CryptoPP::HexEncoder encoder;
    23. std::string output;
    24. encoder.Attach( new CryptoPP::StringSink( output ) );
    25. encoder.Put( digest, sizeof(digest) );
    26. encoder.MessageEnd();
    27.  
    28. std::cout << "Input string: " << message << std::endl;
    29. std::cout << "MD5: " << output << std::endl;
    30.  
    31. return 0;
    32. }
    To copy to clipboard, switch view to plain text mode 

    code from: http://www.cryptopp.com/wiki/Hash_Functions

    remember that you should add these lines to its .pro file before starting to build it:
    LIBS += -lcryptopp552
    CONFIG+=console

    the program should print these on the console window:
    Input string: Hello World!
    MD5: ED076287532E86365E841E92BFC50D8C

    second example is a program that takes 3 arguments at the command line.
    arguments are file names.
    the program then prompts for a Passphrase and then stores an encrypted version of the first file in the second file and then stores the result of decrypting the second file in the third file.
    sample command line I used: release\cryptopptest.exe 1.jpg 2.jpg 3.jpg

    Qt Code:
    1. #include <iostream>
    2.  
    3. #define CRYPTOPP_DEFAULT_NO_DLL
    4. #include <cryptopp/dll.h>
    5. #include <cryptopp/default.h>
    6. #ifdef CRYPTOPP_WIN32_AVAILABLE
    7. #include <windows.h>
    8. #endif
    9.  
    10. USING_NAMESPACE(CryptoPP)
    11. USING_NAMESPACE(std)
    12.  
    13. const int MAX_PHRASE_LENGTH=250;
    14.  
    15. void EncryptFile(const char *in,
    16. const char *out,
    17. const char *passPhrase);
    18. void DecryptFile(const char *in,
    19. const char *out,
    20. const char *passPhrase);
    21.  
    22.  
    23. int main(int argc, char *argv[])
    24. {
    25. try
    26. {
    27. char passPhrase[MAX_PHRASE_LENGTH];
    28. cout << "Passphrase: ";
    29. cin.getline(passPhrase, MAX_PHRASE_LENGTH);
    30. EncryptFile(argv[1], argv[2], passPhrase);
    31. DecryptFile(argv[2], argv[3], passPhrase);
    32. }
    33. catch(CryptoPP::Exception &e)
    34. {
    35. cout << "\nCryptoPP::Exception caught: "
    36. << e.what() << endl;
    37. return -1;
    38. }
    39. catch(std::exception &e)
    40. {
    41. cout << "\nstd::exception caught: " << e.what() << endl;
    42. return -2;
    43. }
    44. }
    45.  
    46.  
    47. void EncryptFile(const char *in,
    48. const char *out,
    49. const char *passPhrase)
    50. {
    51. FileSource f(in, true, new DefaultEncryptorWithMAC(passPhrase,
    52. new FileSink(out)));
    53. }
    54.  
    55. void DecryptFile(const char *in,
    56. const char *out,
    57. const char *passPhrase)
    58. {
    59. FileSource f(in, true,
    60. new DefaultDecryptorWithMAC(passPhrase, new FileSink(out)));
    61. }
    62.  
    63. RandomPool & GlobalRNG()
    64. {
    65. static RandomPool randomPool;
    66. return randomPool;
    67. }
    68. int (*AdhocTest)(int argc, char *argv[]) = NULL;
    To copy to clipboard, switch view to plain text mode 

    code from: http://www.codeguru.com/cpp/misc/mis...le.php/c11953/

    remember that you should add these lines to its .pro file before starting to build it:
    LIBS += -lcryptopp552
    CONFIG+=console

    --------------------------------

    I appreciate your feedback.
    good luck!

  2. The following 2 users say thank you to FS Lover for this useful post:

    programmer.huang (11th March 2010), sedi (16th January 2018)

Similar Threads

  1. Don't configure under vs command prompt when building gcc (mingw) version of qt
    By piotr.dobrogost in forum Installation and Deployment
    Replies: 4
    Last Post: 1st February 2011, 20:28
  2. Windows minGW g++ version 3.4.5
    By PUK_999 in forum Newbie
    Replies: 4
    Last Post: 21st August 2009, 22:38
  3. Replies: 2
    Last Post: 12th July 2009, 08:24
  4. using the qt 4.4.0 evaluation version with MinGw
    By dano in forum Installation and Deployment
    Replies: 4
    Last Post: 4th July 2008, 16:02
  5. Wich MinGW Version should i use for QT 4.4.0?
    By raphaelf in forum Installation and Deployment
    Replies: 3
    Last Post: 27th May 2008, 14:29

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.