设计模式:简单工厂模式

版权声明:所有版权归作者她的吻让他,转载请标明出处. https://blog.csdn.net/qq_37059136/article/details/86479324

前言:

设计模式有无数讲解,但是设计模式却并不是一个独立的知识点,设计模式涉及的几个原则应该成为下意识规范

只有将设计模式放到特定的例子中才能体现模式的便捷及功能

背景:

下面我来模拟下简单工厂的需求背景:

对称加解密算法使用同一个密钥进行加密和解密操作,目前使用较普遍的有AES、3DES、DES、SM1(国密)等。

本次仅需实现AES和3DES算法即可,但代码设计中需考虑后续对对称加解密算法中的其他算法实现的扩展。

提供的功能包括字符串加密、字符串解密、文件加密、文件解密。

解读:

可以看到,关于加解密,有许多算法,比如需要实现的AES跟3DES,他们同属于加解密算法且都提供字符串加解密和文件加解密

那么其实他们的功能是相同的,准确的说,他们所提供给外界使用的方法应该是相同的

由于算法种类很多,诸如AES、3DES、DES、SM1(国密)等,我们不可能让使用者操作这么多类,因为类越多就越复杂,我们可以使用简单工厂来实现算法,且算法可新增(只要算法提供的功能不变)

简单工厂:

//1.基类提供虚函数方法,不实现
//2.多个具体方法类重写基类提供的方法
//3.具体方法子类仅有细小差别
//4.通过使用基类指针指向派生类对象的方法,通过具体差异选择创建具体的方法类

/*所谓简单工厂模式,是一种实例化对象的方式,只要输入需要实例化对象的名字,
就可以通过工厂对象的相应工厂函数来制造你需要的对象。

简单工厂模式的核心是,对于一个父类的多个继承子类,
工厂对象的工厂函数根据用户输入,
自动new出一个子类对象并返回其父类的指针,
这样利用父类的指针执行父类的虚函数,
就可以动态绑定子类的重写函数,
从而实现多态。*/

以上是本人结合资料对简单工厂的总结

简单工厂所需:

一个基类,提供加解密的实际操作方法(虚函数)

若干个继承基类的子类,这些子类实现基类中的虚函数放法,以便实例化

一个工厂,提供一个可用于创建特定子类的函数(子类创建类型由输入控制)

示例:

资源视图:

先展示基类

///*****************************************/
//                           
//说明: CCodeBase头文件定义
//作者: C006               
//
///*****************************************/
#pragma once
#include <string>
using namespace std;


#define ecb 1
#define cbc 2
#define inistr "000000"

#define error_empty  "1or2_empty"
#define error_alg    "Pattern_mismatch"
#define error_mode   "mode_error"
#define error_offset "offset_eeeor"

class CCodeBase
{
public:
	virtual string StringEnc(string InputClear,string InputKey,int iMode = cbc,string InputOffset = inistr) = 0;
	virtual string StringDec(string InputCiphertext,string InputKey,int iMode = cbc,string InputOffset = inistr) = 0;
	virtual string FileEnc(string InputClear,string InputKey,int iMode = cbc,string InputOffset = inistr) = 0;
	virtual string FileDec(string InputCiphertext,string InputKey,int iMode = cbc,string InputOffset = inistr) = 0;
};

该基类仅申明四个纯虚函数

下面看两个子类

///*****************************************/
//                           
//说明: CAES头文件定义
//作者: C006               
//
///*****************************************/
#pragma once
#include "CodeBase.h"


class CAES:public CCodeBase
{
public:
	CAES();
	~CAES();

public:
	string StringEnc(string InputClear,string InputKey,int iMode = cbc,string InputOffset = inistr);
	string StringDec(string InputCiphertext,string InputKey,int iMode = cbc,string InputOffset = inistr);
	string FileEnc(string InputClear,string InputKey,int iMode = cbc,string InputOffset = inistr);
	string FileDec(string InputCiphertext,string InputKey,int iMode = cbc,string InputOffset = inistr);

};
#include "aes.h"
#include <string>
#include <windows.h>
#include <tchar.h>
using namespace std;

