Crypto++的安装及使用

Crypto++是一套关于应用密码学的开源类库,提供了散列(MD5、SHA)、数据加密(DES、AES)、数字签名(RSA、椭圆曲线签名算法ECDSA)等很多有用的算法,算法安全性已经通过 FIPS 140-2(http://csrc.nist.gov/cryptval/140-2.htm) 验证。 Crypto++库包含有大量的算法

1、  分组密码:DES-EDE3, Blowfish, Rijndael

2、  序列密码:

3、  Hash函数:SHA1

4、  消息认证码: HMAC/SHA1

5、  公钥加密:RSA/OAEP/SHA1

6、  签名:RSA/PKCS1v15/SHA1, DSA,Generalized-DSA/SHA1

7、  密钥协议:DH

8、  随机数产生器:RandomPool,AutoSeededRandomPool

Crypto++安装:

Crypto++官方下载:点击下载

下载下来直接解压缩,然后在VS2015或别的VS版本中打开解决方案cryptest.sln,打开后有4个项目,打开cryptlib这个项目的cryptlib.cpp然后在Debug模式和Release模式下分别编译。

编译的时间比较长,完成后会生成cryptlib.lib这个库文件。将下载后解压的Crypto++命名为cryptopp,拷贝到编译器的include目录(例如:F:\VS2015pro\VC\include),将cryptlib.lib文件拷贝到编译器的lib目录。这样我们只需要说明链接cryptlib.lib即可。例如在VS2015中在项目->属性->链接器->命令行->附加选项中添加“cryptlib.lib”。

测试一下是否成功,新建一个项目,把下面代码复制,然后成功运行说明配置成功。

#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;
} 

运行结果如图:

建立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万+

猜你喜欢

转载自blog.csdn.net/qq_32261191/article/details/78855651