• 0

Need help getting Crypto++ usable


Question

I just started using C++ and I am trying to use the Crypto++ library (I heard it was really good)

 

Having to compile libraries to get them to work (and anything related) it still foreign to me.

 

No matter what I do I keep getting linking errors when I try to build my project. I have tried very many combinations so I can't really remember what I did exactly. I tried following instructions from some links below and I also tried copying the crypto++ files into my source folder.

 

I have two main questions

 

1) What do I need to do, to run this code?

http://www.cryptopp.com/w/images/b/bd/AES-CBC-Filter.zip

 

2) When working with libraries, when do they need to be compiled before I can use them?

 

This is where I downloaded Crypto++ 5.6.2 from

http://www.cryptopp.com/#download

 

I was getting some information and examples from these links too

http://programmingknowledgeblog.blogspot.de/2013/04/compiling-and-integrating-crypto-into.html

http://stackoverflow.com/questions/12306956/example-of-aes-using-crypto

 

I'm using VS Express

 

Thanks :)

Link to comment
Share on other sites

9 answers to this question

Recommended Posts

  • 0

I just started using C++ and I am trying to use the Crypto++ library (I heard it was really good)

 

Having to compile libraries to get them to work (and anything related) it still foreign to me.

 

No matter what I do I keep getting linking errors when I try to build my project. I have tried very many combinations so I can't really remember what I did exactly. I tried following instructions from some links below and I also tried copying the crypto++ files into my source folder.

 

I have two main questions

 

1) What do I need to do, to run this code?

http://www.cryptopp.com/w/images/b/bd/AES-CBC-Filter.zip

 

2) When working with libraries, when do they need to be compiled before I can use them?

 

This is where I downloaded Crypto++ 5.6.2 from

http://www.cryptopp.com/#download

 

I was getting some information and examples from these links too

http://programmingknowledgeblog.blogspot.de/2013/04/compiling-and-integrating-crypto-into.html

http://stackoverflow.com/questions/12306956/example-of-aes-using-crypto

 

I'm using VS Express

 

Thanks smile.png

 

A library, (if you have the cpp files should it have any), could be used in an application by simply adding its files into the VS project and using its headers as any other header file and thus directly compiling it as part of the application. Alternatively it could be compiled as a separate project into a static or dynamic library file (.lib / .dll respectively).

 

If you were developing your own static/dynamic library/libraries as part of a larger project, you might have one VS solution containing one or more application projects, and one or more library projects. Each library project would be set to compile as dll or static in their project settings. For each library an application project uses, you would have to add a references to the library project (similarly if one library used another, a reference would be needed), which enables the functionality in the compiled library to be used in the application. Additionally, the application code referencing header files from a library needs to be able to actually find those header files in the file system. For this, you could use the full path or a messy relative path, to point to the header files within the library project directory, however its best to add the path to your application project's 'additional include directories' property, which then allows you to neatly and cleanly include them.

 

http://msdn.microsoft.com/en-us/library/ms235627.aspx

http://msdn.microsoft.com/en-us/library/ms235636.aspx

 

The Crypto++ zip file contains its own set of VS solution/project files. Opening the solution file (cryptest.sln) in VS you can see that the solution consists of four projects. Two of these appear to be for testing purposes. One is a copy of the library configured to be compiled as a static library (cryptlib), and one is a copy of the library configured to be compiled as a dynamic library (cryptopp).

 

Assuming you'd like to use it as a static/dynamic library rather than more hacksihly trying to incorporate the code directly into your application project)...

 

To use this library in your application, you need to achieve the following:

  1. Get the library compiled (your choice as to whether you use the static or dynamic version).
  2. Make the compiled library .lib/.dll file available to be used.
  3. Make the header files available so you can include them with neat clean include references.

The last item here is simple, just add the directory path to the application project's 'additional include directories' property.

 

For the latter two you have a choice. Either you could try to add the cryplib or cryptopp project into the solution in which you're developing your application, then proceed as if this were a library you were developing yourself (add the project reference and then just compile), or alternatively, compile the library separately (from within the VS solution provided in Crypto++), then make the compiled library file available to your application. The latter is the more preferable option, since you very rarely should need to compile this library, so it makes little sense to add it into your application solution.

 