/*******************************************************************************
函数名称:StringEnc
函数说明:字符串加密
输入参数:
string InputClear             :输入明文
string InputKey               :输入密钥
int iMode                     :模式(CBC或ECB)
string InputOffset            :偏移量
输出参数:无
返 回 值:string
作    者:C006
修改日期:2018-1-11
修改说明:基础版本
*******************************************************************************/
string CAES::StringEnc(string InputClear,string InputKey,int iMode ,string InputOffset)
{
	if (InputClear.length() == 0&& InputKey.length() == 0)
	{
		OutputDebugString(_T("明文或密钥为空"));
		return error_empty;
	}
	OutputDebugString(_T("匹配正确,开始加密..."));
	string strOutputString;
	if (ecb == iMode)  //ecb模式
	{
		OutputDebugString(_T("启用aes-ecb模式加密..."));
		string strTmp;
		int iLengthInputClear = InputClear.length();
		int iLengthInputKey = InputKey.length();
		int iMaxLen = 0;
		int iMinLen = 0;
		

		if (iLengthInputClear >= iLengthInputKey)
		{
			iMaxLen = iLengthInputClear;
			iMinLen = iLengthInputKey;
			int j = 0;
			for (int i = 0;i < iMaxLen;i++)
			{
				if (j < iMinLen)
				{
					strOutputString+= InputClear[i];
					strOutputString+= InputKey[j];
					j++;
				}
				else
				{
					strOutputString+=InputClear[i];
				}
			}
			string stroutput;
			stroutput+="AES-";
			stroutput+="ECB-";
			stroutput+=strOutputString;
			OutputDebugString(_T("加密成功"));
			return stroutput;
		}
		else
		{
			iMaxLen = iLengthInputKey;
			iMinLen = iLengthInputClear;
			int j = 0;
			for (int i = 0;i < iMaxLen;i++)
			{
				if (j < iMinLen)
				{
					strOutputString+= InputClear[i];
					strOutputString+= InputKey[j];
					j++;
				}
				else
				{
					strOutputString+=InputKey[i];
				}
			}
			string stroutput;
			stroutput+="AES-";
			stroutput+="ECB-";
			stroutput+=strOutputString;
			OutputDebugString(_T("加密成功"));
			return stroutput;
		}

	}
	else if (cbc == iMode)
	{
		OutputDebugString(_T("启用aes-cbc加密..."));
		int iTmpLen = InputOffset.length();
		if (8 < iTmpLen)
		{
			InputOffset.erase(8);
		}
		else if (8 > iTmpLen)
		{
			string strTmp;
			int j = 8 - iTmpLen;
			for (int i = 0;i < j;i++)
			{
				strTmp+="0";
			}
			InputOffset+=strTmp;

		}
	
			string strTmp;
			int iLengthInputClear = InputClear.length();
			int iLengthInputKey = InputKey.length();
			int iMaxLen = 0;
			int iMinLen = 0;


			if (iLengthInputClear >= iLengthInputKey)
			{
				iMaxLen = iLengthInputClear;
				iMinLen = iLengthInputKey;
				int j = 0;
				for (int i = 0;i < iMaxLen;i++)
				{
					if (j < iMinLen)
					{
						strOutputString+= InputClear[i];
						strOutputString+= InputKey[j];
						j++;
					}
					else
					{
						strOutputString+=InputClear[i];
					}
				}
				string stroutput;
				stroutput+="AES-";
				stroutput+="CBC-";
				stroutput+=InputOffset;
				stroutput+="-";
				stroutput+=strOutputString;
				OutputDebugString(_T("加密成功"));
				return stroutput;
			}
			else
			{
				iMaxLen = iLengthInputKey;
				iMinLen = iLengthInputClear;
				int j = 0;
				for (int i = 0;i < iMaxLen;i++)
				{
					if (j < iMinLen)
					{
						strOutputString+= InputClear[i];
						strOutputString+= InputKey[j];
						j++;
					}
					else
					{
						strOutputString+=InputKey[i];
					}
				}
				string stroutput;
				stroutput+="AES-";
				stroutput+="CBC-";
				stroutput+=InputOffset;
				stroutput+="-";
				stroutput+=strOutputString;
				OutputDebugString(_T("加密成功"));
				return stroutput;
			}		
	}
	OutputDebugString(_T("加密失败"));
}


