继续编写端对端加密报文部分

This commit is contained in:
Saturneic 2019-02-09 00:41:10 +08:00
parent 68e26768fc
commit 4b4523ae4a
3 changed files with 49 additions and 6 deletions

View File

@ -16,6 +16,7 @@
#include "sqlite3.h"
#include "rsa.h"
#include "rng.hpp"
#include "aes.hpp"
class Server;
@ -46,21 +47,20 @@ struct request {
//加密端对端报文
struct encrypt_post{
// 明文部分
// 注册客户端id
uint64_t client_id;
// 目标ip
string ip;
// 目标端口
int port;
// 加密部分
// 匹配id
rng::rng64 p_id;
uint64_t p_id;
// 类型
uint32_t type;
// 内容
Byte *buff;
// 内容长度
uint32_t buff_size;
};
//回复数据包
@ -139,8 +139,12 @@ struct server_info{
};
struct aes_key256{
private:
uint64_t key[4];
public:
// 生成新的随机密钥
aes_key256();
const uint8_t *GetKey(void);
};
//通用服务器类
@ -227,8 +231,8 @@ public:
static void Respond2Packet(packet &pkt, respond &res);
static void Packet2Respond(packet &pkt, respond &res);
static void Post2Packet(packet &pkt, encrypt_post &pst, rng::rng128 key);
static void Packet2Post(packet &pkt, encrypt_post &pst, rng::rng128 key);
static void Post2Packet(packet &pkt, encrypt_post &pst, aes_key256 &key);
static void Packet2Post(packet &pkt, encrypt_post &pst, aes_key256 &key);
};
class Client{
@ -242,6 +246,8 @@ class Client{
SocketUDPClient send_socket;
// 广场服务器通信公钥
public_key_class sqe_pbc;
// 报文密钥
aes_key256 post_key;
public:
// 构造函数(send_port指的是发送的目标端口)
Client(int port = 9050, string send_ip = "127.0.0.1",int send_port = 9049);

View File

@ -54,5 +54,6 @@ typedef char Byte;
#define REQUSET_TYPE 100
#define RESPOND_TYPE 101
#define ENCRYPT_POST_TYPE 101
#endif /* type_h */

View File

@ -561,3 +561,39 @@ void Server::ProcessSendPackets(void){
aes_key256::aes_key256(){
for (int i = 0; i < 4; i++) key[i] = rand64();
}
const uint8_t *aes_key256::GetKey(void){
return (const uint8_t *)&key;
}
void SQEServer::Post2Packet(packet &pkt, encrypt_post &pst, aes_key256 &key){
pkt.type = ENCRYPT_POST_TYPE;
Addr taddr(pst.ip,pst.port);
pkt.address = *taddr.Obj();
pkt.AddBuff(&pst.client_id, sizeof(uint64_t));//0
pkt.AddBuff(&pst.p_id, sizeof(uint64_t));//1
pkt.AddBuff(pst.ip.data(), pst.ip.size());//2
pkt.AddBuff(pst.port, sizeof(uint32_t));//3
pkt.AddBuff(&pst.buff_size, sizeof(uint32_t));//4
// 加密数据
AES_ctx naes;
AES_init_ctx(&naes, key.GetKey());
AES_CBC_encrypt_buffer(naes, pst.buff, pst.buff_size);
pkt.AddBuff(&pst.buff_size, sizeof(uint32_t));//5
}
void SQEServer::Packet2Post(packet &pkt, encrypt_post &pst, aes_key256 &key){
pst.client_id = *(uint64_t *)pkt.buffs[0];
pst.p_id = *(uint64_t *)pkt.buffs[1];
pst.ip = (const char *)pkt.buffs[2];
pst.port = *(uint32_t *)pkt.buffs[3];
pst.buff_size = *(uint32_t)pkt.buffs[4];
Byte *t_data = (Byte *)malloc(pst.buff_size);
memcpy(t_data, pkt.buffs[5], pst.buff_size);
// 解密数据
AES_ctx naes;
AES_init_ctx(&naes, key.GetKey());
AES_CBC_decrypt_buffer(&naes, t_data, pst.buff_size);
pst.buff = t_data;
}