凯撒密码的问题C语言
#include<stdio.h>#include<string.h>intmain(){charsave[10][30];inta,b,i,j;scanf("%d",&...
#include<stdio.h>
#include<string.h>
int main()
{
char save[10][30];
int a,b,i,j;
scanf("%d",&a);
for(i=0;i<a;i++)
{
scanf("%d",&b);
gets(save[i]);
for(j=0;j<30;j++)
{
if((save[i][j]>='A'&&save[i][j]<='Z')||(save[i][j]>='a'&&save[i][j]<='z'))
{
save[i][j]+=b;
if(((save[i][j]>='A'&&save[i][j]<='Z')||(save[i][j]>='a'&&save[i][j]<='z'))==0)
save[i][j]-=26;
}
}
}
for(i=0;i<a;i++)
{
puts(save[i]);
printf("\n");
}
return(0);
}
这是我写的,,,哪里错了
要求是输入的第一行为小于100的正整数 n ,代表计算的数据组个数。然后是 n 组数据输入, 每一组输入包括2行, 第1行是 K 值, -26< K < 26;第2行是明文串,其长度不超过 100 个字符(包含26个大小写字符,不包含空格)。
Sample Input
2
3
itisapen
17
Helloworld
Sample Output
lwlvdshq
Yvccfnficu 展开
#include<string.h>
int main()
{
char save[10][30];
int a,b,i,j;
scanf("%d",&a);
for(i=0;i<a;i++)
{
scanf("%d",&b);
gets(save[i]);
for(j=0;j<30;j++)
{
if((save[i][j]>='A'&&save[i][j]<='Z')||(save[i][j]>='a'&&save[i][j]<='z'))
{
save[i][j]+=b;
if(((save[i][j]>='A'&&save[i][j]<='Z')||(save[i][j]>='a'&&save[i][j]<='z'))==0)
save[i][j]-=26;
}
}
}
for(i=0;i<a;i++)
{
puts(save[i]);
printf("\n");
}
return(0);
}
这是我写的,,,哪里错了
要求是输入的第一行为小于100的正整数 n ,代表计算的数据组个数。然后是 n 组数据输入, 每一组输入包括2行, 第1行是 K 值, -26< K < 26;第2行是明文串,其长度不超过 100 个字符(包含26个大小写字符,不包含空格)。
Sample Input
2
3
itisapen
17
Helloworld
Sample Output
lwlvdshq
Yvccfnficu 展开
2个回答
展开全部
(2)清岁kaiser加密算法
具体程序:
#include<stdio.h>
#include<conio.h>
char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/
{
while(ch>='A'&&ch<='Z')
{
return ('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return ('a'+(ch-'a'+n)%26);
}
return ch;
}
void menu()/*菜单,1.加密,2.解汪晌密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf("\n=========================================================");
printf("\n1.Encrypt the file");
printf("\n2.Decrypt the file");
printf("\n3.Force decrypt file");
printf("\n4.Quit\n");
printf("=========================================================\n");
printf("Please select a item:");
return;
}
main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the key:");
scanf("%d",&n);/*输入加密密码*/
printf("Please input the outfile:");
scanf("%s",outfile);/*输入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nEncrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\困正锋n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the key:");
scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/
n=26-n;
printf("Please input the outfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nDecrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the outfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("==========================================================\n");
printf("The outfile is:\n");
printf("==========================================================\n");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf("\n========================================================\n");
printf("The current key is: %d \n",i);/*显示当前破解所用密码*/
printf("Press 'Q' to quit and other key to continue......\n");
printf("==========================================================\n");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/
{
clrscr();
printf("\nGood Bye!\n");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf("\nForce decrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf("\nGood Bye!\n");
sleep(3);
}
.
希望能够帮助你 ^_^ 也希望能够选为最佳答案!
具体程序:
#include<stdio.h>
#include<conio.h>
char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/
{
while(ch>='A'&&ch<='Z')
{
return ('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return ('a'+(ch-'a'+n)%26);
}
return ch;
}
void menu()/*菜单,1.加密,2.解汪晌密,3.暴力破解,密码只能是数字*/
{
clrscr();
printf("\n=========================================================");
printf("\n1.Encrypt the file");
printf("\n2.Decrypt the file");
printf("\n3.Force decrypt file");
printf("\n4.Quit\n");
printf("=========================================================\n");
printf("Please select a item:");
return;
}
main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the key:");
scanf("%d",&n);/*输入加密密码*/
printf("Please input the outfile:");
scanf("%s",outfile);/*输入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nEncrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\困正锋n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the key:");
scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/
n=26-n;
printf("Please input the outfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nDecrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*输入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the outfile:");
scanf("%s",outfile);/*输入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("==========================================================\n");
printf("The outfile is:\n");
printf("==========================================================\n");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf("\n========================================================\n");
printf("The current key is: %d \n",i);/*显示当前破解所用密码*/
printf("Press 'Q' to quit and other key to continue......\n");
printf("==========================================================\n");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/
{
clrscr();
printf("\nGood Bye!\n");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf("\nForce decrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf("\nGood Bye!\n");
sleep(3);
}
.
希望能够帮助你 ^_^ 也希望能够选为最佳答案!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<坦举缓stdio.h>
#include<string.h>
int main()
{
char save[10][30];
int a,b,i,j;
scanf("%d",&a);
for(i=0;i<a;i++)
{
scanf("%d",&b);
getchar();//加这个
gets(save[i]);
for(j=0;j<30;j++)
{
if((save[i][j]>='A'&&save[i][j]<='Z')||(save[i][j]>='让模a'&&save[i][j]<='z'))
{
save[i][j]+=b;
if(((save[i][j]>答散='A'&&save[i][j]<='Z')||(save[i][j]>='a'&&save[i][j]<='z'))==0)
save[i][j]-=26;
}
}
}
for(i=0;i<a;i++)
{
puts(save[i]);
}
return(0);
}
#include<string.h>
int main()
{
char save[10][30];
int a,b,i,j;
scanf("%d",&a);
for(i=0;i<a;i++)
{
scanf("%d",&b);
getchar();//加这个
gets(save[i]);
for(j=0;j<30;j++)
{
if((save[i][j]>='A'&&save[i][j]<='Z')||(save[i][j]>='让模a'&&save[i][j]<='z'))
{
save[i][j]+=b;
if(((save[i][j]>答散='A'&&save[i][j]<='Z')||(save[i][j]>='a'&&save[i][j]<='z'))==0)
save[i][j]-=26;
}
}
}
for(i=0;i<a;i++)
{
puts(save[i]);
}
return(0);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询