/*******************************************************************************
函数名称:StringDec
函数说明:字符串解密
输入参数:
string InputCiphertext             :输入密文
string InputKey                    :输入密钥
int iMode                          :模式(CBC或ECB)
string InputOffset                 :偏移量
输出参数:无
返 回 值:string
作    者:C006
修改日期:2018-1-11
修改说明:基础版本
*******************************************************************************/
string CAES::StringDec(string InputCiphertext,string InputKey,int iMode,string InputOffset)
{
	if (InputCiphertext.length() == 0&& InputKey.length() == 0)
	{
		OutputDebugString(_T("密文或密钥为空"));
		return error_empty;
	}
	OutputDebugString(_T("匹配正确,开始解密..."));
	int ipos = 10001;
	if (ecb == iMode)
	{
		ipos = InputCiphertext.find_first_of("AES");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到算法"));
			return error_alg;  //算法错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_first_of("ECB");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到模式"));
			return error_mode; //模式错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_last_of("-");
		string strTmp = InputCiphertext.substr(ipos+1);
		//解密密文
		OutputDebugString(_T("开始解密..."));
		int iOdd = 0;
		string strOutString;
		for (;iOdd < strTmp.length();iOdd++)
		{
			strOutString+=strTmp[iOdd];
			iOdd++;
		}
        OutputDebugString(_T("解密成功"));
		return strOutString;
	}
	else if (cbc == iMode)
	{
		ipos = InputCiphertext.find_first_of("AES");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到算法"));
			return error_alg;  //算法错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_first_of("CBC");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到模式"));
			return error_mode; //模式错误
		}
		//判断偏移量
		ipos = 0;
		ipos = InputCiphertext.find_first_of(InputOffset);
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到偏移量"));
			return error_offset;

		}

		ipos = 0;
		ipos = InputCiphertext.find_last_of("-");
		string strTmp = InputCiphertext.substr(ipos+1);
		//解密密文
		OutputDebugString(_T("开始解密..."));
		int iOdd = 0;
		string strOutString;
		for (;iOdd < strTmp.length();iOdd++)
		{
			strOutString+=strTmp[iOdd];
			iOdd++;
		}
		OutputDebugString(_T("解密成功"));
		return strOutString;
	}
	
}


/*******************************************************************************
函数名称:FileDec
函数说明:文件解密
输入参数:
string InputCiphertext             :输入密文
string InputKey                    :输入密钥
int iMode                          :模式(CBC或ECB)
string InputOffset                 :偏移量
输出参数:无
返 回 值:string
作    者:C006
修改日期:2018-1-11
修改说明:基础版本
*******************************************************************************/
string CAES::FileDec(std::string InputCiphertext, std::string InputKey, int iMode, std::string InputOffset)
{
	if (InputCiphertext.length() == 0&& InputKey.length() == 0)
	{
		OutputDebugString(_T("密文或密钥为空"));
		return error_empty;
	}
	OutputDebugString(_T("匹配正确,开始解密..."));
	int ipos = 10001;
	if (ecb == iMode)
	{
		ipos = InputCiphertext.find_first_of("AES");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到算法"));
			return error_alg;  //算法错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_first_of("ECB");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到模式"));
			return error_mode; //模式错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_last_of("-");
		//D:/test/3DES-ECB-12345678-aa.txt
		//解密密文
		OutputDebugString(_T("开始解密..."));
		int ipos2 = InputCiphertext.find_last_of("/");
		int ilen1 = ipos- ipos2;
		InputCiphertext.replace(ipos2,ilen1,"");
		OutputDebugString(_T("解密成功"));
		return InputCiphertext;
	}
	else if (cbc == iMode)
	{
		ipos = InputCiphertext.find_first_of("AES");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到算法"));
			return error_alg;  //算法错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_first_of("CBC");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到模式"));
			return error_mode; //模式错误
		}
		//判断偏移量
		ipos = 0;
		ipos = InputCiphertext.find_first_of(InputOffset);
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到偏移量"));
			return error_offset;

		}

		ipos = 0;
		ipos = InputCiphertext.find_last_of("-");
		//D:/test/3DES-ECB-12345678-aa.txt
		//解密密文
		OutputDebugString(_T("开始解密..."));
		int ipos2 = InputCiphertext.find_last_of("/");
		int ilen1 = ipos- ipos2;
		InputCiphertext.replace(ipos2,ilen1,"");
		OutputDebugString(_T("解密成功"));
		return InputCiphertext;
	}
}


