一.維吉尼亞密碼加密文本文件
要求:
用維吉尼亞密碼實 現(xiàn)加密任意文本文件,注意用控制臺方式實現(xiàn)肌索。
輸入格式:
>-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]); //接收加解密標(biāo)志益愈,密鑰夷家,文件名
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); //文件打開方式為清除原有數(shù)據(jù)库快,重新天填入
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();//關(guān)閉文件指針
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;
}
二.維吉尼亞密碼加密任意類型文件
要求:
用維吉尼亞密碼加密任意類型的文件闽铐,用控制臺方式實現(xiàn)。
代碼:
// Console維吉尼亞加密任意文件
//2015-10-4
#include <iostream>
#include <fstream> //文件流
#include <string> //string型操作
#include <cstdlib> //讀取寫入二進(jìn)制數(shù)據(jù)
#include <cstdio> //為生成臨時文件名
using namespace std;
int main(int argc, char *argv[])
{
if (argc != 4)
{
//輸入有錯時返回提示信息并結(jié)束程序
cout << "輸入指令錯誤添谊,請確認(rèn)輸入格式為: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]);//接收加解密標(biāo)志斩狱,密鑰,文件名
//把keytext存到臨時文件里所踊,然后以二進(jìn)制讀取作為密鑰
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);//恢復(fù)臨時文件指針到文件頭,以便后續(xù)操作
cout << "臨時文件操作完成" << endl;
char temp[1];//存儲待加密解密文件數(shù)據(jù)
int et = tag.find('-e'), dt = tag.find('-d');//查找加密解密指令
if (et != string::npos && dt == string::npos) //加密文件
{
cout << "-e 確認(rèn)為加密操作" << 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);//文件打開方式為清除原有數(shù)據(jù)遏考,重新天填入
int i = 0, count = 0;
char tempdate[1];//臨時存儲一字節(jié)加密解密數(shù)據(jù)
char ttemp[1];//存儲密鑰數(shù)據(jù)
while (fin.read(temp, 1)) //外循環(huán)讀取源文件
{
for (;;)//內(nèi)循環(huán)讀取密鑰數(shù)據(jù)
{
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 << "字節(jié)數(shù)據(jù)" << endl;
fin.close();//關(guān)閉文件指針
fout.close();
tempfout.close();
tempfin.close();
remove(tempfname);//刪除臨時文件
}
else if (et == string::npos && dt != string::npos) //解密文件
{
cout << "-d 確認(rèn)為解密操作" << 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);//文件打開方式為清除原有數(shù)據(jù),重新天填入
int i = 0, count = 0;
char tempdate[1];//臨時存儲一字節(jié)加密解密數(shù)據(jù)
char ttemp[1];//存儲密鑰數(shù)據(jù)
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" << "請自行確認(rèn)解密后文件類型后補(bǔ)充文件后綴" << endl;
cout << "總共解密" << count << "字節(jié)數(shù)據(jù)" << endl;
fin.close();//關(guān)閉文件指針
fout.close();
tempfout.close();
tempfin.close();
remove(tempfname);//刪除臨時文件
}
else
{
cout << "沒有找到加密或者解密指令 :-e or -d" << endl; //沒有找到加密加密指令娃肿,返回錯誤信息
exit(EXIT_FAILURE);
}
return 0;
}