// crypto.cpp : 定义静态库的函数。 // #include "pch.h" #include "framework.h" #include "Crypto.h" #include #include #include #include #include #include #include #pragma comment(lib,"cryptlib.lib") // TODO: 这是一个库函数示例 void fncrypto() { } std::string Crypto::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::DEFAULT_PADDING ); stf_encription.Put(reinterpret_cast(data.c_str()), data.length()); stf_encription.MessageEnd(); } catch (std::exception e) { std::cout << e.what() << std::endl; } return encrypt_str; } std::string Crypto::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; } std::string Crypto::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 ""; } } std::string Crypto::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::DEFAULT_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 ""; } }