57 lines
1.9 KiB
C++
57 lines
1.9 KiB
C++
#ifndef A89D0C3C_4F05_4631_A720_D8B2CEA5E99A
|
|
#define A89D0C3C_4F05_4631_A720_D8B2CEA5E99A
|
|
#include <Poco/Base64Decoder.h>
|
|
#include <Poco/Base64Encoder.h>
|
|
#include <Poco/Crypto/Cipher.h>
|
|
#include <Poco/Crypto/CipherFactory.h>
|
|
#include <Poco/Crypto/CipherKey.h>
|
|
#include <Poco/StreamCopier.h>
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include "Logger.h"
|
|
namespace Util
|
|
{
|
|
class AESUtil
|
|
{
|
|
public:
|
|
static std::string encryptAES(const std::string& plaintext, const std::string& key, const std::string& iv)
|
|
{
|
|
try
|
|
{
|
|
Poco::Crypto::CipherKey cipherKey("aes-256-cbc", key, iv);
|
|
Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory();
|
|
std::unique_ptr<Poco::Crypto::Cipher> pCipher(factory.createCipher(cipherKey));
|
|
|
|
std::string ciphertext = pCipher->encryptString(plaintext, Poco::Crypto::Cipher::ENC_BASE64_NO_LF);
|
|
return ciphertext;
|
|
}
|
|
catch (const Poco::Exception& e)
|
|
{
|
|
logger().error("encryptAES error: %s", std::string(e.what()));
|
|
return "";
|
|
}
|
|
}
|
|
|
|
static std::string decryptAES(const std::string& ciphertext, const std::string& key, const std::string& iv)
|
|
{
|
|
try
|
|
{
|
|
Poco::Crypto::CipherKey cipherKey("aes-256-cbc", key, iv);
|
|
Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory();
|
|
std::unique_ptr<Poco::Crypto::Cipher> pCipher(factory.createCipher(cipherKey));
|
|
|
|
std::string plaintext = pCipher->decryptString(ciphertext, Poco::Crypto::Cipher::ENC_BASE64_NO_LF);
|
|
return plaintext;
|
|
}
|
|
catch (const Poco::Exception& e)
|
|
{
|
|
logger().error("decryptAES error: %s", std::string(e.what()));
|
|
return "";
|
|
}
|
|
}
|
|
};
|
|
}
|
|
#endif
|