📄 mcs.cpp
字号:
// MixedCS.cpp: implementation of the CMcs class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Mcs.h"
#include "MixedCS.h"
#include "Window.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
///////////////////////////////////////////////////////////////////////////////
// CMcs Functions
///////////////////////////////////////////////////////////////////////////////
#define CHECK(x) {if( !(x) ) return false;}
#define FILE_CHECK(x,file) {if( !(x) ) {RunError(file);return false;}}
#define FILE_CHECK_MSG(x,file,msg) {if( !(x) ) {CWindow::ShowMessage(msg);\
RunError(file);return false;}}
/******************************************************************************/
// 名称:Encrypt
// 功能:加密
// 参数:KeyStr,ModStr为0结尾的密钥串,用于设置RSA密钥和模n;Is3DES—3次DES标志
// 返回:加密成功返回true,否则返回false
// 备注:无须保存3次DES标志,因为DES会根据密钥长度自动切换
// 更新:2003/1/30
// 作者:0200935王俊川
/******************************************************************************/
bool CMcs::Encrypt(char *OutFile,char *InFile,char *RsaKeyStr,char *RsaModStr,bool Is3DES)
{
CHECK( RsaKeyStr && RsaModStr && OpenFile(OutFile,InFile) )
char deskey[17];
CWindow wnd;
// 版本信息
mcshead.Ver = 1;
// 显示等待光标
wnd.ShowWaitCursor();
wnd.SetWindowCaption("RSA正在加密DES密钥......");
// 产生随机密钥串
des.RandKeyStr(deskey);
// 如果使用3次DES加密,则再产生一个密钥
if( Is3DES )
des.RandKeyStr(&deskey[8]);
// 加密DES密钥串
FILE_CHECK( mcshead.KeyLen=rsa.Encrypt(mcshead.DesKey,deskey,Is3DES?16:8,
RsaKeyStr,RsaModStr),OutFile )
// 结束等待光标
wnd.EndWaitCursor();
// 写入信息头
_lwrite(fh_out,(char*)&mcshead,sizeof(mcshead));
// 进行DES加密
FILE_CHECK( des.Encrypt(fh_out,fh_in,deskey),OutFile )
CloseFile();
return true;
}
/******************************************************************************/
// 名称:Decrypt
// 功能:解密
// 参数:KeyStr,ModStr为0结尾的密钥串,用于设置RSA密钥和模n
// 返回:解密成功返回true,否则返回false
// 备注:
// 更新:2003/1/30
// 作者:0200935王俊川
/******************************************************************************/
bool CMcs::Decrypt(char *OutFile,char *InFile,char *RsaKeyStr,char *RsaModStr)
{
CHECK( RsaKeyStr && RsaModStr && OpenFile(OutFile,InFile) )
CWindow wnd;
// 读取信息头并检查长度
FILE_CHECK_MSG( _lread(fh_in,&mcshead,sizeof(mcshead))==sizeof(mcshead),
OutFile, "错误:该文件不是有效的MCS加密文件!" )
// 版本控制
FILE_CHECK_MSG( mcshead.Ver==1,OutFile,"该版程序无法解密此文件。\n请使用该程序的最新版。")
// 显示等待光标
wnd.ShowWaitCursor();
wnd.SetWindowCaption("RSA正在解密DES密钥......");
// 解密DES密钥串
int len = rsa.Decrypt(mcshead.DesKey,mcshead.DesKey,mcshead.KeyLen,RsaKeyStr,RsaModStr);
// 结束等待光标
wnd.EndWaitCursor();
// 检查RSA密钥的正确性
FILE_CHECK( len, OutFile )
FILE_CHECK_MSG( len<=16, OutFile,"错误:RSA密钥不正确! ")
mcshead.DesKey[len] = '\0';
// 进行DES解密
FILE_CHECK( des.Decrypt(fh_out,fh_in,mcshead.DesKey),OutFile )
CloseFile();
return true;
}
///////////////////////////////////////////////////////////////////////////////
// End of Files
///////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -