| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- // crypto.cpp : 定义静态库的函数。
- //
- #include "pch.h"
- #include "framework.h"
- #include "Crypto.h"
- #include <iostream>
- #include <sstream>
- #include <cryptopp/aes.h>
- #include <cryptopp/filters.h>
- #include <cryptopp/modes.h>
- #include <cryptopp/BASE64.h>
- #include <cryptopp/hex.h>
- #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<CryptoPP::AES>::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<const unsigned char*>(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<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;
- }
- 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<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 "";
- }
- }
- 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<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::DEFAULT_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 "";
- }
- }
|