C/C++ 使用 cryptopp 加密解密

Crypto++(也称为CryptoPP)是一个流行的开源密码学库,提供了各种密码学算法的实现。它是用C++编写的,并在许可证允许的范围内可自由使用和修改。

Crypto++支持多种密码学算法,包括对称加密算法(如AES、DES、RC4)、哈希函数(如SHA-1、SHA-256)、消息认证码(如HMAC)、公钥加密算法(如RSA、DSA、ECC)以及其他密码学功能。

使用Crypto++,您可以在自己的C++应用程序中轻松地集成密码学功能。它提供了面向对象的接口和易于使用的API,使得实现加密和解密操作、生成和验证数字签名、计算哈希值等任务变得相对简单。

官网:Crypto++: Crypto++ Library 8.6 API Reference

github:GitHub - LYingSiMon/cryptopp: free C++ class library of cryptographic schemes

文档:Crypto++: Crypto++ Library 8.6 API Reference

环境搭建
引入 cryptlib.lib , 以及所有项目中的头文件

AES 加密测试(ECB 模式为例)

#include <iostream>
 
#include "cryptlib.h"
#include "rijndael.h"
#include "modes.h"
#include "files.h"
#include "osrng.h"
#include "hex.h"
#include "base64.h"
 
using namespace CryptoPP;
 
// aes ebc 加密(输出 base64)
std::string aes_encrypt_ecb_base64(std::string data , unsigned char* key, int keylen)
{
    std::string encrypt_str;
 
    try 
    {
        CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption ecb_encription(key, keylen);
        CryptoPP::StreamTransformationFilter stf_encription(
            ecb_encription,
            new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encrypt_str)),
            CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
        );
        stf_encription.Put(reinterpret_cast<const unsigned char*>(data.c_str()), data.length() + 1);
        stf_encription.MessageEnd();
    }
    catch (std::exception e) {
        std::cout << e.what() << std::endl;
    }
 
    return encrypt_str;
}
 
// aes ebc 加密(输出 hex) 
std::string aes_encrypt_ecb_hex(std::string data , unsigned char* key, int keylen)
{
    std::string encrypt_str;
 
    try 
    {
        CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption ecb_encription(key, keylen);
        CryptoPP::StreamTransformationFilter stf_encription(
            ecb_encription,
            new CryptoPP::HexEncoder(new CryptoPP::StringSink(encrypt_str)),
            CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
        );
        stf_encription.Put(reinterpret_cast<const unsigned char*>(data.c_str()), data.length() + 1);
        stf_encription.MessageEnd();
    }
    catch (std::exception e) {
        std::cout << e.what() << std::endl;
    }
 
    return encrypt_str;
}
 
// aes ebc 解密(输出 base64)
std::string aes_decrypt_ecb_base64(std::string base64_data, unsigned char* key, int keylen)
{
    try 
    {
        std::string aes_encrypt_data;
        CryptoPP::Base64Decoder decoder;
        decoder.Attach(new CryptoPP::StringSink(aes_encrypt_data));
        decoder.Put(reinterpret_cast<const unsigned char*>(base64_data.c_str()), base64_data.length());
        decoder.MessageEnd();
 
        std::string decrypt_data;
        CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption ebc_description(key, keylen);
        CryptoPP::StreamTransformationFilter stf_description(
            ebc_description,
            new CryptoPP::StringSink(decrypt_data), 
            CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
        );
 
        stf_description.Put(
            reinterpret_cast<const unsigned char*>(aes_encrypt_data.c_str()), 
            aes_encrypt_data.length()
        );
        stf_description.MessageEnd();
 
        return decrypt_data;
    }
    catch (std::exception e) {
        std::cout << e.what() << std::endl;
        return "";
    }
}
 
// aes ebc 解密(输出 hex)
std::string aes_decrypt_ecb_hex(std::string hex_data, unsigned char* key, int keylen)
{
    try
    {
        std::string aes_encrypt_data;
        CryptoPP::HexDecoder decoder;
        decoder.Attach(new CryptoPP::StringSink(aes_encrypt_data));
        decoder.Put(reinterpret_cast<const unsigned char*>(hex_data.c_str()), hex_data.length());
        decoder.MessageEnd();
 
        std::string decrypt_data;
        CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption ebc_description(key, keylen);
        CryptoPP::StreamTransformationFilter stf_description(
            ebc_description,
            new CryptoPP::StringSink(decrypt_data),
            CryptoPP::BlockPaddingSchemeDef::ZEROS_PADDING
        );
 
        stf_description.Put(
            reinterpret_cast<const unsigned char*>(aes_encrypt_data.c_str()),
            aes_encrypt_data.length()
        );
        stf_description.MessageEnd();
 
        return decrypt_data;
    }
    catch (std::exception e) {
        std::cout << e.what() << std::endl;
        return "";
    }
}
 
 
int main()
{
    // ebc base64 
    std::string en_base64 = aes_encrypt_ecb_base64("hello cryptopp",(unsigned char*)"1234567812345678", 16);
    printf("en:%s \n", en_base64.c_str());
    std::string de_base64 = aes_decrypt_ecb_base64(en_base64, (unsigned char*)"1234567812345678", 16);
    printf("de:%s \n", de_base64.c_str());
 
    // ebc hex
    std::string en_hex = aes_encrypt_ecb_hex("hello cryptopp", (unsigned char*)"1234567812345678", 16);
    printf("en:%s \n", en_hex.c_str());
    std::string de_hex = aes_decrypt_ecb_hex(en_hex, (unsigned char*)"1234567812345678", 16);
    printf("de:%s \n", de_hex.c_str());
    
 
 
    (void)getchar();
    return 0;
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用Crypto++库实现AES加密和解密的示例代码: ```c++ #include <iostream> #include <iomanip> #include <string> #include <cryptlib.h> #include <aes.h> #include <modes.h> #include <base64.h> using namespace CryptoPP; std::string encryptAES(const std::string& plainText, const std::string& key) { std::string cipherText; // 将密钥转换为byte数组 byte aesKey[AES::DEFAULT_KEYLENGTH]; ::memcpy(aesKey, key.c_str(), key.size()); // 选择ECB加密模式 ECB_Mode<AES>::Encryption encryption(aesKey, sizeof(aesKey)); // 加密 StringSource(plainText, true, new StreamTransformationFilter(encryption, new StringSink(cipherText) ) ); // 返回Base64编码后的密文 return base64_encode(reinterpret_cast<const unsigned char*>(cipherText.data()), cipherText.size()); } std::string decryptAES(const std::string& cipherText, const std::string& key) { std::string plainText; // 将密钥转换为byte数组 byte aesKey[AES::DEFAULT_KEYLENGTH]; ::memcpy(aesKey, key.c_str(), key.size()); // 选择ECB加密模式 ECB_Mode<AES>::Decryption decryption(aesKey, sizeof(aesKey)); // 解密 StringSource(base64_decode(cipherText), true, new StreamTransformationFilter(decryption, new StringSink(plainText) ) ); // 返回明文 return plainText; } int main() { // 待加密的明文和密钥 std::string plainText = "Hello, world!"; std::string key = "1234567890123456"; // 加密 std::string cipherText = encryptAES(plainText, key); std::cout << "Cipher text: " << cipherText << std::endl; // 解密 std::string decryptedText = decryptAES(cipherText, key); std::cout << "Decrypted text: " << decryptedText << std::endl; return 0; } ``` 这个示例代码使用了Crypto++库提供的AES加密和解密函数,使用ECB加密模式进行加密和解密,返回的是Base64编码后的密文。你可以根据自己的需求修改加密模式和返回结果的格式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值