So, going with the latter option (compile separately and then make available to the application project); Simply compile the library, then open your application's VS solution and in the project properties, go to 'configuration properties' > Linker > Input, and add the file under 'additional dependencies' (on the drop down select 'edit', then add the filename on a new line in the text box). For it to find the file, either you need to copy and paste a copy into the directory in which your compiled application exe will be saved to (which I think makes the next step redundant), or I think you need to add the path into 'additional library directories' under Linker > General.

 

Compile the library before compiling the application.

 

There is actually a readme.txt file in the Crypto++ zip file (under cryptlib > miscellaneous when opened in VS) which states the following:

 

On Windows, Crypto++ can be compiled into 3 forms: a static library including all algorithms, a DLL with only FIPS Approved algorithms, and a static library with only algorithms not in the DLL. (FIPS Approved means Approved according to the FIPS 140-2 standard.) The DLL may be used by itself, or it may be used together with the second form of the static library. MSVC project files are included to build all three forms, and sample applications using each of the three forms are also included.

To compile Crypto++ with MSVC, open the "cryptest.dsw" (for MSVC 6 and MSVC .NET 2003) or "cryptest.sln" (for MSVC 2005 - 2010) workspace file and build one or more of the following projects:

  • cryptopp - This builds the DLL. Please note that if you wish to use Crypto++   as a FIPS validated module, you must use a pre-built DLL that has undergone   the FIPS validation process instead of building your own.
  • dlltest - This builds a sample application that only uses the DLL.
  • cryptest Non-DLL-Import Configuration - This builds the full static library   along with a full test driver.
  • cryptest DLL-Import Configuration - This builds a static library containing   only algorithms not in the DLL, along with a full test driver that uses   both the DLL and the static library.

To use the Crypto++ DLL in your application, #include "dll.h" before including any other Crypto++ header files, and place the DLL in the same directory as your .exe file. dll.h includes the line #pragma comment(lib, "cryptopp") so you don't have to explicitly list the import library in your project settings. To use a static library form of Crypto++, make the "cryptlib" project a dependency of your application project, or specify it as an additional library to link with in your project settings. In either case you should check the compiler options to make sure that the library and your application are using the same C++ run-time libraries and calling conventions.

 

So to compile the library/libraries, open the Crypto++ solution, then depending on what you want:

  • For the dll: Select the cryptopp project, then compile as release/debug.
  • For the full static library (alternate to dll, includes the non-fips functionality the dll does not have): Select the cryptlib project, then compile as release/debug.
  • For the supplementary static library (used in addition to dll, providing the additional non-fips functionality): Select the cryptlib project, then compile with 'DLL-Import Release'/DLL-Import Debug' (instead of release/debug in the drop down).
Link to comment
Share on other sites

  • 0

If I was you, I would just use a single class to do AES encryption, that library seems to be overkill unless you're planning to use a bunch of classes from it. Anyway, to use that Crypto++ library just download and unzip it, open the Visual Studio solution, right click on the solution, select Build. Then open your sample project, go to project settings, under C++, add the Crypto++ library directory to the Additional Include directories, and the output folder (the one containing the .lib file) to Linker/Additional library directories. Also add "cryptopp.lib" (i don't know what's the name exactly) to Additional dependencies. Then you should be able to compile the sample. If it doesn't, also check if you're using the same C standard lib (Static or DLL), and if you're using different character sets (Unicode or otherwise) between the static lib and the sample. Those 2 settings always cause the most problems.

 

Ask if you have more questions, I'd gladly help. :)

Link to comment
Share on other sites

  • 0

1) What is the problem with running that code?

 

2) Usually long as the project contains any .cpp files, it will need to be compiled separately.

Link to comment
Share on other sites

  • 0

Having to compile libraries to get them to work (and anything related) it still foreign to me.

2) When working with libraries, when do they need to be compiled before I can use them?

This is where I downloaded Crypto++ 5.6.2 from

http://www.cryptopp.com/#download

It looks like you can download a prebuilt release version of that library. So my guess is, you won't need to compile it. The only condition for using those prebuilds is:

Crypto++ Library 5.3.0 (32-bit and 64-bit Windows DLL, calling application must be compiled with MSVC 2005)

No matter what I do I keep getting linking errors when I try to build my project. I have tried very many combinations so I can't really remember what I did exactly. I tried following instructions from some links below and I also tried copying the crypto++ files into my source folder.

 

1) What do I need to do, to run this code?

http://www.cryptopp.com/w/images/b/bd/AES-CBC-Filter.zip

You need to specify the include directory (where the header files for crypto++ are located), and link to the actual compiled crypto++ dll. This is noted at the top of the Driver.cpp file. On Linux, it's simply a matter of passing '-I/usr/include/cryptopp Driver.cpp -lcryptopp' to the gcc. I would imagine that you can do the same somewhere in VS, perhaps in the build settings for the project.
Link to comment
Share on other sites

  • 0

 

A library, (if you have the cpp files should it have any), could be used in an application by simply adding its files into the VS project and using its headers as any other header file and thus directly compiling it as part of the application. Alternatively it could be compiled as a separate project into a static or dynamic library file (.lib / .dll respectively).

 

.....

  • For the dll: Select the cryptopp project, then compile as release/debug.
  • For the full static library (alternate to dll, includes the non-fips functionality the dll does not have): Select the cryptlib project, then compile as release/debug.
  • For the supplementary static library (used in addition to dll, providing the additional non-fips functionality): Select the cryptlib project, then compile with 'DLL-Import Release'/DLL-Import Debug' (instead of release/debug in the drop down).

 

 

 

If I was you, I would just use a single class to do AES encryption, that library seems to be overkill unless you're planning to use a bunch of classes from it. Anyway, to use that Crypto++ library just download and unzip it, open the Visual Studio solution, right click on the solution, select Build. Then open your sample project, go to project settings, under C++, add the Crypto++ library directory to the Additional Include directories, and the output folder (the one containing the .lib file) to Linker/Additional library directories. Also add "cryptopp.lib" (i don't know what's the name exactly) to Additional dependencies. Then you should be able to compile the sample. If it doesn't, also check if you're using the same C standard lib (Static or DLL), and if you're using different character sets (Unicode or otherwise) between the static lib and the sample. Those 2 settings always cause the most problems.

 

Ask if you have more questions, I'd gladly help. :)

 

Hey guys, I tried following your guides but it still isn't working. I am getting a lot of errors. Here is an imgur album showing some errors, plus my settings

http://imgur.com/a/eyTqh

*I compiled the crypto++ library separately btw

 

EDIT: My runtime library is Multi-threaded /MT

Link to comment
Share on other sites

  • 0

Under Linker > input > additional dependencies, I believe you should / only-need-to supply the file name, not the path. The point of adding the path to the compiled library in Linker > general > additional library directories was so that it could find the file.

 

I'll grab a copy and try it myself...

Link to comment
Share on other sites

  • 0

Ok, after stupidly trying to use cryptopp.lib from the DLL_Output directory instead of cryptlib.lib from the Output directory, leading to linker errors that led me down completely the wrong path - I'm way too tired for this right now and was on the verge of giving up until I noticed the problem - I'm now getting the errors you're seeing, and I have a solution:

 

http://stackoverflow.com/questions/14714877/crypto-mismatch-detected-for-runtimelibrary

 

So make sure 'configuration properties' > C/C++ > Code Generation > Runtime Library is set to 'Multithreaded Debug' for the debug build of your application instead of 'Multithreaded Debug DLL', and similarly 'Multithreaded Release' for the release build.

Link to comment
Share on other sites

  • 0

Ok, after stupidly trying to use cryptopp.lib from the DLL_Output directory instead of cryptlib.lib from the Output directory, leading to linker errors that led me down completely the wrong path - I'm way too tired for this right now and was on the verge of giving up until I noticed the problem - I'm now getting the errors you're seeing, and I have a solution:

 

http://stackoverflow.com/questions/14714877/crypto-mismatch-detected-for-runtimelibrary

 

So make sure 'configuration properties' > C/C++ > Code Generation > Runtime Library is set to 'Multithreaded Debug' for the debug build of your application instead of 'Multithreaded Debug DLL', and similarly 'Multithreaded Release' for the release build.

Perfect! It works now :)

 

Thanks so much everyone!

Link to comment
Share on other sites

  • 0

Perfect! It works now :)

 

Thanks so much everyone!

This goes back to our previous discussions about portability. As you can see you don't always get to choose whether you use static or dynamic linking, it depends on the libs you work with (unless you can recompile these with the settings you want).

Link to comment
Share on other sites

This topic is now closed to further replies.