/*******************************************************************************
函数名称:FileEnc
函数说明:文件加密
输入参数:
string InputClear             :输入文件路径
string InputKey               :输入密钥
int iMode                     :模式(CBC或ECB)
string InputOffset            :偏移量
输出参数:无
返 回 值:string
作    者:C006
修改日期:2018-1-11
修改说明:基础版本
*******************************************************************************/
string CAES::FileEnc(string InputClear,string InputKey,int iMode /* = cbc */,string InputOffset /* = inistr */)
{
	if (InputClear.length() == 0&& InputKey.length() == 0)
	{
		OutputDebugString(_T("文件路径或密钥为空"));
		return error_empty;
	}
	OutputDebugString(_T("匹配正确,开始加密..."));
	string strOutputString;
	if (ecb == iMode)  //ecb模式
	{
		OutputDebugString(_T("启用aes-ecb模式加密..."));
		string strTmp;
		int iLengthInputClear = InputClear.length();
		int iLengthInputKey = InputKey.length();
		int iMaxLen = 0;
		int iMinLen = 0;

		string stroutput;

		stroutput+="AES-";
		stroutput+="ECB-";
		stroutput+=InputKey;
		stroutput+="-";
		OutputDebugString(_T("加密成功"));
		int ipos = InputClear.find_last_of("/");
		InputClear.insert(ipos+1,stroutput);
		return InputClear;
		

	}
	else if (cbc == iMode)
	{
		OutputDebugString(_T("启用aes-cbc加密..."));
		int iTmpLen = InputOffset.length();
		if (8 < iTmpLen)
		{
			InputOffset.erase(8);
		}
		else if (8 > iTmpLen)
		{
			string strTmp;
			int j = 8 - iTmpLen;
			for (int i = 0;i < j;i++)
			{
				strTmp+="0";
			}
			InputOffset+=strTmp;

		}
		string stroutput;

		stroutput+="AES-";
		stroutput+="CBC-";
		stroutput+=InputOffset;
		stroutput+="-";
		stroutput+=InputKey;
		stroutput+="-";
		OutputDebugString(_T("加密成功"));
		int ipos = InputClear.find_last_of("/");
		InputClear.insert(ipos+1,stroutput);
		return InputClear;

			
	}
	OutputDebugString(_T("加密失败"));
}

CAES::CAES()
{
	;
}
CAES::~CAES()
{
	;
}
///*****************************************/
//                           
//说明: C3DES头文件定义
//作者: C006               
//
///*****************************************/
#pragma once
#include "CodeBase.h"

class C3DES:public CCodeBase
{
public:
	C3DES();
	~C3DES();

public:
	string StringEnc(string InputClear,string InputKey,int iMode = cbc,string InputOffset = inistr);
	string StringDec(string InputCiphertext,string InputKey,int iMode = cbc,string InputOffset = inistr);
	string FileEnc(string InputClear,string InputKey,int iMode = cbc,string InputOffset = inistr);
	string FileDec(string InputCiphertext,string InputKey,int iMode = cbc,string InputOffset = inistr);
};
#include "3des.h"
#include <string>
#include <Windows.h>
#include <tchar.h>
using namespace std;


