Modified By Vim Firstly.
This commit is contained in:
parent
2d15bc935e
commit
31aa47cac6
6
.gitignore
vendored
6
.gitignore
vendored
@ -40,6 +40,12 @@
|
|||||||
*.cbp
|
*.cbp
|
||||||
*.marks
|
*.marks
|
||||||
|
|
||||||
|
# Vim
|
||||||
|
*.swp
|
||||||
|
|
||||||
# Project Files
|
# Project Files
|
||||||
cmake-build-debug
|
cmake-build-debug
|
||||||
.idea
|
.idea
|
||||||
|
.cache
|
||||||
|
build
|
||||||
|
.DS_Store
|
||||||
|
@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.13)
|
|||||||
project(Net)
|
project(Net)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
include_directories(include/)
|
include_directories(include/)
|
||||||
include_directories(utils/)
|
include_directories(utils/)
|
||||||
|
67
compile_commands.json
Normal file
67
compile_commands.json
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/src/utils",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/utils.dir/aes_cbc_encryptor.cpp.o -c /home/eric/NetCpp/src/utils/aes_cbc_encryptor.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/src/utils/aes_cbc_encryptor.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/src/utils",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/utils.dir/random_generator.cpp.o -c /home/eric/NetCpp/src/utils/random_generator.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/src/utils/random_generator.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/src/utils",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/utils.dir/rsa_key_chain.cpp.o -c /home/eric/NetCpp/src/utils/rsa_key_chain.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/src/utils/rsa_key_chain.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/src/utils",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/utils.dir/sha256_generator.cpp.o -c /home/eric/NetCpp/src/utils/sha256_generator.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/src/utils/sha256_generator.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/src/debug_tools",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/debugTools.dir/print_tools.cpp.o -c /home/eric/NetCpp/src/debug_tools/print_tools.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/src/debug_tools/print_tools.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/src/communicate",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/commu.dir/tcp_client.cpp.o -c /home/eric/NetCpp/src/communicate/tcp_client.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/src/communicate/tcp_client.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/src/communicate",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/commu.dir/tcp_server.cpp.o -c /home/eric/NetCpp/src/communicate/tcp_server.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/src/communicate/tcp_server.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/src/communicate",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/commu.dir/tcp_session.cpp.o -c /home/eric/NetCpp/src/communicate/tcp_session.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/src/communicate/tcp_session.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/test/test_util",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testUtils.dir/main.cpp.o -c /home/eric/NetCpp/test/test_util/main.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/test/test_util/main.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/test/test_util",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testUtils.dir/test_aes.cpp.o -c /home/eric/NetCpp/test/test_util/test_aes.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/test/test_util/test_aes.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/test/test_util",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testUtils.dir/test_rsa.cpp.o -c /home/eric/NetCpp/test/test_util/test_rsa.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/test/test_util/test_rsa.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/test/test_util",
|
||||||
|
"command": "/usr/bin/c++ -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testUtils.dir/test_sha.cpp.o -c /home/eric/NetCpp/test/test_util/test_sha.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/test/test_util/test_sha.cpp"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"directory": "/home/eric/NetCpp/build/test/test_commu",
|
||||||
|
"command": "/usr/bin/c++ -DDEBUG -I/home/eric/NetCpp/include -I/home/eric/NetCpp/utils -std=gnu++14 -o CMakeFiles/testCommu.dir/main.cpp.o -c /home/eric/NetCpp/test/test_commu/main.cpp",
|
||||||
|
"file": "/home/eric/NetCpp/test/test_commu/main.cpp"
|
||||||
|
}
|
||||||
|
]
|
@ -5,7 +5,6 @@
|
|||||||
#ifndef NET_TCP_SERVER_H
|
#ifndef NET_TCP_SERVER_H
|
||||||
#define NET_TCP_SERVER_H
|
#define NET_TCP_SERVER_H
|
||||||
|
|
||||||
|
|
||||||
// 基础依赖
|
// 基础依赖
|
||||||
#include <project.h>
|
#include <project.h>
|
||||||
|
|
||||||
@ -14,41 +13,45 @@
|
|||||||
|
|
||||||
namespace Net {
|
namespace Net {
|
||||||
|
|
||||||
class TCPServer {
|
/**
|
||||||
public:
|
* TCP Server using
|
||||||
TCPServer(int port, int max_connection);
|
*
|
||||||
|
*/
|
||||||
|
class TCPServer {
|
||||||
|
public:
|
||||||
|
TCPServer(int port, int max_connection);
|
||||||
|
|
||||||
~TCPServer(){
|
~TCPServer() {
|
||||||
stop();
|
stop();
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void stop(){
|
void stop() {
|
||||||
if(p_accept_manager_thread != nullptr)
|
if (p_accept_manager_thread != nullptr)
|
||||||
p_accept_manager_thread->interrupt();
|
p_accept_manager_thread->interrupt();
|
||||||
this->status = -1;
|
this->status = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t readByte();
|
uint8_t readByte();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
int fd;
|
||||||
|
int status = 0;
|
||||||
|
struct sockaddr_in server_addr;
|
||||||
|
std::queue<uint8_t> recv_buff;
|
||||||
|
boost::mutex buff_mutex;
|
||||||
|
boost::thread *p_accept_manager_thread;
|
||||||
|
|
||||||
int fd;
|
void cycle();
|
||||||
int status = 0;
|
|
||||||
struct sockaddr_in server_addr;
|
|
||||||
std::queue<uint8_t> recv_buff;
|
|
||||||
boost::mutex buff_mutex;
|
|
||||||
boost::thread *p_accept_manager_thread;
|
|
||||||
|
|
||||||
void cycle();
|
static void connection_manager(TCPServer *server);
|
||||||
|
|
||||||
static void accept_manager(TCPServer *server);
|
static void accept(int fd, boost::mutex *buff_mutex,
|
||||||
|
std::queue<uint8_t> *recv_buff, const int *status);
|
||||||
|
|
||||||
static void accept(int fd, boost::mutex *buff_mutex, std::queue<uint8_t> *recv_buff, const int *status);
|
void create_socket(int port);
|
||||||
|
};
|
||||||
|
|
||||||
void create_socket(int port);
|
} // namespace Net
|
||||||
};
|
|
||||||
|
|
||||||
}
|
#endif // NET_TCP_SERVER_H
|
||||||
|
|
||||||
#endif //NET_TCP_SERVER_H
|
|
||||||
|
@ -7,41 +7,39 @@
|
|||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
|
||||||
#include <openssl/aes.h>
|
#include <openssl/aes.h>
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "debug_tools/print_tools.h"
|
#include "debug_tools/print_tools.h"
|
||||||
#include "random_generator.h"
|
#include "random_generator.h"
|
||||||
|
|
||||||
namespace Net {
|
namespace Net {
|
||||||
|
|
||||||
class AESCBCEncryptor {
|
class AESCBCEncryptor {
|
||||||
public:
|
public:
|
||||||
|
AESCBCEncryptor();
|
||||||
|
|
||||||
AESCBCEncryptor();
|
string getKeyData() const;
|
||||||
|
|
||||||
string getKeyData() const;
|
void encrypt(const std::string &data, std::string &encrypted_data);
|
||||||
|
|
||||||
void encrypt(const std::string &data, std::string &encrypted_data);
|
void decrypt(std::string &data, const std::string &encrypt_data);
|
||||||
|
|
||||||
void decrypt(std::string &data, const std::string &encrypt_data);
|
private:
|
||||||
|
const int nrounds = 8;
|
||||||
|
|
||||||
private:
|
uint8_t key[32], iv[32];
|
||||||
const int nrounds = 8;
|
|
||||||
|
|
||||||
uint8_t key[32], iv[32];
|
EVP_CIPHER_CTX *e_ctx = EVP_CIPHER_CTX_new();
|
||||||
|
|
||||||
EVP_CIPHER_CTX *e_ctx = EVP_CIPHER_CTX_new();
|
std::string key_data;
|
||||||
|
|
||||||
std::string key_data;
|
void generate_random_key_data();
|
||||||
|
|
||||||
void generate_random_key_data();
|
void aes_init(std::string &key_data);
|
||||||
|
};
|
||||||
|
|
||||||
void aes_init(std::string &key_data);
|
} // namespace Net
|
||||||
|
|
||||||
};
|
#endif // NET_AES_CBC_ENCRYPTOR_H
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif //NET_AES_CBC_ENCRYPTOR_H
|
|
||||||
|
@ -2,81 +2,86 @@
|
|||||||
// Created by 胡宇 on 2020/7/7.
|
// Created by 胡宇 on 2020/7/7.
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "debug_tools/print_tools.h"
|
|
||||||
#include "communicate/tcp_server.h"
|
#include "communicate/tcp_server.h"
|
||||||
|
#include "debug_tools/print_tools.h"
|
||||||
|
|
||||||
extern int errno;
|
extern int errno;
|
||||||
|
|
||||||
void Net::TCPServer::cycle() {
|
void Net::TCPServer::cycle() {
|
||||||
boost::thread accept_manager_thread(TCPServer::accept_manager, this);
|
boost::thread accept_manager_thread(TCPServer::connection_manager, this);
|
||||||
this->p_accept_manager_thread = &accept_manager_thread;
|
this->p_accept_manager_thread = &accept_manager_thread;
|
||||||
accept_manager_thread.detach();
|
accept_manager_thread.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Net::TCPServer::accept_manager(Net::TCPServer *server) {
|
void Net::TCPServer::connection_manager(Net::TCPServer *server) {
|
||||||
Net::PrintTools::debugPrintSuccess("AcceptManager Started.");
|
Net::PrintTools::debugPrintSuccess("AcceptManager Started.");
|
||||||
while(true){
|
while (true) {
|
||||||
int connect_fd = ::accept(server->fd, nullptr, nullptr);
|
int connect_fd = ::accept(server->fd, nullptr, nullptr);
|
||||||
Net::PrintTools::debugPrintSuccess("New Connection.");
|
Net::PrintTools::debugPrintSuccess("New Connection.");
|
||||||
|
|
||||||
if(connect_fd < 0) throw std::runtime_error(strerror(errno));
|
if (connect_fd < 0)
|
||||||
else{
|
throw std::runtime_error(strerror(errno));
|
||||||
boost::thread accept_thread(TCPServer::accept, connect_fd, &server->buff_mutex, &server->recv_buff, &server->status);
|
else {
|
||||||
accept_thread.detach();
|
boost::thread accept_thread(TCPServer::accept, connect_fd,
|
||||||
}
|
&server->buff_mutex, &server->recv_buff,
|
||||||
|
&server->status);
|
||||||
|
accept_thread.detach();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex, std::queue<uint8_t> *recv_buff, const int *status) {
|
void Net::TCPServer::accept(int fd, boost::mutex *buff_mutex,
|
||||||
Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection.");
|
std::queue<uint8_t> *recv_buff, const int *status) {
|
||||||
uint8_t buff[1024];
|
Net::PrintTools::debugPrintSuccess("Try Getting Data From Connection.");
|
||||||
int len;
|
uint8_t buff[1024];
|
||||||
std::vector<uint8_t> accept_buff;
|
int len;
|
||||||
while((len = recv(fd, buff, sizeof(buff), 0)) > 0 && *status == 0){
|
std::vector<uint8_t> accept_buff;
|
||||||
Net::PrintTools::debugPrintSuccess("Received.");
|
while ((len = recv(fd, buff, sizeof(buff), 0)) > 0 && *status == 0) {
|
||||||
for(int i = 0; i < len; ++i) accept_buff.push_back(buff[i]);
|
Net::PrintTools::debugPrintSuccess("Received.");
|
||||||
}
|
for (int i = 0; i < len; ++i)
|
||||||
if(*status == 0) {
|
accept_buff.push_back(buff[i]);
|
||||||
buff_mutex->lock();
|
}
|
||||||
for(unsigned char & i : accept_buff)
|
if (*status == 0) {
|
||||||
recv_buff->push(i);
|
buff_mutex->lock();
|
||||||
buff_mutex->unlock();
|
for (unsigned char &i : accept_buff)
|
||||||
}
|
recv_buff->push(i);
|
||||||
close(fd);
|
buff_mutex->unlock();
|
||||||
Net::PrintTools::debugPrintSuccess("Connection Closed.");
|
}
|
||||||
|
close(fd);
|
||||||
|
Net::PrintTools::debugPrintSuccess("Connection Closed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Net::TCPServer::create_socket(int port) {
|
void Net::TCPServer::create_socket(int port) {
|
||||||
if((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) throw std::runtime_error(strerror(errno));
|
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
|
||||||
|
throw std::runtime_error(strerror(errno));
|
||||||
|
|
||||||
std::memset(&server_addr, 0, sizeof(struct sockaddr_in));
|
std::memset(&server_addr, 0, sizeof(struct sockaddr_in));
|
||||||
this->server_addr.sin_family = AF_INET;
|
this->server_addr.sin_family = AF_INET;
|
||||||
this->server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
this->server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
this->server_addr.sin_port = htons(port);
|
this->server_addr.sin_port = htons(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
Net::TCPServer::TCPServer(int port, int max_connection) {
|
Net::TCPServer::TCPServer(int port, int max_connection) {
|
||||||
|
|
||||||
// 创建 socket
|
// 创建 socket
|
||||||
create_socket(port);
|
create_socket(port);
|
||||||
|
|
||||||
if(bind(fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)
|
if (bind(fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
|
||||||
throw std::runtime_error(strerror(errno));
|
throw std::runtime_error(strerror(errno));
|
||||||
|
|
||||||
if(listen(fd, max_connection) < 0)
|
if (listen(fd, max_connection) < 0)
|
||||||
throw std::runtime_error(strerror(errno));
|
throw std::runtime_error(strerror(errno));
|
||||||
|
|
||||||
|
cycle();
|
||||||
cycle();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Net::TCPServer::readByte() {
|
uint8_t Net::TCPServer::readByte() {
|
||||||
uint8_t byte = '\0';
|
uint8_t byte = '\0';
|
||||||
buff_mutex.try_lock();
|
buff_mutex.try_lock();
|
||||||
if(!recv_buff.empty()) {
|
if (!recv_buff.empty()) {
|
||||||
byte = recv_buff.front();
|
byte = recv_buff.front();
|
||||||
recv_buff.pop();
|
recv_buff.pop();
|
||||||
}
|
}
|
||||||
buff_mutex.unlock();
|
buff_mutex.unlock();
|
||||||
return byte;
|
return byte;
|
||||||
}
|
}
|
||||||
|
@ -4,74 +4,78 @@
|
|||||||
|
|
||||||
#include "utils/aes_cbc_encryptor.h"
|
#include "utils/aes_cbc_encryptor.h"
|
||||||
|
|
||||||
|
#include "utils/sha256_generator.h"
|
||||||
|
|
||||||
void Net::AESCBCEncryptor::encrypt(const string &data, string &encrypted_data) {
|
void Net::AESCBCEncryptor::encrypt(const string &data, string &encrypted_data) {
|
||||||
int c_len = data.length() + AES_BLOCK_SIZE, f_len = 0;
|
int c_len = data.length() + AES_BLOCK_SIZE, f_len = 0;
|
||||||
|
|
||||||
auto *encrypt_buffer = reinterpret_cast<uint8_t *>(malloc(c_len));
|
auto *encrypt_buffer = reinterpret_cast<uint8_t *>(malloc(c_len));
|
||||||
|
|
||||||
EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
|
EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
|
||||||
EVP_EncryptUpdate(e_ctx, encrypt_buffer, &c_len,
|
EVP_EncryptUpdate(e_ctx, encrypt_buffer, &c_len,
|
||||||
reinterpret_cast<const unsigned char *>(data.data()), data.length());
|
reinterpret_cast<const unsigned char *>(data.data()),
|
||||||
EVP_EncryptFinal_ex(e_ctx, encrypt_buffer + c_len, &f_len);
|
data.length());
|
||||||
|
EVP_EncryptFinal_ex(e_ctx, encrypt_buffer + c_len, &f_len);
|
||||||
|
|
||||||
int len = c_len + f_len;
|
int len = c_len + f_len;
|
||||||
|
|
||||||
if(!encrypted_data.empty()) encrypted_data.clear();
|
if (!encrypted_data.empty())
|
||||||
|
encrypted_data.clear();
|
||||||
|
|
||||||
encrypted_data.append(reinterpret_cast<const char *>(encrypt_buffer), len);
|
encrypted_data.append(reinterpret_cast<const char *>(encrypt_buffer), len);
|
||||||
|
|
||||||
EVP_CIPHER_CTX_reset(e_ctx);
|
EVP_CIPHER_CTX_reset(e_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Net::AESCBCEncryptor::decrypt(string &data, const string &encrypt_data) {
|
void Net::AESCBCEncryptor::decrypt(string &data, const string &encrypt_data) {
|
||||||
int p_len = encrypt_data.length(), f_len = 0;
|
int p_len = encrypt_data.length(), f_len = 0;
|
||||||
auto *plain_buffer = static_cast<uint8_t *>(malloc(p_len));
|
auto *plain_buffer = static_cast<uint8_t *>(malloc(p_len));
|
||||||
|
|
||||||
EVP_DecryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
|
EVP_DecryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
|
||||||
EVP_DecryptUpdate(e_ctx, plain_buffer, &p_len,
|
EVP_DecryptUpdate(
|
||||||
reinterpret_cast<const unsigned char *>(encrypt_data.data()), encrypt_data.length());
|
e_ctx, plain_buffer, &p_len,
|
||||||
EVP_DecryptFinal_ex(e_ctx, plain_buffer + p_len, &f_len);
|
reinterpret_cast<const unsigned char *>(encrypt_data.data()),
|
||||||
|
encrypt_data.length());
|
||||||
|
EVP_DecryptFinal_ex(e_ctx, plain_buffer + p_len, &f_len);
|
||||||
|
|
||||||
int len = p_len + f_len;
|
int len = p_len + f_len;
|
||||||
|
|
||||||
if(!data.empty()) data.clear();
|
if (!data.empty())
|
||||||
|
data.clear();
|
||||||
|
|
||||||
data.append(reinterpret_cast<const char *>(plain_buffer), len);
|
data.append(reinterpret_cast<const char *>(plain_buffer), len);
|
||||||
|
|
||||||
EVP_CIPHER_CTX_reset(e_ctx);
|
EVP_CIPHER_CTX_reset(e_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Net::AESCBCEncryptor::generate_random_key_data() {
|
void Net::AESCBCEncryptor::generate_random_key_data() {
|
||||||
Rand::UniformUInt rand(0, UINT32_MAX);
|
Rand::UniformUInt rand(0, UINT32_MAX);
|
||||||
|
|
||||||
uint32_t p_data[8];
|
uint32_t p_data[8];
|
||||||
for(unsigned int & i : p_data){
|
for (unsigned int &i : p_data) {
|
||||||
i = rand.generate();
|
i = rand.generate();
|
||||||
}
|
}
|
||||||
|
|
||||||
key_data.append(reinterpret_cast<const char *>(p_data), 32);
|
key_data.append(reinterpret_cast<const char *>(p_data), 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Net::AESCBCEncryptor::aes_init(string &key_data) {
|
void Net::AESCBCEncryptor::aes_init(string &key_data) {
|
||||||
|
|
||||||
|
int i =
|
||||||
|
EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), nullptr,
|
||||||
|
reinterpret_cast<const unsigned char *>(key_data.c_str()),
|
||||||
|
key_data.length(), nrounds, key, iv);
|
||||||
|
if (i != 32) {
|
||||||
|
throw std::runtime_error("key data must equal 256 bits.");
|
||||||
|
}
|
||||||
|
|
||||||
int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha256(), nullptr,
|
EVP_CIPHER_CTX_init(e_ctx);
|
||||||
reinterpret_cast<const unsigned char *>(key_data.c_str()), key_data.length(),
|
EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
|
||||||
nrounds, key, iv);
|
|
||||||
if (i != 32) {
|
|
||||||
throw std::runtime_error("key data must equal 256 bits.");
|
|
||||||
}
|
|
||||||
|
|
||||||
EVP_CIPHER_CTX_init(e_ctx);
|
|
||||||
EVP_EncryptInit_ex(e_ctx, EVP_aes_256_cbc(), nullptr, key, iv);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string Net::AESCBCEncryptor::getKeyData() const {
|
string Net::AESCBCEncryptor::getKeyData() const { return key_data; }
|
||||||
return key_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
Net::AESCBCEncryptor::AESCBCEncryptor() {
|
Net::AESCBCEncryptor::AESCBCEncryptor() {
|
||||||
generate_random_key_data();
|
generate_random_key_data();
|
||||||
aes_init(key_data);
|
aes_init(key_data);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user