Installation and use of Crypto ++

Crypto ++ is a set of open source on the application of cryptography library provides hash (MD5, SHA), data encryption (DES, AES), a digital signature (RSA, elliptic curve signature algorithm ECDSA), and many other useful algorithms, algorithm security It was tested through FIPS 140-2 (http://csrc.nist.gov/cryptval/140-2.htm) authentication. Crypto ++ library contains a large number of algorithms

1, a block cipher: DES-EDE3, Blowfish, Rijndael

2, stream cipher:

3, Hash functions: SHA1

4, a message authentication code: HMAC / SHA1

5, public-key encryption: RSA / OAEP / SHA1

6, Signature: RSA / PKCS1v15 / SHA1, DSA, Generalized-DSA / SHA1

7, key agreement: DH

8, a random number generator: RandomPool, AutoSeededRandomPool

Crypto ++ installation:

Crypto ++ official Download: Download

Unzip downloaded directly, and then open the solution in cryptest.sln or other VS VS2015 version, there are four open projects, open cryptlib.cpp cryptlib this project and are compiled in Debug mode and Release mode.

Compile a long time, after the completion of the library file is generated cryptlib.lib. After download, unzip the Crypto ++ named cryptopp, to include a copy of the directory compiler (eg: F: \ VS2015pro \ VC \ include), copy the file to the lib directory cryptlib.lib compiler. We just need to explain to link cryptlib.lib. For example, in VS2015 in the project -> Properties -> Linker -> Command Line -> Additional options add "cryptlib.lib".

Test is successful, create a new project, copy the code below, then successfully run the configuration was successful.

 

#include <iostream>
using namespace std;
#include <stdlib.h>
#include <cryptopp/aes.h>
using namespace CryptoPP;

int main() 
{
       cout << "hello crypto++" << endl;
       cout << "Aes block size is " << AES::BLOCKSIZE << endl;
       system("pause");
       return 0;
} 

Run results shown in Figure:

 

 

Establish Crypto ++ SDK

在C:\ProgramFiles\中新建文件夹,取名“CryptoPP”,里面新建文件夹“include”、“lib”,在“lib”中新建文件夹“Debug”、“Release”。将Crypto++库中的所有头文件复制到“include”文件夹中,再将上面生成的两个cryptlib.lib分别复制到“Debug”和“Release”中。

 

设置工程属性

选择工程属性(Alt +F7):

(1)“配置属性”→“C/C++” →“常规”,右边的“附加包含目录”设置为上面建好的Crypto++ SDK的Include文件夹,“C:\Program Files\CyptoPP\include”;

 

(2) 配置属性”→“链接器” →“常规”,右边的“附加库目录”设置为上面建好的Crypto++ SDK的Lib\Debug文件夹,“C:\Program Files\CyptoPP\lib\debug”(Release模式下对应着Release文件夹);

 

(3) “配置属性”→“C/C++” →“代码生成”,右边的“运行库”设置为“多线程调试(/MTd)”(Release模式下对应着“Multi-threaded (/MT)”)

 

现在可以使用Crypto++的所有东西了。

 

例如RSA算法:

 

#include "randpool.h"

#include "rsa.h"

#include "hex.h"

#include "files.h"

#include <iostream>

#include <stdlib.h>

using namespace std;

using namespace CryptoPP;


#pragma comment(lib, "cryptlib.lib")


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

// 函数声明

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

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();


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

// 主程序

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

void main()

{

    char priKey[128] = { 0 };

    char pubKey[128] = { 0 };

    char seed[1024] = { 0 };


    // 生成 RSA 密钥对

    strcpy(priKey, "pri"); // 生成的私钥文件

    strcpy(pubKey, "pub"); // 生成的公钥文件

    strcpy(seed, "seed");

    GenerateRSAKey(1024, priKey, pubKey, seed);


    // RSA 加解密

    char message[1024] = { 0 };

    cout << "Origin Text:\t" << "Hello World!" << endl << endl;

    strcpy(message, "Hello World!");

    string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密

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

    string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA 解密

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

    system("pause");
}


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

// 生成RSA密钥对

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

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加密

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

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解密

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

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;

}


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

// 定义全局的随机数池

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

RandomPool & GlobalRNG()

{

    static RandomPool randomPool;

    return randomPool;

}

 

 

 

发布了31 篇原创文章 · 获赞 55 · 访问量 28万+

Guess you like

Origin blog.csdn.net/qq_32261191/article/details/78855651