整理代码
This commit is contained in:
commit
c7b4e178ec
364 changed files with 59691 additions and 0 deletions
57
util/AESUtil.h
Normal file
57
util/AESUtil.h
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#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
|
||||
Loading…
Add table
Add a link
Reference in a new issue