📄 main.cpp
字号:
//Main.cpp
//
#include <iostream>
#include <fstream>
using namespace std;
#include "Main.h"
//宏定义
#define BUFFER_SIZE 128
//程序流程状态
#define EXIT -1
#define INPUT 0
#define PROCESS 1
#define QUIT 3
#define MENU 4
#define SHOW 5
//全局变量
int program_state=MENU;
//int program_state=INPUT;
char szSourceFilename[128]="2.txt"; //源文文件名
char szCryptographFilename[128]="3.txt"; //密文文件名
char cKey=(char)0x97; //密钥
void main ()
{
cout<<"Welcome here!"<<endl;
cout<<"Example of inputing filename: c:/boot.ini"<<endl;
cout<<"Can I help you?"<<endl;
do
{
switch (program_state)
{
case MENU:
Menu ();
break;
case SHOW:
Show ();
program_state = MENU;
break;
case INPUT:
Input ();
program_state = PROCESS;
break;
case PROCESS:
try
{
cout<<"Processing..."<<endl;
Process ();
cout<<"Process end"<<endl;
}
catch (int)
{
cout<<"Error!"<<endl;
}
program_state = MENU;
break;
case QUIT:
if (Quit ())
{
program_state = EXIT;
}
else
{
program_state = MENU;
}
break;
default:
program_state = MENU;
}
}while (program_state != EXIT);
cout<<"See you next time"<<endl;
}
void Menu ()
{
int nID;
try
{
cout<<"1: File encrypt"<<endl;
cout<<"2: Opening file"<<endl;
cout<<"3: Exit program"<<endl;
cout<<"Please input 1/2/3:";
cin>>nID;
}
catch (istream)
{
cout<<"\"cin\" error "<<endl;
program_state = EXIT;
return;
}
catch (ostream)
{
cout<<"\"cout\" error "<<endl;
program_state = EXIT;
}
switch (nID)
{
case 1:
program_state=INPUT;
break;
case 2:
program_state=SHOW;
break;
case 3:
program_state=QUIT;
break;
default:
;
}
}
void Show ()
{
try
{
char buffer[128];
cout<<"Please input filename:";
cin>>buffer;
ShowFile (buffer);
}
catch (char)
{
cout<<"File countn't find"<<endl;
}
}
void ShowFile (char *pszSourceFilename)
{
ifstream infile;
infile.open (pszSourceFilename, ios::in);
if (!infile)
{
throw '0';
}
char buffer[128+1]={0};
while (infile.eof () == false)
{
for (int i=0; i<128; i++)
{
buffer[i] = '\0';
}
infile.read (buffer,128);
cout<<buffer;
}
cout<<endl;
infile.close ();
}
void Input ()
{
cout<<"Please input source filename:";
cin>>szSourceFilename;
cout<<"Please input crytograph filename:";
cin>>szCryptographFilename;
cout<<"Please input key that from -127 to 128:";
cin>>cKey;
}
void Process ()
{
try
{
Encrypt (szCryptographFilename, szSourceFilename, cKey);
}
catch (int)
{
cout<<"Reading source file error!"<<endl;
throw 1;
}
catch (char)
{
cout<<"Reading crytograph file error!"<<endl;
throw 1;
}
}
bool Quit ()
{
char ch;
do
{
cout<<"Do you sure exit? y/n:";
cin>>ch;
cout<<endl;
ch = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;
}while (ch != 'y' && ch != 'n');
if (ch == 'y')
{
return true;
}
return false;
}
void Encrypt (char * pszCryptographFilename, char * pszSourceFilename, char cKey)
{
ifstream infile;
infile.open (pszSourceFilename, ios::in | ios::binary);
if (!infile)
{
throw (int)cKey;
}
ofstream outfile;
outfile.open (pszCryptographFilename, ios::out | ios::binary);
if (!outfile)
{
infile.close ();
throw cKey;
}
char buffer[BUFFER_SIZE]={0};
while (infile.eof ()==false)
{
for (int i=0; i<BUFFER_SIZE; i++)
{
buffer[i] ='\0';
}
infile.read (buffer, BUFFER_SIZE);
if (infile.eof ()==true)
{
int i=0;
while (buffer[i] != '\0')
{
buffer[i++] ^= cKey;
}
outfile.write (buffer, i);
infile.close ();
outfile.close ();
return;
}
for (i=0; i<BUFFER_SIZE; i++);
{
if (cKey % i == 0)
{
buffer[i] ^= 0x19;
}
else
{
buffer[i] ^= cKey;
}
}
outfile.write (buffer, BUFFER_SIZE);
}
infile.close ();
outfile.close ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -