说明 密文是由明文和密钥的对应字符“相加”而成的。如果明文比密钥短,则只使用部分密钥。同样,如果明文比密钥短,则密钥将被多次使用。
例如,用密钥“CAT”对明文“HELLO”进行编码:
明文:你好
键:CATCA
密文:KFFOP
并使用密钥“FIDO”对明文“DOG”进行编码:
明文:狗
键:FID
密文:JXK
要将两个字母相加,请使用以下约定:A=1、B=2、...、Z=26。如果两个字母的总和大于 26,则从总和中减去 26。例如:A + E = 1 + 5 = 6 = F,D + X = 4 + 24 = 28 = 2 = B。
- 现在我的代码的问题是,如果关键字符较少,我无法重复关键字符以进一步编码纯文本,如何重复关键字符,所以可以进一步编码?
帮帮我,伙计们。
这是我的代码:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],k[50],str1[100];
int i,n;
gets(str);// Input plain text.
gets(str1);//Input key.
for(i=0;str[i]!='\0';i++)
{
n=(str[i]-65+1)+(str1[i]-65+1);//Extracting the numerical position and adding them.
if(n>26) //if numerical value exceeds 26 then subtracting 26 from it and getting the numerical value.
{
n=n-26;
}
str[i]=n+64;//storing the ciphered character.
}
for(i=0;str[i]!='\0';i++)//printing the ciphered characters.
printf("%c",str[i]);
return 0;
}