我想加密,然後解密文件。當我嘗試解密文件時,我想在屏幕上顯示內容,以確保解密過程完成沒有問題。但是,我沒有顯示文件的解密。我不確定我的代碼中缺少什麼。我正在使用Dev_C++。您的幫助將非常感激。代碼如下。加密/解密文件?
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string line;
string file, encrfile;
int i, key_length, longueur;
unsigned int key=0;
char ch[100];
cout<<"enter a secret key: ";
cin.getline(ch, 100);
for (i=0;ch[i];i++)
key=(key+3)*ch[i];
cout<<"Password generated: "<<key;
cout<<"\n\nEnter the name of the input file: ";
getline(cin,file);
cout<<"\nEnter the name of the output file: ";
getline(cin,encrfile);
ifstream IS;
IS.open(file.c_str());
ofstream OS;
OS.open(encrfile.c_str());
while(IS>>line);
{
//encrypting each character
for (i=0;i<line.length();i++)
{
line[i]^=rand()>>8;
OS<<line[i]; //writing the character in the output file
}
}
IS.close();
OS.close();
cout<<"File "<<encrfile<<" has been encrypted"<<endl;
cout<<"\nEnter the name of the file to decrypt: ";
getline(cin,encrfile);
cout<<"\n\nDecryption of file: "<<endl;
ifstream IS2;
IS2.open(encrfile.c_str());
while(IS2>>line);
{
for (i=0;i<line.length();i++)
{
line[i]^=rand()>>8;
cout<<line[i];
}
}
IS2.close();
return 0;
}
您正在寫出隨機字節,您預計會發生什麼? – PlasmaHH 2013-05-13 20:05:21
你不用任何東西的鑰匙。 – 2013-05-13 20:05:23
注意:'while(IS >> line)'讀一個字不是一行。所以你會從你的文件中刪除所有的空格。 – 2013-05-13 20:19:58