#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main (){
char s[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char b[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char result[1000];
char a[1000];
char c;
int i,l,count=0,t=0,k,p;
int f,h;
flag2: printf("凯撒密码加解密:\n请输入字母文本:\n");
gets(a);
l=strlen(a);
for(i=0;i<l;i++)
{
if(((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z'))&&(l<=1000))
{
t++;
}
else
{ count++;
}
}
if(count>0)
{ printf("输入错误或超过输入范围!请重新输入!\n");
count=0;
goto flag2;
}
printf("\n");
flag3:printf(" 1:加密\n 2:解密\n 3.结束\n");
h=scanf("%d",&f);
if(h==1)
{
if (f== 1)
{
flag1: printf("Input the Key(1~26):" );
if(scanf("%d",&k)==1)
{
if (k>0&&k<=26)
{
for(i=0;i<l;i++)
{
if(a[i]>='A'&&a[i]<='Z')
{
result[i]=b[((a[i]-'A')+k)%26];
}
else if (a[i]>='a'&&a[i]<='z')
{
result[i]=s[((a[i]-'a')+k)%26];
}
else result[i]=a[i];
printf("%c",result[i]);
}
printf("\n\n");
goto flag3;
}
else
{
printf("Please Input 1~26!\n");
goto flag1;}
}
else
{
printf("输入错误!请重新输入!\n");
fflush(stdin);
goto flag1;}
}
else if (f == 2){
for (k=1;k<=26;k++)
{printf("k=%d,",k);
for(i=0;i<l;i++){
if(a[i]>='A'&&a[i]<='Z')
{
p=((a[i]-'A')-k);
while(p<0)p+=26;
result[i]=b[p];
}
else if (a[i]>='a'&&a[i]<='z')
{
p=((a[i]-'a')-k);
while(p<0)p+=26;
result[i]=s[p];
}
else result[i]=a[i];
printf("%c",result[i]);
}
printf("\n");
}
printf("\n\n");
goto flag3;
}
else (f == 3);
{
goto flag4;
}
}
else
{
printf("没有这个选项!请重新输入!\n");
fflush(stdin);
goto flag3;
}
flag4:return 0;
}
古典密码之凯撒密码
最新推荐文章于 2022-11-06 22:14:53 发布