灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3187回复:0

[C++技术]C++ I/O操作——简单文件加密

楼主#
更多 发布于:2013-07-08 13:09
[cpp]
#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 = buffer + psw;
      
      outtxt << buffer <<endl;
    }
    
    outfile.clear();
    outfile.close();
 
 
    return 0;
}

#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 = buffer + psw;
  
      outtxt << buffer <<endl;
 }
 
 outfile.clear();
 outfile.close();


    return 0;
}算是文件加密入门。

喜欢0 评分0
游客

返回顶部