#include<stdio.h>
#define M 100
main()
{
char c[100], key[100];
int lenc, lenk, i=0, j=0;
int a,tmp;
printf("请选择您需要的操作:\n");
printf("1. 加密\n");
printf("2. 解密\n");
printf("您的选择是:");
scanf("%d",&a);
switch (a)
{
case 1:
{
printf("请输入原文:");
getchar();
gets(c);
lenc = strlen(c);
strcpy(c, strupr(c));
printf("请输入密码:");
getchar();
gets(key);
strcpy(key, strupr(key));
lenk = strlen(key);
for(; i<lenc; i++)
{
j = j%lenk;
if(c[i]>64&&c[i]<91)
{
c[i] = (c[i]-65+key[j]-65)%26+65;
j++;
}
}
printf("对应密文是:%s\n", c);
}
break;
case 2:
{
printf("请输入密文:");
getchar();
gets(c);
lenc = strlen(c);
strcpy(c, strupr(c));
printf("请输入密码:");
getchar();
gets(key);
strcpy(key, strupr(key));
lenk = strlen(key);
for(i=0, j=0; i<lenc; i++)
{
j = j%lenk;
if(c[i]>64&&c[i]<91)
{
tmp = c[i]-65-(key[j]-65);
if(tmp>=0)
c[i] = tmp%26+97;
else
c[i] = (tmp+26)%26+97;
j++;
}
}
printf("对应原文是:%s\n", c);
}
break;
default:
printf("输入错误!");
}
}