这道题,并不难,只是楼主,没有说清,是就字母移位吗?
但是看你的例子,有不全是。
程序如下:
#include
#include
FILE *source;//源文件
FILE *destination;//目标文件
int key;//密钥
char file[100];//文件名
void encryption()//加密
{
char ch;
printf("请输入要加密的文件名\n");
scanf("%s",file);
if((source=fopen(file,"r"))==NULL)
{
printf("无法打开文件!\n");
exit(0);
}
printf("请输入加密后的文件名\n");
scanf("%s",file);
if((destination=fopen(file,"w"))==NULL)
{
printf("无法创建文件!\n");
exit(0);
}
printf("请输入密钥\n");
scanf("%d",&key);
ch=fgetc(source);
while(ch!=EOF)
{
if(ch=='\n')
{
fputc(ch,destination);
ch=fgetc(source);
continue;
}
ch+=key;
fputc(ch,destination);
ch=fgetc(source);
}
fclose(source);
fclose(destination);
}
void decrypt()//解密
{
char ch;
printf("请输入要解密的文件名\n");
scanf("%s",file);
if((source=fopen(file,"r"))==NULL)
{
printf("无法打开文件!\n");
exit(0);
}
printf("请输入加密后的文件名\n");
scanf("%s",file);
if((destination=fopen(file,"w"))==NULL)
{
printf("无法创建文件!\n");
exit(0);
}
printf("请输入密钥\n");
scanf("%d",&key);
ch=fgetc(source);
while(ch!=EOF)
{
if(ch=='\n')
{
fputc(ch,destination);
ch=fgetc(source);
continue;
}
ch-=key;
fputc(ch,destination);
ch=fgetc(source);
}
fclose(source);
fclose(destination);
}
int main()//主函数提供菜单
{
int choice=0;
printf("******************\n");
printf("1 文件加密\n");
printf("2 文件解密\n");
printf("3 退出\n");
printf("******************\n");
printf("请输入1 2 3选择操作\n");
scanf("%d",&choice);
switch(choice)
{
case 1:encryption();break;
case 2:decrypt();break;
case 3:break;
}
return 0;
}
#include
void code(char *p,int key)
{
while(*p!='\0')
{
*p=97+(*p-97+key)%26;
p++;
}
}
void uncode(char *p,int key)
{
while(*p!='\0')
{
*p=97+(*p-71-key)%26;
p++;
}
}
main()
{
char str[100];
int n,key;
printf("输入密匙:");
scanf("%d",&key);
printf("输入1加密,输入2解密:");
scanf("%d",&n);
printf("输入字符串:");
scanf("%s",str);
if(n==1)
{
code(str,key);
printf("密文为%s\n",str);
}
else if(n==2)
{
uncode(str,key);
printf("原文为%s\n",str);
}
}
//---------------------------------------------------------------------------
#include
void encryption(const char *fname) /*对文件路径为fname的文件进行加密*/
{
int offset;
char efname[256],ch;
FILE *fp,*fp1;
if ((fp=fopen(fname,"r"))==NULL) {
fprintf(stderr,"FILE NOT FOUND!\n");
return ;
}
printf("path of the encrypted file:");
gets(efname); /*输入加密后的文件的保存路径*/
printf("offset:");
scanf("%d",&offset); /*输入位移量(密钥)*/
fp1=fopen(efname,"w");
while ((ch=fgetc(fp))!=EOF) /*进行加密,并写入密文文件*/
{
fputc(ch+offset,fp1);
}
fclose(fp1);
fclose(fp);
puts("Success!");
}
void decryption(const char *fname) /*对文件路径为fname的文件进行解密*/
{
int offset;
char efname[256],ch;
FILE *fp,*fp1;
if ((fp=fopen(fname,"r"))==NULL) {
fprintf(stderr,"FILE NOT FOUND!\n");
return ;
}
printf("path of the declassified document :");
gets(efname); /*输入解密后的文件的保存路径*/
printf("offset:");
scanf("%d",&offset); /*输入位移量(密钥)*/
fp1=fopen(efname,"w");
while ((ch=fgetc(fp))!=EOF) /*进行解密,并写入明文文件*/
{
fputc(ch-offset,fp1);
}
fclose(fp1);
fclose(fp);
puts("Success!");
}
int main(void)
{
char fpname[256];
puts("A.Encryption");
puts("B.Decryption");
printf("Input A or B(other to EXIT) :"); /*输入a为加密,输入b为解密,输入其它字符则退出程序*/
switch(getchar())
{
case 'a':
case 'A':
printf("FILE:");
fflush(stdin);
gets(fpname); /*输入要加密的明文文件路径*/
encryption(fpname); /*调用加密函数对文件进行加密*/
break;
case 'b':
case 'B':
printf("FILE:");
fflush(stdin);
gets(fpname); /*输入要解密的密文文件路径*/
decryption(fpname); /*调用解密函数对文件进行解密*/
break;
default:break;
}
return 0;
}
//---------------------------------------------------------------------------
全部缩进到首行了,ALT+F8可以自动对齐~~用最简单的C写的,一目了然!设文件为1.txt
#include
#include
#include
#define MAXLONGTH 5000 //如果文件内容较长,请修改MAXLONGTH
void show()//显示源文件内容
{
FILE *fp;
char ch;
if((fp=fopen("1.txt","r"))==NULL)
{
printf("打开文件失败!\n");
getch();
exit(1);
}
ch=fgetc(fp);
while (ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}
void jiami(int num) //密码算法,入口参数:密钥
{
FILE *fp;
char ch;
char strbuffer[MAXLONGTH];
int bufsize = 0;
if((fp=fopen("1.txt","r"))==NULL)
{
printf("打开文件失败!\n");
getch();
exit(1);
}
ch=fgetc(fp);
while (ch!=EOF)
{
ch = ch+num;
putchar(ch);
strbuffer[bufsize]=ch;
bufsize++;
ch=fgetc(fp);
}
strbuffer[bufsize]='\0';
printf("\n\n");
fclose(fp);
if((fp=fopen("1.txt","w+"))==NULL) //重写文件
{
printf("写入文件失败,任意键退出!");
getch();
exit(1);
}
fputs(strbuffer,fp);
fclose(fp);
}
void main()
{
int num = 0;
int chooce = 0;
printf("源文件内容如下:\n");
show();
printf("\n\n请输入密匙:");
scanf("%d",&num);
printf("*****************\n");
printf("** 1.加密 **\n");
printf("** 2.解密 **\n");
printf("*****************\n");
printf("请选择文件处理方式:");
scanf("%d",&chooce);
while(chooce)
switch(chooce) {
case 1: //加密算法
printf("\n加密结果------->");
jiami(num);
chooce = 0;
break;
case 2: //解密算法
num = -num;
printf("\n解密结果------->");
jiami(num);
chooce = 0;
break;
default:
printf("\n无效请求,请重新选择文件处理方式:");
scanf("%d",&chooce);
}
}