【问题标题】:Vignere Cipher in C: Space not being ignoredC中的Vigenere Cipher:空间未被忽略
【发布时间】:2014-06-06 17:57:07
【问题描述】:

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。或者例如,当我在ABCD ABCD 上运行程序./vignere bc 时,我会期望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 库中可用。

【问题讨论】:

    标签: c cs50 vigenere


    【解决方案1】:

    您需要为变量word 使用另一个索引变量,例如j,因为wordmessage 是两个不同的变量,它们的长度可能不同。

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

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

    【讨论】:

    • @boametaphysica,请使用最新版本更新您的问题以及源示例和输出示例。删除旧版本并使用新版本进行更新。
    • 谢谢,我已经更新了我的代码、我的输出和我面临的新问题。
    • 我明白了...谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多