[C++] 使用 cryptopp 加密解密

网友投稿 1537 2022-11-15

[C++] 使用 cryptopp 加密解密

[C++] 使用 cryptopp 加密解密

官网:​​, 以及所有项目中的头文件

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

#include #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::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(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::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(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(base64_data.c_str()), base64_data.length()); decoder.MessageEnd(); std::string decrypt_data; CryptoPP::ECB_Mode::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(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(hex_data.c_str()), hex_data.length()); decoder.MessageEnd(); std::string decrypt_data; CryptoPP::ECB_Mode::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(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;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:设计模式复习-抽象工厂模式
下一篇:Intel汇编语言程序设计学习-第三章 汇编语言基础-上
相关文章

 发表评论

暂时没有评论,来抢沙发吧~