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;
}
算是文件加密入门。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值