How to Use Crypto ++

Source: Internet
Author: User

 

The Crypto ++ library downloaded from the official website is open-source. Only source files and several projects that can generate lib and dll, as well as a sample project for use, therefore, you want to generate the SDK that can be used in your own project.

 

1. Compile the link to generate cryptlib. lib

Open cryptest. sln, which is used to compile the cryptlib project in Debug and Release modes respectively, after successful execution, cryptlib is generated under cryptopp54 \ Win32 \ output \ debug and cryptopp54 \ Win32 \ output \ release. lib file. The author used Crypto ++ 5.4.

During the Build process, right-click the cryptlib project in Solution Explorer and click build. At the first time, it will report an error saying "d: \ cryptopp54 \ adler32.cpp (3): fatal error C1033: cannot open program database 'd: \ cryptopp54 \ win32 \ cryptlib \ debug \ vc80.idb '", it doesn't matter. You can build again by doing this.

 

2. Create Crypto ++ SDK

In C: \ Program Files \, create a folder named "CryptoPP", and create a folder named "include" and "lib ", create Folders "debug" and "release" in "lib ". Copy all header files in the Crypto ++ Library to the "include" folder, and then copy the two generated cryptlib. lib to "debug" and "release" respectively.

 

Iii. RSA encryption and decryption

 

1. Create a Win32 Console Application project in VS 2005 and create an empty project. Create the file main. cpp. The source code is as follows:

 

# Include \ "randpool. h \"

# Include \ "rsa. h \"

# Include \ "hex. h \"

# Include \ "files. h \"

# Include <iostream>

 

Using namespace std;

Using namespace CryptoPP;

 

# Pragma comment (lib, \ "cryptlib. lib \")

 

//------------------------

// Function declaration

//------------------------

Void GenerateRSAKey (unsigned int keyLength, const char * privFilename, const char * pubFilename, const char * seed );

String RSAEncryptString (const char * pubFilename, const char * seed, const char * message );

String RSADecryptString (const char * privFilename, const char * ciphertext );

RandomPool & GlobalRNG ();

 

//------------------------

// Main program

//------------------------

Void main ()

{

Char priKey [128] = {0 };

Char pubKey [128] = {0 };

Char seed [1024] = {0 };

 

// Generate an RSA key pair

Strcpy (priKey, \ "pri \"); // generated private key file

Strcpy (pubKey, \ "pub \"); // generated public key file

Strcpy (seed, \ "seed \");

Generatersafety key (1024, priKey, pubKey, seed );

 

// RSA encryption and decryption

Char message [1024] = {0 };

Cout <\ "Origin Text: \ t \" <\ "Hello World! \ "<Endl;

Strcpy (message, \ "Hello World! \");

String encryptedText = RSAEncryptString (pubKey, seed, message); // RSA encrypts [Page]

Cout <\ "Encrypted Text: \ t \" <encryptedText <endl;

String decryptedText = RSADecryptString (priKey, encryptedText. c_str (); // RSA decryption

Cout <\ "Decrypted Text: \ t \" <decryptedText <endl;

}

 

//------------------------

// Generate an RSA key pair

//------------------------

Void GenerateRSAKey (unsigned int keyLength, const char * privFilename, const char * pubFilename, const char * seed)

{

RandomPool randPool;

RandPool. Put (byte *) seed, strlen (seed ));

 

RSAES_OAEP_SHA_Decryptor priv (randPool, keyLength );

HexEncoder privFile (new FileSink (privFilename ));

Priv. DEREncode (privFile );

PrivFile. MessageEnd ();

 

RSAES_OAEP_SHA_Encryptor pub (priv );

HexEncoder pubFile (new FileSink (pubFilename ));

Pub. DEREncode (pubFile );

PubFile. MessageEnd ();

}

 

//------------------------

// RSA Encryption

//------------------------

String RSAEncryptString (const char * pubFilename, const char * seed, const char * message)

{

FileSource pubFile (pubFilename, true, new HexDecoder );

RSAES_OAEP_SHA_Encryptor pub (pubFile );

 

RandomPool randPool;

RandPool. Put (byte *) seed, strlen (seed ));

 

String result;

StringSource (message, true, new PK_EncryptorFilter (randPool, pub, new HexEncoder (new StringSink (result ))));

Return result;

}

 

//------------------------

// RSA decryption

//------------------------

String RSADecryptString (const char * privFilename, const char * ciphertext)

{

FileSource privFile (privFilename, true, new HexDecoder );

 

RSAES_OAEP_SHA_Decryptor priv (privFile );

 

String result;

StringSource (ciphertext, true, new HexDecoder (new PK_DecryptorFilter (GlobalRNG (), priv, new StringSink (result ))));

Return result; [Page]

}

 

//------------------------

// Define a global random number pool

//------------------------

RandomPool & GlobalRNG ()

{

Static RandomPool randomPool;

Return randomPool;

}

 

2. Set Project Properties

Select Project Properties (Alt + F7 ):

(1) "Configuration Properties" → "C/C ++" → "General", "Additional Include Directories" on the right is set to the Include folder of the Crypto ++ SDK created above, "C: \ Program Files \ polictopp \ include ";

(2) "Configuration Properties" → "Linker" → "General". The "Additional Library Directories" on the right is set to the Lib \ Debug folder of the Crypto ++ SDK, "C: \ Program Files \ polictopp \ lib \ debug" (corresponding to the Release folder in Release mode );

(3) "Configuration Properties" → "C/C ++" → "Code Generation", and "Runtime Library" on the right is set to "Multi-threaded Debug (/MTd) "(" Multi-threaded (/MT) "in the Release Mode )")

 

3. Run the program (Ctrl + F5)

The output result is as follows:

 

 

Origin Text: Hello World!

 

Encrypted Text: 79c72a482482ef45111f961772456310492ab735ecf72329ecb26292d2b26374

Bytes

Bytes

84D9F6FF9BA690F953568D017C02D540

 

Decrypted Text: Hello World!

 

 

If step (3) is not set, the following link error occurs:

Cryptlib. lib (randpool. obj): error LNK2005: \ "public: _ thiscall std: basic_string <char, struct std: char_traits <char>, class std: allocator <char> :: basic_string <char, struct std: char_traits <char>, class std: allocator <char> (char const *)\"(?? 0? $ Basic_string @ DU? $ Char_traits @ D @ std @ V? $ Allocator @ D @ 2 @ std @ QAE @ PBD @ Z) already defined in msvcprtd. lib (MSVCP80D. dll)

It is defined in msvcprtd. lib and MSVCRTD. lib.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.