/*******************************************************************************
函数名称:StringEnc
函数说明:字符串加密
输入参数:
string InputClear             :输入明文
string InputKey               :输入密钥
int iMode                     :模式(CBC或ECB)
string InputOffset            :偏移量
输出参数:无
返 回 值:string
作    者:C006
修改日期:2018-1-11
修改说明:基础版本
*******************************************************************************/
string C3DES::StringEnc(string InputClear,string InputKey,int iMode,string InputOffset)
{
	if (InputClear.length() == 0&& InputKey.length() == 0)
	{
		OutputDebugString(_T("明文或密钥为空"));
		return error_empty;
	}
	OutputDebugString(_T("匹配正确,开始加密..."));
	string strOutputString;
	if (ecb == iMode)  //ecb模式
	{
		OutputDebugString(_T("启用3des-ecb模式加密..."));
		string strTmp;
		int iLengthInputClear = InputClear.length();
		int iLengthInputKey = InputKey.length();
		int iMaxLen = 0;
		int iMinLen = 0;


		if (iLengthInputClear >= iLengthInputKey)
		{
			iMaxLen = iLengthInputClear;
			iMinLen = iLengthInputKey;
			int j = 0;
			for (int i = 0;i < iMaxLen;i++)
			{
				if (j < iMinLen)
				{
					strOutputString+= InputClear[i];
					strOutputString+= InputKey[j];
					j++;
				}
				else
				{
					strOutputString+=InputClear[i];
				}
			}
			string stroutput;
			stroutput+="3DES-";
			stroutput+="ECB-";
			stroutput+=strOutputString;
			OutputDebugString(_T("加密成功"));
			return stroutput;
		}
		else
		{
			iMaxLen = iLengthInputKey;
			iMinLen = iLengthInputClear;
			int j = 0;
			for (int i = 0;i < iMaxLen;i++)
			{
				if (j < iMinLen)
				{
					strOutputString+= InputClear[i];
					strOutputString+= InputKey[j];
					j++;
				}
				else
				{
					strOutputString+=InputKey[i];
				}
			}
			string stroutput;
			stroutput+="3DES-";
			stroutput+="ECB-";
			stroutput+=strOutputString;
			OutputDebugString(_T("加密成功"));
			return stroutput;
		}

	}
	else if (cbc == iMode)
	{
		OutputDebugString(_T("启用3des-cbc加密..."));
		int iTmpLen = InputOffset.length();
		if (8 < iTmpLen)
		{
			InputOffset.erase(8);
		}
		else if (8 > iTmpLen)
		{
			string strTmp;
			int j = 8 - iTmpLen;
			for (int i = 0;i < j;i++)
			{
				strTmp+="0";
			}
			InputOffset+=strTmp;

		}

		string strTmp;
		int iLengthInputClear = InputClear.length();
		int iLengthInputKey = InputKey.length();
		int iMaxLen = 0;
		int iMinLen = 0;


		if (iLengthInputClear >= iLengthInputKey)
		{
			iMaxLen = iLengthInputClear;
			iMinLen = iLengthInputKey;
			int j = 0;
			for (int i = 0;i < iMaxLen;i++)
			{
				if (j < iMinLen)
				{
					strOutputString+= InputClear[i];
					strOutputString+= InputKey[j];
					j++;
				}
				else
				{
					strOutputString+=InputClear[i];
				}
			}
			string stroutput;
			stroutput+="3DES-";
			stroutput+="CBC-";
			stroutput+=InputOffset;
			stroutput+="-";
			stroutput+=strOutputString;
			OutputDebugString(_T("加密成功"));
			return stroutput;
		}
		else
		{
			iMaxLen = iLengthInputKey;
			iMinLen = iLengthInputClear;
			int j = 0;
			for (int i = 0;i < iMaxLen;i++)
			{
				if (j < iMinLen)
				{
					strOutputString+= InputClear[i];
					strOutputString+= InputKey[j];
					j++;
				}
				else
				{
					strOutputString+=InputKey[i];
				}
			}
			string stroutput;
			stroutput+="3DES-";
			stroutput+="CBC-";
			stroutput+=InputOffset;
			stroutput+="-";
			stroutput+=strOutputString;
			OutputDebugString(_T("加密成功"));
			return stroutput;
		}		
	}
	OutputDebugString(_T("加密失败"));
}


/*******************************************************************************
函数名称:StringDec
函数说明:字符串加密
输入参数:
string InputCiphertext             :输入密文
string InputKey                    :输入密钥
int iMode                          :模式(CBC或ECB)
string InputOffset                 :偏移量
输出参数:无
返 回 值:string
作    者:C006
修改日期:2018-1-11
修改说明:基础版本
*******************************************************************************/
string C3DES::StringDec(string InputCiphertext,string InputKey,int iMode,string InputOffset)
{
	if (InputCiphertext.length() == 0&& InputKey.length() == 0)
	{
		OutputDebugString(_T("密文或密钥为空"));
		return error_empty;
	}
	OutputDebugString(_T("匹配正确,开始解密..."));
	int ipos = 10001;
	if (ecb == iMode)
	{
		ipos = InputCiphertext.find_first_of("3DES");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到算法"));
			return error_alg;  //算法错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_first_of("ECB");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到模式"));
			return error_mode; //模式错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_last_of("-");
		string strTmp = InputCiphertext.substr(ipos+1);
		//解密密文
		OutputDebugString(_T("开始解密..."));
		int iOdd = 0;
		string strOutString;
		for (;iOdd < strTmp.length();iOdd++)
		{
			strOutString+=strTmp[iOdd];
			iOdd++;
		}
		OutputDebugString(_T("解密成功"));
		return strOutString;
	}
	else if (cbc == iMode)
	{
		ipos = InputCiphertext.find_first_of("3DES");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到算法"));
			return error_alg;  //算法错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_first_of("CBC");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到模式"));
			return error_mode; //模式错误
		}
		//判断偏移量
		ipos = 0;
		ipos = InputCiphertext.find_first_of(InputOffset);
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到偏移量"));
			return error_offset;

		}

		ipos = 0;
		ipos = InputCiphertext.find_last_of("-");
		string strTmp = InputCiphertext.substr(ipos+1);
		//解密密文
		OutputDebugString(_T("开始解密..."));
		int iOdd = 0;
		string strOutString;
		for (;iOdd < strTmp.length();iOdd++)
		{
			strOutString+=strTmp[iOdd];
			iOdd++;
		}
		OutputDebugString(_T("解密成功"));
		return strOutString;
	}
}

