Vigenere 密码的一般解释:
Vigenere Cipher 是一种类似于 Caesar Cipher 的加密方法。此 Cipher 将一个单词作为参数,并将单词的字母解释如下 - a 为 0,b 为 1,c 为 2,依此类推。
因此,如果您的输入密钥是 abc 并且您想要加密“hi hello”之类的内容,则输出将需要 h 保持不变,i 移动 1 位,h 移动 2 位,e 再次保持不变(因为它是被移动 0),l 移动 1 位,另一个 l 移动 2,依此类推。
基本思想是每个字母在参数中移动相应的字母,空格和其他标点符号被忽略。如果参数比消息短(在大多数情况下),参数只是围绕消息循环。
我的问题:
我的信息刚刚被 Vignere Cipher 的第一个字母加密。
例如,./vc bc
---->message: ABCDE ABCDE
变为BCDEF BCDEF
。换句话说,整个消息只是被 b 的值移动,而它应该被 bc 的值移动(第一个字母+1,其他每个字母+2。)
尽管处于循环中,但我不明白为什么会发生这种情况。
代码:
# include <cs50.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
int main(int argc, string argv[])
{
string word = argv[1];
int i = 0;
int j = 0;
if (argc != 2 || isalpha(word[j]) == false)
{
printf("Please enter a valid command line argument! \n");
return 1;
}
else if (isalpha(word[j]))
{
printf("Message: ");
string message = GetString();
for (int n = strlen(message); i < n; i++)
{
int plaintext = message[i];
int ciphertext = word[j];
int uppcb = (plaintext - 65 + ciphertext - 65);
int upcipher1 = (uppcb) % 26;
int uplc = (plaintext - 65 + ciphertext - 97);
int upcipher2 = (uplc) % 26;
int lopcb = (plaintext - 97 + ciphertext - 97);
int locipher1 = (lopcb) % 26;
int lolp = (plaintext - 97 + ciphertext - 65);
int locipher2 = (lolp) % 26;
if (isupper(word[j]) && isupper(message[i]))
{
j = (j+1)%strlen(word);
int upcode = (upcipher1 + 65);
printf("%c", upcode);
}
else if (isupper(message[i]) && islower(word[j]))
{
j = (j+1)%strlen(word);
int upcode1 = (upcipher2 + 65);
printf("%c", upcode1);
}
else if (islower(message[i]) && islower(word[j]))
{
j = (j+1)%strlen(word);
int locode = (locipher1 + 97);
printf("%c", locode);
}
else if (islower(message[i]) && isupper(word[j]))
{
j = (j+1)%strlen(word);
int locode1 = (locipher2 +97);
printf("%c", locode1);
}
else
{
printf("%c", message[i]);
}
}
printf("\n");
}
}