一.维吉尼亚密码加密文本文件

要求: 用维吉尼亚密码实 现加密任意文本文件,注意用控制台方式实现。 输入格式: >-e/-d key filename (key为一个单词,或者一串字符串)

代码:
// Console-维吉尼亚加密文本
//2015-10-4
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

//void encrypt (string, ifstream);
//void decrypt (string, ofstream);

int main(int argc, char *argv[])
{
if (argc != 4)//处理输入指令,输入有误,返回提示
{//QIUT for wrong input
	cout << "wrong input!" << endl;
	exit(EXIT_FAILURE);
}

//for (int i = 0; i<argc; i++) //验证输入指令
//	cout << argv[i] << '\t';

string tag(argv[1]),key(argv[2]),filename(argv[3]);//接收加解密标志,密钥,文件名
char temp;
int et= tag.find('-e'), dt = tag.find('-d');//查找加密解密指令
int klen= key.size(),j=0;
for (int i = 0; i < klen; i++) //处理密钥为大写
{
	if (key[i] >= 97 && key[i] < 122)
		key[i] = key[i] - 32;
}
if (et!= string::npos&&dt == string::npos)//加密文件
{
	//定义文件指针
	ifstream fin(filename);
	ofstream fout("foe.txt", ios_base::out|ios_base::trunc);//文件打开方式为清除原有数据,重新天填入
	while (fin.get(temp))
	{
		for (;;)
		{
			if (temp >= 65 && temp <= 90){
				temp = (((temp - 65) + (key[j] - 65)) % 26 + 97);
				fout << temp;
			}
			else if (temp >= 97 && temp <= 122){
				temp = (((temp - 97) + (key[j] - 65)) % 26 + 65);
				fout << temp;
			}
			else
				fout << temp;;
			j=(j+1)% klen;
			break;

		}
	}
	fin.close();//关闭文件指针
	fout.close();
}
else if (et == string::npos&&dt != string::npos)//解密文件
{ 
	ifstream fin(filename);
	ofstream fout("fod.txt", ios_base::out | ios_base::trunc);

	while (fin.get(temp))
	{
		for (;;)
		{
			if (temp >= 65 && temp <= 90){
				temp = (((temp - 65 + 26) - (key[j] - 65)) % 26 + 97);
				fout << temp;
			}
			else if (temp >= 97 && temp <= 122){
				temp = (((temp - 97 + 26) - (key[j] - 65)) % 26 + 65);
			    fout << temp;
	     	}
			else
				fout << temp;
			j = (j + 1) % klen;
			break;

		}
	}
	fin.close();
	fout.close();
}
else
{
	cout << "no find :-e or -d" << endl; //没有找到加密加密指令,返回错误信息
	exit(EXIT_FAILURE);
}
return 0;
}

####二.维吉尼亚密码加密任意类型文件 要求: 用维吉尼亚密码加密任意类型的文件,用控制台方式实现。 ######代码: // Console维吉尼亚加密任意文件 //2015-10-4

#include <iostream>
#include <fstream>  //文件流
#include <string>  //string型操作
#include <cstdlib> //读取写入二进制数据
#include <cstdio> //为生成临时文件名

using namespace std;

int main(int argc, char *argv[])
{
if (argc != 4)
{//输入有错时返回提示信息并结束程序
	cout << "输入指令错误,请确认输入格式为:exeFileName '-e'or'-d' keyword FileName" << endl;
	exit(EXIT_FAILURE);
}

//for (int i = 0; i<argc; i++) //验证输入指令
//	cout << argv[i] << '\t';

string tag(argv[1]), keytext(argv[2]), filename(argv[3]);//接收加解密标志,密钥,文件名
//把keytext存到临时文件里,然后以二进制读取作为密钥

char tempfname[L_tmpnam] = { '\0' };
tmpnam_s(tempfname);//生成临时文件名
//cout << tempfname << endl;
ofstream tempfout(tempfname, ios_base::out | ios_base::trunc);
tempfout << keytext;//把keytext写入临时文件
ifstream tempfin;
tempfin.open(tempfname, ios_base::in| ios_base::binary);
tempfin.seekg(0,ios::end);//定位指针到文件末尾
streampos tfp = tempfin.tellg();//tfp为定位指针,指在文件末尾,所以就是文件长度
tempfin.seekg(0, ios::beg);//恢复临时文件指针到文件头,以便后续操作
cout << "临时文件操作完成" << endl;

char temp[1];//存储待加密解密文件数据
int et = tag.find('-e'), dt = tag.find('-d');//查找加密解密指令

if (et != string::npos&&dt == string::npos)//加密文件
{
	cout << "-e 确认为加密操作" << endl << "待加密文件名:" << filename << endl;
	//定义文件指针
	ifstream fin;
	fin.open(filename, ios_base::in | ios_base::binary);
	ofstream fout("file.enc", ios_base::out | ios_base::trunc | ios_base::binary);//文件打开方式为清除原有数据,重新天填入
	int i=0,count=0;
	char tempdate[1];//临时存储一字节加密解密数据
	char ttemp[1];//存储密钥数据
	while (fin.read(temp,1))//外循环读取源文件
	{
		for (;;)//内循环读取密钥数据
		{
			tempfin.read(ttemp, 1);
			tempdate[0] = (ttemp[0] + temp[0]) % 256;
			fout.write(&tempdate[0],1);
			count++;
			if(count==tfp)
				tempfin.seekg(0);
			break;
		}
	}
	cout << "加密完成,加密后文件:file.enc" << endl;
	cout << "总共加密"<<count<<"字节数据"<<endl;
	fin.close();//关闭文件指针
	fout.close();
	tempfout.close();
	tempfin.close();
	remove(tempfname);//删除临时文件
}
else if (et == string::npos&&dt != string::npos)//解密文件
{
	cout << "-d 确认为解密操作" << endl << "待解密文件名:" << filename << endl;
	//定义文件指针
	ifstream fin;
	fin.open(filename, ios_base::in | ios_base::binary);
	ofstream fout("FileOfDec", ios_base::out | ios_base::trunc | ios_base::binary);//文件打开方式为清除原有数据,重新天填入
	int i = 0, count = 0;
	char tempdate[1];//临时存储一字节加密解密数据
	char ttemp[1];//存储密钥数据
	while (fin.read(temp, 1))
	{
		for (;;)
		{
			tempfin.read(ttemp, 1);
			tempdate[0] = (temp[0]-ttemp[0]) % 256;
			fout.write(&tempdate[0], 1);
			count++;
			if (count == tfp)
				tempfin.seekg(0);
			break;
		}
	}
	cout << "解密完成,解密后文件:FileOfDec"<<"请自行确认解密后文件类型后补充文件后缀" << endl;
	cout << "总共解密" << count << "字节数据" << endl;
	fin.close();//关闭文件指针
	fout.close();
	tempfout.close();
	tempfin.close();
	remove(tempfname);//删除临时文件
}
else
{
	cout << "没有找到加密或者解密指令 :-e or -d" << endl; //没有找到加密加密指令,返回错误信息
	exit(EXIT_FAILURE);
}
return 0;
}