(The most detailed strategy on the whole network) [Crypto++] Run Cryptopp in Visual studio2022

Article directory

  • foreword
  • 1. What is Cryptopp?
    • 1. Cryptopp (CRYPTO++) official documentation wiki
  • 2. Download Cryptopp
    • 2. Crypto++ download address
    • 3. Download the PEM package
  • 3. Use the Cryptopp library in VS2022
    • 4. Processing crypto++ source files
    • 5. Using crypto++ library in VS2022 project
  • 4. Summary of some errors about c ++ after running the code

Foreword

  1. What is Cryptopp?
  2. How to download Cryptopp?
  3. How to use Crypto++ in VS?

1. What is Cryptopp?

Free C++ library for cryptographic schemes, originally written by Wei Dai, including ciphers, message authentication codes, one-way hash functions, public-key cryptography, key agreement schemes, and zip compression.
PEM package: A partial implementation of message encryption that allows you to read and write PEM-encoded keys and parameters, including encrypted private keys. Additional documentation includes support for RSA, DSA, EC, ECDSA keys and Diffie-Hellman parameters. The package includes five additional source files, a script to create test keys using OpenSSL, a C++ program to test reading and writing keys, and a Crypto++ program to verify script for the key.

1. Cryptopp (CRYPTO++) official document wiki

https://cryptopp.com/wiki/Main_Page

2. Download Cryptopp

2. Crypto++ download address

  • Official website address: https://www.cryptopp.com/#download
  • github address (version 8.7.0): https://github.com/weidai11/cryptopp/releases/tag/CRYPTOPP_8_7_0
    There are four options, please download the source code (as shown in the picture)

3. Download PEM package

  • The official address of the pem package: https://cryptopp.com/wiki/PEM_Pack
    Slide the page to the bottom and download (as shown in the picture)

3. Using Cryptopp library in VS2022

4. Processing crypto++ source files

  1. Open the downloaded and decompressed crypto++ file and pem package, copy and paste all the files contained in the pem package into the crypto++ package:

Figure 1 is the decompressed pem package

Figure 2 shows all the files included in the pem package

  1. Paste all the files in Figure 2 into the decompressed crypto++ file shown in Figure 3, and double-click to open cryptest.sln in the crypto++ file:
  2. Four sub-projects can be seen in VS2022 (as shown in the figure):
  • cryptdll – generate cryptopp.dll dynamic library
  • dlltest – used to test cryptopp.dll, depends on cryptdll project
  • cryptlib – generate cryptlib.lib static library
  • cryptest – used to test cryptopp, depends on cryptlib project
  1. Add the pem package to the crypto++ source file:

Right click “Header Files” -> Add -> Existing Item:

  • pem.h
  • pem_common.h

Right click on “Source Files” -> Add -> Existing Item:

  • pem_common.cpp
  • pem_read.cpp
  • pem_write.cpp


5. Build subproject cryptlib to generate lib file
In the two cases of Release and debug respectively, adjust the device model (this machine is x64), right-click the sub-project cryptlib and click “Generate”. Once the output shows success, it is complete.

At this point, there will be an additional folder named x64 under the crypto++ source folder. There are three subfolders under this folder, one of which is named Output, and there are two subfolders under the Output folder, as shown in the figure . There are lib files under the Debug and Release folders. These two lib files are the lib libraries we will use later.

  1. Process crypto++ files:
    Create a new folder and name it cryptopp (or other names), and create two folders under the folder, named include and lib respectively.
  • Copy all header files (files ending in .h) in the crypto++ source file to the include folder under the new folder.

  • Copy and paste the above Output folder into the lib file under the newly created folder.

    At this point, we need to use the crypto++ library to finish. In short, for the entire crypto++ source code downloaded from the official website, we only need to use the two library files include and lib.

5. Using crypto++ library in VS2022 project

  1. Open VS2022, File -> New -> Project -> Console Application

  2. Replace the original hello world code in the newly created project with the following code (the code is the AES encryption code given by crypto++ official website) as the test code:

#include "cryptlib.h"
#include "rijndael.h"
#include "modes.h"
#include "files.h"
#include "osrng.h"
#include "hex.h"

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{<!-- -->
    using namespace CryptoPP;

    AutoSeededRandomPool prng;
    HexEncoder encoder(new FileSink(std::cout));

    SecByteBlock key(AES::DEFAULT_KEYLENGTH);
    SecByteBlock iv(AES::BLOCKSIZE);

    prng.GenerateBlock(key, key.size());
    prng. GenerateBlock(iv, iv. size());

    std::string plain = "CBC Mode Test: Hello!";
    std::string cipher, recovered;

    std::cout << "plain text: " << plain << std::endl;

    /**********************************\
    \************************************/

    try
    {<!-- -->
        CBC_Mode<AES>::Encryption e;
        e.SetKeyWithIV(key, key.size(), iv);

        StringSource s(plain, true,
            new StreamTransformationFilter(e,
                new StringSink(cipher)
            ) // StreamTransformationFilter
        ); // StringSource
    }
    catch (const Exception & e)
    {<!-- -->
        std::cerr << e.what() << std::endl;
        exit(1);
    }

    /**********************************\
    \************************************/

    std::cout << "key: ";
    encoder. Put(key, key. size());
    encoder. MessageEnd();
    std::cout << std::endl;

    std::cout << "iv: ";
    encoder. Put(iv, iv. size());
    encoder. MessageEnd();
    std::cout << std::endl;

    std::cout << "cipher text: ";
    encoder.Put((const byte*) &cipher[0], cipher.size());
    encoder. MessageEnd();
    std::cout << std::endl;

    /**********************************\
    \************************************/

    try
    {<!-- -->
        CBC_Mode<AES>::Decryption d;
        d. SetKeyWithIV(key, key. size(), iv);

        StringSource s(cipher, true,
            new StreamTransformationFilter(d,
                new StringSink(recovered)
            ) // StreamTransformationFilter
        ); // StringSource

        std::cout << "recovered text: " << recovered << std::endl;
    }
    catch (const Exception & e)
    {<!-- -->
        std::cerr << e.what() << std::endl;
        exit(1);
    }

    return 0;
}
  1. Project -> Properties
    Follow the image to modify options:
    a. Change here to the include file path under our newly created folder.

    b. Pay attention to whether your current mode is Release or Debug. The runtime corresponding to Release is /MT, and the corresponding to Debug is /MTd.

    c. Change here to the path under the lib file under our newly created folder. Check your own configuration, Debug corresponds to the path under the Debug file; Release corresponds to the path under the Release file.

    d. Select “Input” in “Linker” and add “cryptlib.lib” in “Additional Dependencies”

  2. Run the test code, if the result picture appears, it means that the configuration of crypto ++ is successful.

4. Summary of some errors about c++ after running the code

  1. The solution to how to run multiple source files separately in VS
    Reference blog post: https://blog.csdn.net/m0_62638970/article/details/121794983
  2. The usage details of setw() and setfill() in c++: https://blog.csdn.net/chen_zan_yu_/article/details/86663579
  3. C++ – “std” has no member “string”: https://blog.csdn.net/cedian0443/article/details/104930355

Finally, since the official documentation wiki of crypto++ is not comprehensive, I would like to thank the following blog posts for their contributions:

  • Crypto++ installation and calling under VS2019
  • Installation and use of Crypto++
  • Crypto++ installation and simple use of RSA encryption and decryption
  • Video teaching (youtube): https://www.youtube.com/watch?v=5XE4zEN-WKg