Crypto++ AES加密和解密

安装

  1. 源码下载 https://cryptopp.com/#download
  2. 用visual studio打开项目
  3. cryptlib项目属性中配置Debug/x64, C/C++ > 代码生成中 配置运行库为MDd, 要保持和引用项目一致
  4. 生成
  5. 目录x64\Output\Debug\cryptlib.lib下就是对应的库了

使用

  1. visual studio创建一个简单项目, 运行库同样MDd
  2. 配置项目属性 C/C++ > 常规 > 附加包含目录 , 填cryptopp的源码路径
  3. 链接器 > 输入 > 附加依赖项 填库文件全路径

示例

#include <iostream>
#include <string>
#include <aes.h> 
#include <filters.h> 
#include <modes.h>
#include <hex.h>

using namespace std;
using namespace CryptoPP;

int main()
{
    string plaintext = "Hello, Crypto++";
    string key = "1234567890123456";

    cout << "明文:" << plaintext << endl;

    byte iv[AES::BLOCKSIZE];

    memset(iv, '0', AES::BLOCKSIZE);
    CBC_Mode<AES>::Encryption encryption((byte*)key.c_str(), key.length(), iv);
    string ciphertext;
    StringSource(plaintext, true, new StreamTransformationFilter(encryption, new CryptoPP::HexEncoder(new CryptoPP::StringSink(ciphertext), false)));

    cout << "密文:" << ciphertext << endl;


    std::string hexDecodedCipherText;
    CryptoPP::StringSource(ciphertext, true,
        new CryptoPP::HexDecoder(
            new CryptoPP::StringSink(hexDecodedCipherText)
        )
    );

    CBC_Mode<AES>::Decryption decryption((byte *)key.c_str(), key.length(), iv);
    string decryptedtext;
    StringSource(hexDecodedCipherText, true, new StreamTransformationFilter(decryption, new StringSink(decryptedtext)));


    cout << "解密后的明文:" << decryptedtext << endl;

    return 0;
}

输出

明文:Hello, Crypto++
密文:5870292e8773d7d61055e4062e41c5a6
解密后的明文:Hello, Crypto++
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值