/*******************************************************************************
函数名称:FileDec
函数说明:文件解密
输入参数:
string InputCiphertext             :输入密文
string InputKey                    :输入密钥
int iMode                          :模式(CBC或ECB)
string InputOffset                 :偏移量
输出参数:无
返 回 值:string
作    者:C006
修改日期:2018-1-11
修改说明:基础版本
*******************************************************************************/
string C3DES::FileDec(std::string InputCiphertext, std::string InputKey, int iMode, std::string InputOffset)
{
	if (InputCiphertext.length() == 0&& InputKey.length() == 0)
	{
		OutputDebugString(_T("密文或密钥为空"));
		return error_empty;
	}
	OutputDebugString(_T("匹配正确,开始解密..."));
	int ipos = 10001;
	if (ecb == iMode)
	{
		ipos = InputCiphertext.find_first_of("3DES");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到算法"));
			return error_alg;  //算法错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_first_of("ECB");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到模式"));
			return error_mode; //模式错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_last_of("-");
		//D:/test/3DES-ECB-12345678-aa.txt
		//解密密文
		OutputDebugString(_T("开始解密..."));
		int ipos2 = InputCiphertext.find_last_of("/");
		int ilen1 = ipos- ipos2;
		InputCiphertext.replace(ipos2,ilen1,"");
		OutputDebugString(_T("解密成功"));
		return InputCiphertext;
	}
	else if (cbc == iMode)
	{
		ipos = InputCiphertext.find_first_of("3DES");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到算法"));
			return error_alg;  //算法错误
		}
		ipos = 0;
		ipos = InputCiphertext.find_first_of("CBC");
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到模式"));
			return error_mode; //模式错误
		}
		//判断偏移量
		ipos = 0;
		ipos = InputCiphertext.find_first_of(InputOffset);
		if (ipos == string::npos)
		{
			OutputDebugString(_T("密文中未检测到偏移量"));
			return error_offset;

		}

		ipos = 0;
		ipos = InputCiphertext.find_last_of("-");
		//D:/test/3DES-ECB-12345678-aa.txt
		//解密密文
		OutputDebugString(_T("开始解密..."));
		int ipos2 = InputCiphertext.find_last_of("/");
		int ilen1 = ipos- ipos2;
		InputCiphertext.replace(ipos2,ilen1,"");
		OutputDebugString(_T("解密成功"));
		return InputCiphertext;
	}
}


/*******************************************************************************
函数名称:FileEnc
函数说明:文件加密
输入参数:
string InputClear             :输入文件路径
string InputKey               :输入密钥
int iMode                     :模式(CBC或ECB)
string InputOffset            :偏移量
输出参数:无
返 回 值:string
作    者:C006
修改日期:2018-1-11
修改说明:基础版本
*******************************************************************************/
string C3DES::FileEnc(string InputClear,string InputKey,int iMode /* = cbc */,string InputOffset /* = inistr */)
{
	if (InputClear.length() == 0&& InputKey.length() == 0)
	{
		OutputDebugString(_T("文件路径或密钥为空"));
		return error_empty;
	}
	OutputDebugString(_T("匹配正确,开始加密..."));
	string strOutputString;
	if (ecb == iMode)  //ecb模式
	{
		OutputDebugString(_T("启用3des-ecb模式加密..."));
		string strTmp;
		int iLengthInputClear = InputClear.length();
		int iLengthInputKey = InputKey.length();
		int iMaxLen = 0;
		int iMinLen = 0;

		string stroutput;

		stroutput+="3DES-";
		stroutput+="ECB-";
		stroutput+=InputKey;
		stroutput+="-";
		OutputDebugString(_T("加密成功"));
		int ipos = InputClear.find_last_of("/");
		InputClear.insert(ipos+1,stroutput);
		return InputClear;


	}
	else if (cbc == iMode)
	{
		OutputDebugString(_T("启用3des-cbc加密..."));
		int iTmpLen = InputOffset.length();
		if (8 < iTmpLen)
		{
			InputOffset.erase(8);
		}
		else if (8 > iTmpLen)
		{
			string strTmp;
			int j = 8 - iTmpLen;
			for (int i = 0;i < j;i++)
			{
				strTmp+="0";
			}
			InputOffset+=strTmp;

		}
		string stroutput;

		stroutput+="3DES-";
		stroutput+="CBC-";
		stroutput+=InputOffset;
		stroutput+="-";
		stroutput+=InputKey;
		stroutput+="-";
		OutputDebugString(_T("加密成功"));
		int ipos = InputClear.find_last_of("/");
		InputClear.insert(ipos+1,stroutput);
		return InputClear;


	}
	OutputDebugString(_T("加密失败"));
}


