加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C++ 简单文件的加密与解密

(2011-07-21 17:24:59)
标签:

it

分类: 工作/开发方面

创建一个控制台程序,复制代码运行即可

核心代码转载于:http://blog.csdn.net/JarvisChu/article/details/5785870

#include "stdafx.h"
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>   

void encfile(char *in_filename,char *pwd,char *out_filename);  
void decryptfile(char* in_filename,char *pwd,char *out_filename);  

int main(int argc,char *argv[])  
 
 int option;  
 char in_filename[30];  
 char out_filename[30];  
 char pwd[8];  

 printf("                   thank you for using this program....../n"); 
 printf("1. Encrypt a file         2. Decrypt a file/n");     
 printf("chose your option....."); 


 scanf("%d",&option); 
 getchar(); 
 if(argc!=4){  
  printf("/nPlease input In-filename:/n");  
  gets(in_filename);  
  printf("Please input your Password:/n");  
  gets(pwd);  
  printf("Please input Out-filename:/n");  
  gets(out_filename);  

  
 else{  
  strcpy(in_filename,argv[1]);  
  strcpy(pwd,argv[2]);  
  strcpy(out_filename,argv[3]);  
  

 switch(option){ 
 case 1: //加密      
  encfile(in_filename,pwd,out_filename);  
  break; 
 case 2://解密  
  decryptfile(in_filename,pwd,out_filename); 
  break; 
 default: 
  break; 
    

 system("pause");  
 return 0;  
 

  
void encfile(char *in_filename,char *pwd,char *out_file)  
 
 FILE *fp1,*fp2;  
 register char ch;  
 int j=0;  
 int j0=0;  
 fp1=fopen(in_filename,"r");  
 if(fp1==NULL){  
  printf("cannot open in-file./n");  
  exit(1);  
  
 fp2=fopen(out_file,"w");  
 if(fp2==NULL){  
  printf("cannot open or create out-file./n");  
  exit(1);  
  

 while(pwd[++j0]);   

 ch=fgetc(fp1);  

   
 while(!feof(fp1)){  
  if(j0 >7) 
   j0 = 0; 
  ch += pwd[j0++]; 
  fputc(ch,fp2);  
  ch=fgetc(fp1);  
  
 fclose(fp1);  
 fclose(fp2);  
 

  
void decryptfile(char *in_filename,char *pwd,char *out_file)  
 
 FILE *fp1,*fp2;  
 register char ch;  
 int j=0;  
 int j0=0;  
 fp1=fopen(in_filename,"r");  
 if(fp1==NULL){  
  printf("cannot open in-file./n");  
  exit(1);  
  
 fp2=fopen(out_file,"w");  
 if(fp2==NULL){  
  printf("cannot open or create out-file./n");  
  exit(1);  
  

 while(pwd[++j0]);    
 ch=fgetc(fp1);  
   
 while(!feof(fp1)){  
  if(j0 >7) 
   j0 = 0; 
  ch -= pwd[j0++]; 
  fputc(ch,fp2); 
  ch=fgetc(fp1);  
  
 fclose(fp1);  
 fclose(fp2);  
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有