#include "logutils.h"
#include "HttpRequestUtilsEx.h"
#include "DataTransfrom.h"
#include "utils/config.h"
#include "utils/cryptutils.h"
#include "aes.h"
#include "modes.h"
#include "filters.h"
#include "base64.h"
#include "hex.h"
#include "queue.h"
#include <fstream>
std::string StringToHex(const std::string& data)
{
const std::string hex = "0123456789ABCDEF";
std::stringstream ss;
for (std::string::size_type i = 0; i < data.size(); ++i)
ss << hex[(unsigned char)data[i] >> 4] << hex[(unsigned char)data[i] & 0xf];
std::cout << ss.str() << std::endl;
return ss.str();
}
std::string HexToStr(const std::string& str)
{
std::string result;
for (size_t i = 0; i < str.length(); i += 2)
{
std::string byte = str.substr(i, 2);
char chr = (char)(int)strtol(byte.c_str(), NULL, 16);
result.push_back(chr);
}
return result;
}
// AES加密字符串,结果转为BASE64
std::string test_encrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryption;
encryption.SetKeyWithIV((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::StringSource encryptor(str_in, true,
new CryptoPP::StreamTransformationFilter(encryption,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(str_out),
CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING // do not append a newline
), CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING
)
);
return str_out;
}
// AES解密字符串,从BASE64解密
std::string test_decrypt2(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decryption;
decryption.SetKeyWithIV((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::StringSource decryptor(str_in,
true,
new CryptoPP::Base64Decoder(
new CryptoPP::StreamTransformationFilter(decryption,
new CryptoPP::StringSink(str_out),
CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING
)
)
);
return str_out;
}
// AES解密字符串,从BASE64解密
std::string test_decrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string strBase64Decoded = "";
std::string strDecodeOut = "";
try
{
CryptoPP::StringSource(str_in, true,
new CryptoPP::Base64Decoder(new CryptoPP::StringSink(strBase64Decoded)));
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption cbc_decrypt;
cbc_decrypt.SetKeyWithIV((byte *)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::StringSource(strBase64Decoded,
true,
new CryptoPP::StreamTransformationFilter(cbc_decrypt,
new CryptoPP::StringSink(strDecodeOut),
CryptoPP::BlockPaddingSchemeDef::BlockPaddingScheme::PKCS_PADDING)
);
}catch (...) {
strDecodeOut = "";
}
return strDecodeOut;
}
void decrypt_byte(const std::string& key, const std::string& iv, const byte cipherData[], const int cipherDataLen, byte plainData[], int &plainDataLen)
{
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decryption;
decryption.SetKeyWithIV((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::ArraySink *dst = new CryptoPP::ArraySink(plainData, plainDataLen);
CryptoPP::StreamTransformationFilter *transformation = new CryptoPP::StreamTransformationFilter(decryption, dst,
CryptoPP::BlockPaddingSchemeDef::BlockPaddingScheme::PKCS_PADDING);
CryptoPP::ArraySource decryptor(cipherData, cipherDataLen,true, transformation);
plainDataLen = (int)dst->TotalPutLength();
}
void DecryptData(const std::string& key, const std::string& iv, const byte cipherData[], const int cipherDataLen, byte plainData[], int &plainDataLen)
{
//try {
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decrypt;
decrypt.SetKeyWithIV((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::ArraySink decSink(plainData, plainDataLen);
CryptoPP::StreamTransformationFilter stfdec(decrypt, new CryptoPP::Redirector(decSink),
CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING);
stfdec.Put(cipherData, cipherDataLen);
stfdec.MessageEnd();
// }
//catch (const CryptoPP::Exception &e) {
// }
}
void decrypt_byte_gf(const std::string& key, const std::string& iv, std::string &cipher)
{
//try
// {
std::string strBase64Decoded;
CryptoPP::StringSource(cipher, true,
new CryptoPP::Base64Decoder(new CryptoPP::StringSink(strBase64Decoded)));
std::string recovered;
CryptoPP::CBC_Mode< CryptoPP::AES >::Decryption d;
d.SetKeyWithIV((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
// The StreamTransformationFilter removes
// padding as required.
CryptoPP::StringSource s(strBase64Decoded.c_str(), true,
new CryptoPP::StreamTransformationFilter(d,
new CryptoPP::StringSink(recovered),
CryptoPP::BlockPaddingSchemeDef::BlockPaddingScheme::PKCS_PADDING
) // StreamTransformationFilter
); // StringSource
#if 1
CryptoPP::StreamTransformationFilter filter(d);
filter.Put((const byte*)cipher.data(), cipher.size());
filter.MessageEnd();
const size_t ret = filter.MaxRetrievable();
recovered.resize(ret);
filter.Get((byte*)recovered.data(), recovered.size());
#endif
std::cout << "recovered text: " << recovered << std::endl;
//}
//catch (const CryptoPP::Exception& e)
//{
exit(1);
//}
}
//CryptoPP::QByteArray decrypt_byte2(QByteArray in) {
//
// QByteArray iv = in.left(CryptoPP::AES::BLOCKSIZE);
// in.remove(0, CryptoPP::AES::BLOCKSIZE);
// string decrypted;
// QByteArray result;
//
// try {
// CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption aes_dec;
// aes_dec.SetKeyWithIV((byte *)key().data(), keySize(), (byte *)iv.data());
//
// ArraySource((byte *)in.data(), in.size(), true,
// new StreamTransformationFilter(aes_dec,
// new StringSink(decrypted)));
//
// result = QByteArray(decrypted.c_str(), in.size());
// }
// catch (CryptoPP::Exception err) {
// result = "Failed to decrypt";
// qDebug() << QString(err.GetWhat().c_str());
// }
//
// return result;
//}
// 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解密,这里传入的base64_data是AES加密后用base64编码得到的数据
std::string aes_encrypt(unsigned char* key, int keylen, unsigned char* iv, std::string base64_data)
{
try {
// 没有找到输入流的前置处理接口,这里先做base64解码
std::string aes_encrypt_data;
CryptoPP::Base64Encoder ecoder;
ecoder.Attach(new CryptoPP::StringSink(aes_encrypt_data));
ecoder.Put(reinterpret_cast<const unsigned char*>(base64_data.c_str()), base64_data.length());
ecoder.MessageEnd();
// 类似AES加密,得到原始数据
std::string decrypt_data;
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryption(key, keylen, iv);
CryptoPP::StreamTransformationFilter stf_description(encryption,
new CryptoPP::StringSink(decrypt_data), CryptoPP::BlockPaddingSchemeDef::PKCS_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解密,这里传入的base64_data是AES加密后用base64编码得到的数据
std::string aes_decrypt2(unsigned char* key, int keylen, unsigned char* iv, std::string base64_data)
{
try {
// 没有找到输入流的前置处理接口,这里先做base64解码
std::string aes_encrypt_data;
CryptoPP::Base64Decoder ecoder;
ecoder.Attach(new CryptoPP::StringSink(aes_encrypt_data));
ecoder.Put(reinterpret_cast<const unsigned char*>(base64_data.c_str()), base64_data.length());
ecoder.MessageEnd();
// 类似AES加密,得到原始数据
std::string decrypt_data;
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption encryption(key, keylen, iv);
CryptoPP::StreamTransformationFilter stf_description(encryption,
new CryptoPP::StringSink(decrypt_data), CryptoPP::BlockPaddingSchemeDef::PKCS_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 "";
}
}
void decrypt2(const std::string& key, const std::string& iv, const byte cipherData[], const int cipherDataLen, byte plainData[], int &plainDataLen)
{
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decryption;
decryption.SetKeyWithIV((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::ArraySink *dst = new CryptoPP::ArraySink(plainData, plainDataLen);
CryptoPP::ArraySource decryptor(cipherData, cipherDataLen, true,
new CryptoPP::StreamTransformationFilter(decryption, dst)
);
plainDataLen = (int)dst->TotalPutLength();
}
// aes解密,这里传入的base64_data是AES加密后用base64编码得到的数据
std::string aes_decrypt(unsigned char* key, int keylen, unsigned char* iv, std::string base64_data)
{
try {
// 没有找到输入流的前置处理接口,这里先做base64解码
std::string aes_encrypt_data;
byte *base64_buff = new byte[base64_data.length()];
CryptoPP::Base64Decoder decoder;
CryptoPP::ArraySink base64_array(base64_buff, base64_data.length());
//decoder.Attach(new CryptoPP::StringSink(aes_encrypt_data));
decoder.Attach(&base64_array);
decoder.Put(reinterpret_cast<const unsigned char*>(base64_data.c_str()), base64_data.length());
decoder.MessageEnd();
// 类似AES加密,得到原始数据
std::string decrypt_data;
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption cbc_description;
cbc_description.SetKeyWithIV(key, keylen, iv);
//CryptoPP::ArraySink decSink(buff, base64_data.length());
CryptoPP::StreamTransformationFilter stfdec(cbc_description, new CryptoPP::Redirector(base64_array),
CryptoPP::BlockPaddingSchemeDef::PKCS_PADDING);
/*byte *data = new byte[base64_array.m_to];
stfdec.Put(data, base64_data.length());
stfdec.MessageEnd();*/
/*CryptoPP::StreamTransformationFilter stf_description(cbc_description,
new CryptoPP::StringSink(decrypt_data), CryptoPP::BlockPaddingSchemeDef::PKCS_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 "";
}
}
cryptopp aes cbc pcsk padding 加解密
最新推荐文章于 2025-02-11 22:22:27 发布