C++ I/O操作——簡單文件加密

#include<iostream>
#include<fstream>
#include<string>
/*
  實現對輸入文件進行簡單加密和解密,然後輸出到輸出文件。
  解密時密碼需要和加密時密碼一致。
  
*/

using namespace std;

int main()
{   string str;
    char buffer[256];
	int psw;  int a;
	cout <<"encode or decode ? if encode, please input 1 else decode input 2 :"<<endl;  // encode or decode ?
	cin >> a;	                                                     // 
	cout <<"please input the psw:"<<endl;                            // input psw
	cin >> psw;
	if(a == 2)
		psw = 0 - psw;                                           
    cout <<"please input the filename of source...."<<endl;           // input source file   
	cin >> str;
	fstream outfile(str.c_str(), ios::in);
	cout <<"please input the filename of destinity...."<<endl;       // input destiny file
	cin >> str;
	ofstream outtxt(str.c_str(),ios::ate| ios::out );
	
	while(!outfile.eof())                                         // process 
	{ 
	  cout <<"222"<<endl;
	  outfile.getline(buffer, 256,'\n');
	  cout << buffer <<endl;
	  for(int i = 0; i < strlen(buffer); i++)                   //algorithm of encode or decode
	  buffer[i] = buffer[i] + psw;
	  
      outtxt << buffer <<endl;
	}
	
	outfile.clear();
	outfile.close();


    return 0;
}
算是文件加密入門。
發佈了53 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章