加密程银嫌序代码:
#include<stdio.h>
main()
{
char
c,filename[20]
FILE
*fp1,*fp2
printf("请输入待加密的文件名:\n")
scanf("%s",filename)
fp1=fopen(filename,"r")
fp2=fopen("miwen.txt","w")
do
{
c=fgetc(fp1)
if(c>=32&&c<=126)
{
c=c-32
c=126-c
}
if(c!=-1)
fprintf(fp2,"%c",c)
}
while(c!=-1)
}
解密程序代码:
#include<stdio.h>
#include<string.h>
main()
{
char
c,filename[20]
char
yanzhengma[20]
FILE
*fp1,*fp2
printf("请输入待解密文件名:\n")
scanf("%s",filename)
printf("请输入验证码:\n")
scanf("%s",yanzhengma)
if(strcmp(yanzhengma,"shan")==0)
{
fp1=fopen(filename,"r")
fp2=fopen("yuanwen.txt","w")
do
{
c=fgetc(fp1)
if(c>=32&&c<=126)
{
c=126-c
c=32+c
}
if(c!=-1)
fprintf(fp2,"%c",c)
}
while(c!=-1)
}
else
{
printf("锋辩手验证码错误!请重新输入:\n")
scanf("%s",filename)
}
}
c语言文件加密和解密方法如下:
1、首先打开VC++6.0;
2、选择文件,新建;
3、选择C++ source file 新建一个空白文档;
4、声明头文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
首先写个加密函数,算法就是简介里说的
void EncryptFile(FILE *sfp,FILE *dfp,char 者册pwd)
{
char ch
if(sfp==0||dfp==0)
{
printf("ERROR!\n")
悉运return
}
while((ch=fgetc(sfp))!=EOF)
{
if((ch>='a')&&(ch<='z'))
{
ch=(ch-'a'+1)%26+'a'
ch=ch^pwd
}
if((ch>='A')&&(ch<='Z'))
{
ch=(ch-'A'+1)%26+'A'
ch=ch^pwd
}
fputc(ch,dfp)
}
}
写解密子函数:与加密的过程相反
void DecryptFile(FILE *sfp,FILE *dfp,char pwd)
{
char ch
while((ch=fgetc(sfp))!=EOF)
{
if((ch>='a')&&(ch<='z'))
{
ch=ch^pwd
睁嫌梁 ch=(ch-'a'+25)%26+'a'
}
if((ch>='A')&&(ch<='Z'))
{
ch=ch^pwd
ch=(ch-'A'+25)%26+'A'
}
fputc(ch,dfp)
}
}
输出函数,输出文件内容
void OutputFile(FILE *fp)
{
char ch
while((ch=fgetc(fp))!=EOF)
putchar(ch)
}
主函数,主要调用这几个函数
int main()
{
/*用户输入的要加密的文件名*/
char sfilename[20]
/*用户输入加密后保存的文件名*/
char dfilename[20]
/*用来保存密码字符*/
char pwd
FILE *sfp,*dfp
printf("\nPlease input filename to be encrypted:\n")
/*得到要加密的文件名*/
gets(sfilename)
/*得到加密后你要的文件名*/
printf("input filename to save the encrypted file:\n")
gets(dfilename)
/*得到加密字符*/
printf("Please input your Password:\n")
//scanf("%c",&pwd)
pwd=getch()
/*屏幕以*来表示输入的加密字符*/
printf("*\n")
/*以只读方式打开要加密的文件*/
if((sfp=fopen(sfilename,"r"))==0)
{
printf("Can't open the file :%s\n",sfilename)
exit(0)
}
/*输出要加密的文件*/
printf("\nThe the text of file to be encrypted is:\n")
OutputFile(sfp)
/*建立加密后的文件*/
if((dfp=fopen(dfilename,"w+"))==0)
{
printf("Can't open or create the file :%s\n",dfilename)
//exit(0)
}
/*文件加密*/
fseek(sfp,0L,SEEK_SET)
EncryptFile(sfp,dfp,pwd)
printf("\n\nEncrypted the file successfully!\n")
/*输出加密后的文件*/
printf("\nAfter encrypting the text of file is:\n")
fseek(dfp,0L,SEEK_SET)
OutputFile(dfp)
fclose(sfp)
fclose(dfp)
getch()
return 0
}
这里使用的是按位加密,按ASCII码进行加密的算法自己写个,很容易的。#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void
dofile(char
*in_fname,char
*pwd,char
*out_fname)/*对文件进行加密的具体函数*/
void
usage(char
*name)
void
main(int
argc,char
*argv[])/*定义main()函数的命令行参数*/
{
char
in_fname[30]/*用户输入的要加密的文件名*/
char
out_fname[30]
char
pwd[10]/*用来保存密码*/
if(argc!=4)
{/*容错处理*/
usage(argv[0])
printf("\nIn-fname:\n")
gets(in_fname)/*得到要加密的文件名*/
while(*in_fname==NULL)
{
printf("\nIn-fname:\n")
gets(in_fname)
}
printf("Password
6-8:\n")
gets(pwd)/*得到密码*/
while(*pwd==NULL
||
strlen(pwd)>8
||
strlen(pwd)<6)
{
printf("Password
6-8:\n")
gets(pwd)
}
printf("Out-file:\n")
gets(out_fname)/颤袭*得到加密后你要的文件名*/
while(*in_fname==NULL)
{
printf("Out-file:\n")
gets(out_fname)
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n")
printf("Out-file:\n")
gets(out_fname)
}
dofile(in_fname,pwd,out_fname)
printf("加肆洞灶密成功,解密请再次运行程序\n")
}
else
{/*如果命令行参数正确,便直接运行程序*/
strcpy(in_fname,argv[1])
strcpy(pwd,argv[2])
strcpy(out_fname,argv[3])
while(*pwd==NULL
||
strlen(pwd)>8
||
strlen(pwd)<6)
{
printf("Password
faied!\n")
printf("Password
6-8:\n")
gets(pwd)
}
while(!strcmp(in_fname,out_fname))
{
printf("文件名不能和源文件相同\n")
printf("Out-file:\n")
gets(out_fname)
while(*in_fname==NULL)
{
printf("Out-file:\n")
gets(out_fname)
}
}
dofile(in_fname,pwd,out_fname)
printf("加密成功,解密请再次运行程序\n")
}
}
/*加密子函数开始*/
void
dofile(char
*in_fname,char
*pwd,char
*out_file)
{
FILE
*fp1,*fp2
register
char
ch
int
j=0
int
j0=strlen(pwd)
fp1=fopen(in_fname,"rb")
if(fp1==NULL)
{
printf("cannot
open
in-file.\n")
exit(1)/*如果不能打开要加密的文件,便退出程序*/
}
fp2=fopen(out_file,"wb")
if(fp2==NULL)
{
printf("cannot
open
or
create
out-file.\n")
exit(1)/*如果不能建立加密后的文件,便退出*/
}
/*加密算法开始*/
while(j0>=0)
{
ch=fgetc(fp1)
while(!feof(fp1))
{
fputc(ch^pwd[j>=j0?j=0:j++],fp2)/*异或后写入fp2文件*/
ch=fgetc(fp1)
}
j0--
}
fclose(fp1)/*关闭源裂扮文件*/
fclose(fp2)/*关闭目标文件*/
}
void
usage(char
*name)
{
printf("\t=======================File
encryption======================\n")
printf("\tusage:
%s
In-fname
password
out_fname\n",name)
printf("\tExample:
%s
file1.txt
12345678
file2.txt\n",name)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)