版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FAKER_0X0/article/details/89786847
1 数据的发送和接收
报文协议
CSocketProtocol.h
#pragma once
//定义报文协议类,纯虚函数作为面向抽象类(接口)编程
class CSocketProtocol
{
public: //报文的初始化、发送、接收、销毁函数,返回值为0则函数正常运行
virtual int cltSocketInit() = 0;
virtual int cltSocketSend(unsigned char *buf, int buflen) = 0; //报文数组首地址,报文长度
virtual int cltSocketRev(unsigned char *buf, int *buflen) = 0; //重要:报文接收长度为指针
virtual int cltSocketDestory() = 0;
};
产商入围
CSckFactoryImp1.h
#pragma once
#include "CSocketProtocol.h"
//第一个产商入围,类继承,虚函数重写
class CSckFactoryImp1 : public CSocketProtocol
{
public:
virtual int cltSocketInit();
virtual int cltSocketSend(unsigned char *buf, int buflen);
virtual int cltSocketRev(unsigned char *buf, int *buflen);
virtual int cltSocketDestory();
private:
unsigned char *p; //私有变量,p为一个内存中转站,存储发送的内容,把内容送给接收
int len;
};
CSckFactoryImp2.h
#pragma once
#include "CSocketProtocol.h"
//第一个产商入围,类继承,虚函数重写
class CSckFactoryImp2 : public CSocketProtocol
{
public:
virtual int cltSocketInit();
virtual int cltSocketSend(unsigned char *buf, int buflen);
virtual int cltSocketRev(unsigned char *buf, int *buflen);
virtual int cltSocketDestory();
private:
unsigned char *p; //私有变量,p为一个内存中转站,存储发送的内容,把内容送给接收
int len;
};
CSckFactoryImp1.cpp
//第一个产商的函数定义
#include <iostream>
#include "CSckFactoryImp1.h"
using namespace std;
int CSckFactoryImp1::cltSocketInit()
{
p = NULL;
len = 0;
return 0;
}
int CSckFactoryImp1::cltSocketSend(unsigned char *buf, int buflen)
{
p = new unsigned char[buflen];
if (p == NULL) { //看内存分配是否成功
return -1;
}
memcpy(p, buf, buflen); //内存拷贝函数,从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中
len = buflen;
return 0;
}
//Rev函数中,buflen为什么要通过指针来操作?
//总结:需要保证修改形参能直接修改实参
int CSckFactoryImp1::cltSocketRev(unsigned char *buf, int *buflen)
{
if (buf == NULL || buflen == NULL) {
return -1;
}
*buflen = len;
memcpy(buf, p, len);
return 0;
}
int CSckFactoryImp1::cltSocketDestory()
{
if (p != NULL) {
delete[] p;
p = NULL;
len = 0;
}
return 0;
}
CSckFactoryImp2.cpp
//第二个产商的函数定义
#include <iostream>
#include "CSckFactoryImp2.h"
using namespace std;
int CSckFactoryImp2::cltSocketInit()
{
p = NULL;
len = 0;
return 0;
}
int CSckFactoryImp2::cltSocketSend(unsigned char *buf, int buflen)
{
p = new unsigned char[buflen];
if (p == NULL) { //看内存分配是否成功
return -1;
}
memcpy(p, buf, buflen); //内存拷贝函数,从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中
len = buflen;
return 0;
}
//Rev函数中,buflen为什么要通过指针来操作?
//总结:需要保证修改形参能直接修改实参
int CSckFactoryImp2::cltSocketRev(unsigned char *buf, int *buflen)
{
if (buf == NULL || buflen == NULL) {
return -1;
}
*buflen = len;
memcpy(buf, p, len);
return 0;
}
int CSckFactoryImp2::cltSocketDestory()
{
if (p != NULL) {
delete[] p;
p = NULL;
len = 0;
}
return 0;
}
测试框架
mainclass01.cpp
//信息系统框架集成第三方产品案例
//框架:数据的发送和接收
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"
using namespace std;
//框架
//报文发送与接收函数 5个参数
//ret 存储接口->底层接口 判断函数是否正常运行
//调用报文协议类的4个函数
int sckSendAndRec01(CSocketProtocol *sp, unsigned char *in, int inlen, unsigned char *out, int *outlen)
{
int ret = 0;
ret = sp->cltSocketInit();
if (ret != 0) {
goto End;
}
ret = sp->cltSocketSend(in, inlen);
if (ret != 0) {
goto End;
}
ret = sp->cltSocketRev(out, outlen);
if (ret != 0) {
goto End;
}
End:
ret = sp->cltSocketDestory();
return 0;
}
//写一个框架
int main01()
{
int ret = 0;
unsigned char in[4096]; //定义输入输出的数据长度不超过4K
int inlen;
unsigned char out[4096];
int outlen = 0;
strcpy((char *)in, "aadddddddddaaaaaaaa"); //要输入的内容赋给in,强制转换类型
inlen = 9;
//接入的产商
//CSocketProtocol *sp = new CSckFactoryImp1;
CSocketProtocol *sp = new CSckFactoryImp2;
ret = sckSendAndRec01(sp, in, inlen, out, &outlen);
//cout << out << outlen << endl;
if (ret != 0) {
cout << "func sckSendAndRec err:" << ret << endl;
return ret;
}
delete sp;
return ret;
}
2 数据的加密发送和解密接收(全局函数方式)
加解密协议
CEncDesProtocol.h
#pragma once
//实现加密和解密,让第三方实现的接口
class CEncDesProtocol
{
public:
CEncDesProtocol()
{
;
}
virtual ~CEncDesProtocol() //虚析构函数,通过父类对象释放子类对象内存
{
;
}
virtual int encData(unsigned char *plain, int plainlen, unsigned char *crypt, int *cryptlen) = 0;
virtual int decData(unsigned char *crypt, int cryptlen, unsigned char *plain, int *plainlen) = 0;
//纯虚函数作接口
//明文和密文的转换,注意最后的长度参数都为指针
};
des.h
#pragma once
class des
{
public:
//用户使用的函数 加密
static int DesEnc(unsigned char *pInData,
int nInDataLen,
unsigned char *poutData,
int *poutDataLen);
// 用户使用函数 des 解密
static int DesDec(unsigned char *pInData,
int nInDataLen,
unsigned char *poutData,
int *poutDataLen);
};
华为产商加解密
HwEncDec.h
#pragma once
//第三方实现,华为的加解密
#include "CEncDesProtocol.h"
class HwEncDec : public CEncDesProtocol
{
public: //报文的初始化、发送、接收、销毁函数,返回值为0则函数正常运行
virtual int encData(unsigned char *plain, int plainlen, unsigned char *crypt, int *cryptlen);
virtual int decData(unsigned char *crypt, int cryptlen, unsigned char *plain, int *plainlen);
};
des.cpp
#include "des.h"
//emmm..这里是只有一个框架而没有加密解密算法吧..
//还有两个函数为么参数一样..?
//用户使用的函数 加密
int des::DesEnc(unsigned char *pInData, int nInDataLen, unsigned char *poutData, int*poutDataLen)
{
return 0;
}
// 用户使用函数 des 解密
int des::DesDec(unsigned char *pInData, int nInDataLen, unsigned char *poutData, int*poutDataLen)
{
return 0;
}
HwEncDec.cpp
#include <iostream>
#include "HwEncDec.h"
#include "des.h"
using namespace std;
int HwEncDec::encData(unsigned char *plain, int plainlen, unsigned char *crypt, int *cryptlen)
{
int ret = 0;
//此处调用需要定义为静态成员函数
ret = des::DesEnc(plain, plainlen, crypt, cryptlen); //调用解密的用户使用函数
if (ret != 0) {
cout << "func DesEnc err.." << ret << endl;
return ret;
}
return ret;
}
int HwEncDec::decData(unsigned char *crypt, int cryptlen, unsigned char *plain, int *plainlen)
{
int ret = 0;
ret = des::DesDec(crypt, cryptlen, plain, plainlen);
if (ret != 0) {
cout << "func DesDec err.." << ret << endl;
return ret;
}
return ret;
}
测试框架
mainclass02.cpp
//信息系统框架集成第三方产品案例
//框架:数据的加密发送和解密接收
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"
#include "des.h"
#include "HwEncDec.h"
#include "CEncDesProtocol.h"
using namespace std;
//框架
//报文发送与接收函数 5个参数
//ret 存储接口->底层接口 判断函数是否正常运行
//调用报文协议类的4个函数
int sckSendAndRec02(CSocketProtocol *sp, unsigned char *in, int inlen, unsigned char *out, int *outlen)
{
int ret = 0;
ret = sp->cltSocketInit();
if (ret != 0) {
goto End;
}
ret = sp->cltSocketSend(in, inlen);
if (ret != 0) {
goto End;
}
ret = sp->cltSocketRev(out, outlen);
if (ret != 0) {
goto End;
}
End:
ret = sp->cltSocketDestory();
return 0;
}
//报文发送与接收函数加上加密和解密
int sckSendAndRecDecEnc02(CSocketProtocol *sp, CEncDesProtocol *ed, unsigned char *in, int inlen, unsigned char *out, int *outlen)
{
int ret = 0;
unsigned char data[4096]; //定义密文段存储空间
int datalen = 0; //定义密文段长度
ret = sp->cltSocketInit();
if (ret != 0) {
goto End;
}
ret = ed->encData(in, inlen, data, &datalen);// 数据发送之前要加密,发送的是密文
if (ret != 0) {
goto End;
}
ret = sp->cltSocketSend(data, datalen);// 发送密文
if (ret != 0) {
goto End;
}
ret = sp->cltSocketRev(data, &datalen);// 收到的是密文,要进行解密
if (ret != 0) {
goto End;
}
ret = ed->decData(data, datalen, out, outlen);//解密
if (ret != 0) {
goto End;
}
End:
ret = sp->cltSocketDestory();
return 0;
}
//写一个框架
int main02()
{
int ret = 0;
unsigned char in[4096]; //定义输入输出的数据长度不超过4K
int inlen;
unsigned char out[4096];
int outlen = 0;
strcpy((char *)in, "aadddddddddaaaaaaaa"); //要输入的内容赋给in,强制转换类型
inlen = 9;
//接入的产商
//CSocketProtocol *sp = new CSckFactoryImp1;
CSocketProtocol *sp = new CSckFactoryImp2;
ret = sckSendAndRec02(sp, in, inlen, out, &outlen);
//cout << out << outlen << endl;
if (ret != 0) {
cout << "func sckSendAndRec err:" << ret << endl;
return ret;
}
//delete sp;
//加密和解密
CEncDesProtocol *ed = new HwEncDec;
ret = sckSendAndRecDecEnc02(sp, ed, in, inlen, out, &outlen);
if (ret != 0) {
cout << "func sckSendAndRecDecEnc err:" << ret << endl;
return ret;
}
delete sp;
delete ed;
return ret;
}
3 数据的加密发送和解密接收(类成员函数方式)
测试框架
//信息系统框架集成第三方产品案例
//框架:数据的加密发送和解密接收
//将报文发送与接收函数封装成成员函数,可以用多继承或者组合的方式
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"
#include "des.h"
#include "HwEncDec.h"
#include "CEncDesProtocol.h"
using namespace std;
//多继承方式
//class MainOp : public CSocketProtocol, public CEncDesProtocol
//{
//
//};
//组合方式
class MainOp
{
public:
MainOp()
{
sp = NULL;
ed = NULL;
}
MainOp(CSocketProtocol *sp, CEncDesProtocol *ed)//注意参数都为组合的类对象的指针
{
this->sp = sp;
this->ed = ed;
}
void setSp(CSocketProtocol *sp) //注入?
{
this->sp = sp;
}
void setEd(CEncDesProtocol *ed)
{
this->ed = ed;
}
public: //将报文发送与接收函数封装成成员函数
int sckSendAndRec(unsigned char *in, int inlen, unsigned char *out, int *outlen)
{
int ret = 0;
ret = sp->cltSocketInit();
if (ret != 0) {
goto End;
}
ret = sp->cltSocketSend(in, inlen);
if (ret != 0) {
goto End;
}
//cout << "in: " << in << " inlen: " << inlen << endl; //输出测试
ret = sp->cltSocketRev(out, outlen);
if (ret != 0) {
goto End;
}
cout << "out: "; //输出测试
cout.write((char *)out, *outlen);
cout<< " outlen: " << *outlen << endl;
End:
ret = sp->cltSocketDestory();
return 0;
}
//报文发送与接收函数加上加密和解密
int sckSendAndRecDecEnc(unsigned char *in, int inlen, unsigned char *out, int *outlen)
{
int ret = 0;
unsigned char data[4096]; //定义密文段存储空间
int datalen = 0; //定义密文段长度
ret = sp->cltSocketInit();
if (ret != 0) {
goto End;
}
ret = ed->encData(in, inlen, data, &datalen);// 数据发送之前要加密,发送的是密文
if (ret != 0) {
goto End;
}
ret = sp->cltSocketSend(data, datalen);// 发送密文
if (ret != 0) {
goto End;
}
ret = sp->cltSocketRev(data, &datalen);// 收到的是密文,要进行解密
if (ret != 0) {
goto End;
}
ret = ed->decData(data, datalen, out, outlen);//解密
if (ret != 0) {
goto End;
}
End:
ret = sp->cltSocketDestory();
return 0;
}
private:
CSocketProtocol *sp; //组合对象
CEncDesProtocol *ed;
};
//写一个框架
int main()
{
int ret = 0;
unsigned char in[4096]; //定义输入输出的数据长度不超过4K
int inlen;
unsigned char out[4096];
int outlen = 0;
strcpy((char *)in, "aadddddddddaaaaaaaa"); //要输入的内容赋给in,强制转换类型
inlen = 9;
//接入的产商
//CSocketProtocol *sp = new CSckFactoryImp1;
CSocketProtocol *sp = new CSckFactoryImp2;
CEncDesProtocol *ed = new HwEncDec;
MainOp *op1 = new MainOp;
op1->setSp(sp); //把对象指针注入类
op1->setEd(ed);
//MainOp *op1 = new MainOp(sp, ed); //有参构造函数初始化
ret = op1->sckSendAndRec(in, inlen, out, &outlen);
if (ret != 0) {
cout << "func sckSendAndRec err:" << ret << endl;
return ret;
}
ret = op1->sckSendAndRecDecEnc(in, inlen, out, &outlen);
if (ret != 0) {
cout << "func sckSendAndRecDecEnc err:" << ret << endl;
return ret;
}
delete sp;
delete ed;
delete op1;
return ret;
}