//Socket报文发送c++升级版
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include"des.h"
//协议类
class SCK_HANDLE{
public:
SCK_HANDLE(){
memset(buf, 0, sizeof(buf));
buflen = 0;
}
char buf[1024];
int buflen;
};
//框架类
class SocketFrame{
public:
virtual int cltSocketSend(unsigned char *buf/*in*/) = 0;
virtual int cltSocketRev(unsigned char **buf/*out*/, int *buflen/*out*/) = 0;
virtual ~SocketFrame(){}
};
//加密框架类
class Encrypt{
public:
//加密
virtual int DesEncSocket(char *plainstr/*in*/, int plainlen, char **cryptstr/*out*/, int *cryptlen) = 0;
//解密
virtual int DesDecSocket(char *cryptstr/*in*/, int cryptlen, char **plainstr/*out*/, int *plainlen) = 0;
};
//我自己的加密类
class MyEncrypt :public Encrypt{
public:
//加密
virtual int DesEncSocket(char *plainstr/*in*/, int plainlen, char **cryptstr/*out*/, int *cryptlen){
int ERRO_MSG = 0;
if (plainstr == NULL || cryptstr == NULL || cryptlen==NULL)
{
ERRO_MSG = 1;
cout << "加密参数列表为空!" << endl;
return ERRO_MSG;
}
//des密文比明文长,但是密文最多比明文多8个字节(所以我取10个足够)
*cryptstr = (char *)malloc((sizeof(char)*plainlen)+10);
if (cryptstr==NULL)
{
ERRO_MSG = 2;
cout << "加密函数分配输出内存失败!" << endl;
return ERRO_MSG;
}
int ret= DesEnc((unsigned char *)plainstr, plainlen, (unsigned char *)*cryptstr, cryptlen);
if (ret!=0)
{
ERRO_MSG = 3;
cout << "des加密函数执行失败!" << endl;
return ERRO_MSG;
}
return ERRO_MSG;
}
//解密
virtual int DesDecSocket(char *cryptstr/*in*/, int cryptlen, char **plainstr/*out*/, int *plainlen){
int ERRO_MSG = 0;
if (plainstr == NULL || cryptstr == NULL || plainlen == NULL)
{
ERRO_MSG = 1;
cout << "解密参数列表为空!" << endl;
return ERRO_MSG;
}
//分配内存空间(des密文只可能比明文长)
*plainstr = (char *)malloc((sizeof(char)*cryptlen));
if (plainstr==NULL)
{
ERRO_MSG = 2;
cout << "分配输出内存失败!" << endl;
return ERRO_MSG;
}
int ret = DesDec((unsigned char *)cryptstr, cryptlen, (unsigned char *)*plainstr, plainlen);
if (ret != 0)
{
ERRO_MSG = 3;
cout << "des解密函数执行失败!" << endl;
return ERRO_MSG;
}
return ERRO_MSG;
}
};
//第三方的类
class MySocket :public SocketFrame{
public:
MySocket(){
//分配内存,因为handle贯穿整个报文发送,所以不适合栈内存
handle = new SCK_HANDLE();
mye = new MyEncrypt();
}
MySocket(MySocket &msp/*in*/){
cout << "不允许使用拷贝够咱函数" << endl;
//以防不备
handle = new SCK_HANDLE();
handle->buflen = msp.handle->buflen;
strcpy(handle->buf, msp.handle->buf);
}
//接收报文
virtual int cltSocketSend(unsigned char *buf/*in*/){
int ERRO_MSG = 0;
if (buf==NULL)
{
ERRO_MSG = 1;
cout << "接收报文的值为空!" << endl;
return ERRO_MSG;
}
if (handle==NULL)
{
ERRO_MSG = 2;
cout << "报文对象没有初始化!" << endl;
return ERRO_MSG;
}
//报文加密
int tempt = strlen((const char *)buf) + 1;
//准备密文接收缓存
unsigned char *tempstr = NULL;
int ret= mye->DesEncSocket((char *)buf, tempt, (char **)&tempstr, &tempt);
if (ret!=0)
{
ERRO_MSG = 3;
cout << "密文加密失败" << endl;
return ERRO_MSG;
}
//将密文放进报文结构体
strcpy(handle->buf, (const char*)tempstr);
//释放内存
if (tempstr!=NULL)
{
free(tempstr);
tempstr = NULL;
}
handle->buflen = tempt;
return ERRO_MSG;
}
//发送报文
virtual int cltSocketRev(unsigned char **buf/*out*/, int *buflen/*out*/){
int ERRO_MSG = 0;
if (buf == NULL || buflen==NULL)
{
ERRO_MSG = 1;
cout << "业务接受报文数组不存在或者业务接受报文长度变量不存在!" << endl;
return ERRO_MSG;
}
if (handle == NULL)
{
ERRO_MSG = 2;
cout << "报文对象没有初始化!" << endl;
return ERRO_MSG;
}
//解密报文
int ret = mye->DesDecSocket(handle->buf, handle->buflen, (char **)buf, buflen);
if (ret!=0)
{
ERRO_MSG = 3;
cout << "报文对象密文解密失败!" << endl;
return ERRO_MSG;
}
return ERRO_MSG;
}
~MySocket(){
if (handle!=NULL)
{
delete handle;
handle = NULL;
}
if (mye!=NULL)
{
delete mye;
mye = NULL;
}
}
private:
SCK_HANDLE *handle;
MyEncrypt *mye;
};
//操作业务类
class ProtectSocket{
public:
void Setsp(SocketFrame &pin){
sp = &pin;
}
void ProtectA();
~ProtectSocket(){
}
private:
SocketFrame *sp;
};
void ProtectSocket::ProtectA(){
unsigned char newbuf1[1024] = "我是发送的报文:dddddddddddddd";
//发送报文
int ret = 0;
ret = sp->cltSocketSend(newbuf1);
if (ret!=0)
{
cout << "报文发送出错!" << endl;
}
unsigned char *newbuf2 = NULL;
int newlen2 = 0;
//接受报文
ret = sp->cltSocketRev(&newbuf2, &newlen2);
if (ret!=0)
{
cout << "报文接受出错!" << endl;
}
//释放内存
if (newbuf2!=NULL)
{
free(newbuf2);
newbuf2 = NULL;
}
}
void main(){
//new 第三方类对象
SocketFrame *sp = new MySocket();
ProtectSocket *psc = new ProtectSocket();
psc->Setsp(*sp);
psc->ProtectA();
delete psc;
psc = NULL;
delete sp;
sp = NULL;
system("pause");
}