我最近为一个学校项目编写了以下代码;我的目标是制作一个基本的加密程序。目前要使用此程序加密文件,需要知道文件名并手动输入控制台,包括文件扩展名。为了提高程序的用户友好性,我想实现一个打开 Windows 文件资源管理器窗口的功能,以便用户可以选择他们想要加密的文件。在互联网上进行大量搜索后,我无法找到任何方法将其实现到我的代码中。所以我的问题是,C++ 库中是否存在此功能,如果存在,我如何将它实现到我的代码中。
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
using namespace std;
#define ENCRYPTION_FORMULA (int) Byte * 25
#define DECRYPTION_FORMULA (int) Byte / 25
int Encrypt (char * FILENAME, char * NEW_FILENAME)
{
std::ifstream inFile;
std::ofstream outFile;
char Byte;
inFile.open(FILENAME, ios::in | ios::binary);
outFile.open(NEW_FILENAME, ios::out | ios::binary);
while(!inFile.eof())
{
char NewByte;
Byte = inFile.get();
if (inFile.fail())
return 0;
NewByte = ENCRYPTION_FORMULA;
outFile.put(NewByte);
}
inFile.close();
outFile.close();
return 1;
}
int Decrypt (char * FILENAME, char * NEW_FILENAME)
{
std::ifstream inFile;
std::ofstream outFile;
char Byte;
inFile.open(FILENAME, ios::in | ios::binary);
outFile.open(NEW_FILENAME, ios::out | ios::binary);
while(!inFile.eof())
{
char NewByte;
Byte = inFile.get();
if (inFile.fail())
return 0;
NewByte = DECRYPTION_FORMULA;
outFile.put(NewByte);
}
inFile.close();
outFile.close();
return 1;
}
int main()
{
char EncFile[200];
char NewEncFile[200];
char DecFile[200];
char NewDecFile[200];
int Choice;
cout << "NOTE: You must encrypt the file with the same file extension!"<<endl;
cout << "Enter 1 to Encrypt / 2 to Decrypt"<<endl;
cin >> Choice;
switch(Choice)
{
case 1:
cout << "Enter the current Filename: ";
cin >> EncFile;
cout << "Enter the new Filename: ";
cin >> NewEncFile;
Encrypt(EncFile, NewEncFile);
break;
case 2:
cout << "Enter the current Filename: ";
cin >> DecFile;
cout << "Enter the new Filename: ";
cin >> NewDecFile;
Decrypt(DecFile, NewDecFile);
break;
}
return 0; //Exit!
}