0

Vignere 密码的一般解释:

Vignere 密码是一种类似于凯撒密码的加密方法。此 Cipher 将一个单词作为参数,并将单词的字母解释如下 - a 为 0,b 为 1,c 为 2,依此类推。

因此,如果您的输入密钥是“abc”并且您想要加密“hi hello”之类的内容,则输出将需要 h 保持不变,i 移动 1 位,h 移动 2 位,e 再次保持相同(如它被移动 0),l 移动 1 位,另一个 l 移动 2,依此类推。

基本思想是每个字母在参数中移动相应的字母,空格和其他标点符号被忽略。如果参数比消息短(在大多数情况下),参数只是围绕消息循环。


我的问题:

唯一存在的问题是它似乎只是为句子的第一个单词做正确的事情。它没有忽略空格。

我在下面粘贴了我的新代码。

例如,对于程序./vigenere bacon和消息Meet me at the park at eleven am,我希望Negh zf av huf pcfx bt gzrwep oz作为输出。但是,我得到的是Negh ne og tjs qaty bt svfvgb bm. 或者例如,当我在 上运行程序./vignere bcABCD ABCD,我会期望BDDF BDDF. 相反,我得到的是BDDF CCEE(当它应用类似于bcbcbcbcb而不是bcbc bcbc.

虽然第一个单词正在正确更改,但它似乎正在加密整个句子,而它应该只加密字母 - 我已经使用 isalpha 命令指定了这一点。

这就是为什么它看起来有点令人费解。

代码:

# 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");     
    }


}

注意:这实际上是 C 而不是 C++,并且 GetInt() 和 GetString() 等一些命令在我正在使用的 CS50 库中可用。

4

1 回答 1

1

您需要变量的另一个索引变量,word例如jsincewordmessage是两个不同的变量,它们可能具有不同的长度。

因此,if (isupper(word[i]) && isupper(message[i]))您将改为使用if (isupper(word[j]) && isupper(message[i]))索引变量j从零开始计数到strlen(word) - 1然后从零重新开始的东西。

它适用于您的特殊情况的原因是因为messageword的长度相同,因此当到达字符串结尾时,无需word再次将 over 的索引重新设置为零word

于 2014-06-06T18:14:18.540 回答