C++ 文档加密与解密运用【Crypto++】库

一、下载Cryptopp

鼠标放到下面网址,点击下载即可

github地址(8.7.0版本):https://github.com/weidai11/cryptopp/releases/tag/CRYPTOPP_8_7_0

二 、下载PEM包

pem包官方地址:PEM Pack - Crypto++ Wiki

三、调用Crypto++库

1、解压crypto++包和pem包,将pem文件夹中包含的全部文件复制粘贴到crypto++文件夹中

2、双击打开crypto++文件夹中的cryptest.sln工程

3、在cryptlib子工程里面添加头文件

  • pem.h
  • pem_common.h

 4、在cryptlib子工程源文件中添加

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

5、build子工程cryptlib,生成lib文件,根据需要生成debug和release版,lib文件在x64->output里面的debug和release文件夹中

 新建文件夹include,将crypto++源文件中所有头文件(.h结尾的文件)复制到include文件夹内,供后面vs工程调用。

四、VS调用crypto++库

1、打开VS(我的是2019版本),文件->新建->项目->空项目

把下面的代码复制进去

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


2、修改项目属性

(1)C/C++——常规——附加包含目录:选择我们刚刚新建的include文件夹路径

(2)链接器——常规——附加库目录:选择cryptlib.lib所在的路径

(3)连接器——输入——附加依赖项:添加cryptlib.lib

3、点击编译运行,出现以下结果则成功

五、加密&解密实例

#include <iostream>
#include "randpool.h"
#include "rsa.h"
#include "hex.h"
#include "files.h"
#include "config.h"
#include "stdcpp.h"
#include "modes.h"
#include"base64.h"

using namespace std;
using namespace CryptoPP;

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

// 加密

string encrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CFB_Mode<CryptoPP::AES>::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());

CryptoPP::StringSource encryptor(str_in, true,
new CryptoPP::StreamTransformationFilter(encryption,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(str_out),
false // do not append a newline
)
)
);
return str_out;
}

//解密
string decrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CFB_Mode<CryptoPP::AES>::Decryption decryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());

CryptoPP::StringSource decryptor(str_in, true,
new CryptoPP::Base64Decoder(
new CryptoPP::StreamTransformationFilter(decryption,
new CryptoPP::StringSink(str_out)
)
)
);
return str_out;
}

// Main函数

void main()
{
std::string str = "123456789012345";  //需要加密的字符串
std::string key = "01234567891234560123456789123456"; // 32 bytes 密钥
std::string iv = "0123456789123456"; // 16 bytes
std::string str_encrypted =  encrypt(str, key, iv);   //加密后密文

std::string str_decrypted = decrypt(str_encrypted, key, iv);  //解密后明文
std::cout << "str_encrypted: " << str_encrypted << std::endl;
std::cout << "str_decrypted: " << str_decrypted << std::endl;
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值