#include<string.h>
#include<stdlib.h>
main()
{
void sc(char *fp,char *key,int Flen,int Klen)
FILE *fp
char *pBuf,filename[20],key[20],ch
printf("请输入选择:A、加密B、解密 C退出\n")
ch=getchar()
while(ch!='c'&&ch!='C')
{
if(ch=='a'||ch=='A'||ch=='b'||ch=='B')
{
printf("请输入要打开的文件名:\n")
scanf("%s",filename)
if((fp=fopen(filename,"rb"))==NULL)
{printf("无法打开文件,请注意尺唤输入后缀!\n")<br/> exit(0)<br/> }
fseek(fp,0,SEEK_END)
int len=ftell(fp)
pBuf=new char[len+1]
rewind(fp)
fread(pBuf,1,len,fp)
pBuf[len]=0
printf("%s\n",pBuf)
fclose(fp)
printf("请输入加密/解密的密码:\n")
scanf("%s",key)
sc(pBuf,key,len,strlen(key))
printf("请输入保存加密文件的文件名:\n")
scanf("%s",filename)
if((fp=fopen(filename,"wb"))==NULL)
{printf("无法保存文件,请注意磁盘是否已满!\n"陵物凯)<br/蚂带> exit(0)<br/> }
else
fwrite(pBuf,1,len,fp)
fclose(fp)
printf("请输入选择:A、加密B、解密 C退出\n")
}
else {
printf("输入错误,请重新输入\n")
}
ch=getchar()
ch=getchar()
}
}
void sc(char *fp,char *key,int Flen,int Klen)
{int i,j,k<br/>for(i=0i<Fleni+=Klen)<br/>for(j=i,k=0k<Klenj++,k++)<br/>fp[j]^=key[k]<br/><br/>fp[i]='\0'<br/>printf("%s\n",fp)<br/>}
C语言设计一个简单的加密解密程序如下:加密程序代码:
#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)
}
}
直接对字符串按字符加减密钥的位数就可以了。#include
<iostream.h>困族衡
#define
MAX
1000
//加密
char
*
Encryption(char
*E,int
Key)
{
for(int
i=0*(E+i)i++)
{
*(E+i)
+=
Key
if(*(E+i)>'z'穗握)
*(E+i)
-=
('z'-'a')+1
}
return
E
}
//解密
char
*
Decryption(char
*E,int
Key)
{
for(int
i=0*(E+i)i++)
{
*(E+i)
-=
Key
if(*(E+i)<'a')
*(E+i)
+=
('z'-'a')+1
}
return
E
}
void
main()
{
char
a[MAX]
int
key
cout<<"输入字符串:"<<endl
cin
>>
a
cout<<"输入汪做密钥:"<<endl
cin
>>key
cout<<"加密输出:"<<Encryption(a,key)<<endl
cout<<"解密输出:"<<Decryption(a,key)<<endl
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)