📄 fileencrypt.cpp
字号:
#include "FileEncrypt.h"
#include "TripleDes.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <afxcmn.h>
#include <afx.h>
#include <iostream.h>
extern CProgressCtrl m_progress; //声明为引用外部文件所定义的全局变量
void FileEncrypt::DesFile(CFile *fIn,CFile *fOut,char key[16],bool type)//函数定义 type为ENCRYPT表示解密,为DECRYPT表示加密
{
char buf[8];
DWORD numread=0;
DWORD length=fIn->GetLength();
memset(buf, 0, sizeof(buf));
::SetFileAttributes(fIn->GetFilePath(),FILE_ATTRIBUTE_READONLY);//操作之前,将文件属性设置为只读
while(fIn->Read(buf, 8))
{
Des_Go(buf, buf,sizeof(buf), key, strlen(key),type);
fOut->Write(buf, 8);
numread+=8;
m_progress.SetPos(numread*100/length); //进度条操作
}
m_progress.SetPos(0);
::SetFileAttributes(fIn->GetFilePath(),FILE_ATTRIBUTE_NORMAL); //操作完成之后,将文件属性设置为正常
fIn->Close();
fOut->Close();
}
void FileEncrypt::HandleFile(CFile * fIn, char key[16],bool type)//更改文件名
{
CString fName=fIn->GetFileName();
int index=fName.ReverseFind('.');
CString fNameWithoutPlus=fName.Left(index); //除去后缀名的文件名
int len=fName.GetLength()-index;
CString fPlusName=fName.Right(len); //后缀名
CFile *fOut;
char fPlusNameArray[8];
try{
if(fPlusName==".des")
{
fIn->Read(fPlusNameArray,8); //将文件的后缀名保存到数组中
fOut=new CFile(fNameWithoutPlus + fPlusNameArray,
CFile::modeCreate | CFile::modeWrite);//以标题+原来的后缀为文件名创建一个新文件
}
else
{
::strcpy(fPlusNameArray,fPlusName); //将后缀名拷贝到数组中
fOut=new CFile(fNameWithoutPlus + ".des", CFile::modeCreate | CFile::modeWrite);//创建一个新文件
//HANDLE hFile = CreateFile(fNameWithoutPlus,GENERIC_WRITE, FILE_SHARE_WRITE,
// NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
//fOut=new CFile((int)hFile);
fOut->Write(fPlusNameArray,8);//将文件后缀名写入前8个字节中
}
}
catch(CFileException * pEx)
{
pEx->ReportError();
}
DesFile(fIn,fOut,key,type);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -