创建一个控制台程序,复制代码运行即可
核心代码转载于: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);
}
加载中,请稍候......