C3DES::C3DES()
{
	;
}

C3DES::~C3DES()
{
	;
}

以上并未实际完成AES跟3DES的加解密,仅仅做了示范

现在我们基类跟子类都写完了,需要将其添加至简单工厂

///*****************************************/
//                           
//说明: CEADFactory头文件定义
//作者: C006               
//
///*****************************************/
#pragma once
#include "CodeBase.h"
#include "aes.h"
#include "3des.h"

#define _aes    1
#define _3des   2

class CEADFactory
{
public:

	CEADFactory()
	{

	}
	~CEADFactory()
	{

	}

public:
	CCodeBase * CreateType(int iSign)
	{
		CCodeBase * p = NULL;

		if (_aes == iSign)
		{
			p = new CAES;
			return p;
		}
		else if (_3des == iSign)
		{
			p = new C3DES;
		}
	}


};

至此简单工厂就完成

下面是测试

#include <Windows.h>
#include <string>
#include <iostream>
#include "EADFactory.h"
#include "CodeBase.h"
#include "aes.h"
#include "3des.h"
using namespace std;


int main()
{
	CEADFactory CEAD;
	CCodeBase * pC = CEAD.CreateType(_aes);

	//string str1 = "hello world";
	//string str2 = "12345678";

	//string strOut = pC->StringEnc(str1,str2,cbc,"1234");
	//cout << strOut << endl;

	//string strOutdec = pC->StringDec(strOut,str2,cbc,"1234");
	//cout << strOutdec << endl;

	//string strOut1 = pC->StringEnc(str1,str2,ecb);
	//cout << strOut1 << endl;

	//string strOutdec2 = pC->StringDec(strOut,str2,ecb);
	//cout << strOutdec2 << endl;

	CCodeBase * pC1 = CEAD.CreateType(_3des);

	//string strOut2 = pC1->StringEnc(str1,"87654321",cbc,"1234");
	//cout << strOut2 << endl;

	//string strOutdec3 = pC1->StringDec(strOut2,"87654321",cbc,"1234");
	//cout << strOutdec3 << endl;

	//string strOut4 = pC1->StringEnc(str1,str2,ecb);
	//cout << strOut4 << endl;

	//string strOutdec5 = pC1->StringDec(strOut,str2,ecb);
	//cout << strOutdec5 << endl;


//文件加解密
	string strFenc = pC->FileEnc("D:/test/aa.txt","12345678",ecb);
	cout << strFenc << endl;

	string strDec = pC->FileDec(strFenc,"12345678",ecb);
	cout << strDec << endl;
	
	string strFenc1 = pC->FileEnc("D:/test/aa.txt","12345678",cbc,"1234");
	cout << strFenc1 << endl;

	string strDec1 = pC->FileDec(strFenc1,"12345678",cbc,"1234");
	cout << strDec1 << endl;


	string strFdec = pC1->FileEnc("D:/test/aa.txt","12345678",ecb);
	cout << strFdec << endl;

	string strFdec1 = pC1->FileDec(strFdec,"12345678",ecb);
	cout << strFdec1 << endl;

	string strFdec3 = pC1->FileEnc("D:/test/aa.txt","12345678",cbc,"1234");
	cout << strFdec3 << endl;

	string strFdec4 = pC1->FileDec(strFdec3,"12345678",cbc,"1234");
	cout << strFdec4 << endl;



	return 0;
}

去掉代码中注释,全部运行后结果图:

猜你喜欢

转载自blog.csdn.net/qq_37059136/article/details/86479324