Vigenere Cipher。代码输出

问题描述 投票:-1回答:1

我一直在研究cs50 pset2,我认为在使用它几天后我已经将vigenere密码降低了。此代码用于获取用户给出的字母参数(argv []),并将其用作用于将用户(字符串)给出的短语按字母索引中的数字加密的密钥。例如,如果你给出参数'abc'和字符串'cat',那么输出应该是'cbv'(移动0,b移动1,移动2)参数也应该环绕,这样如果字符串是更长一点,参数将换行到它的第一个字符并继续直到字符串结束。

这就是我对代码的看法:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>


int main(int argc, string argv[])
{


    if(argc != 2)
        {
            printf("Try again\n");
            return 1;
        }
    string k = (argv[1]);

    int klen = strlen(k);


    for(int x = 0; x < klen; x++)
        {
            if(isalpha(k[x]))
                {
                    if(isupper(k[x]))
                        {
                            k[x] = tolower(k[x]);
                        }

                        k[x] -= 'a';
                }
            else
                {
                    printf("Try again\n");
                    return 1;
                }
        }

    string code = GetString();

    int clen = strlen(code);

    for(int a = 0, b = 0; a < clen; a++)
        {
            if(isalpha(code[a]))
                {
                    int key = k[b%klen];
                    if(isupper(code[a]))
                        {
                            printf("%c", (((code[a] - 'A') + key)%26) + 'A');
                            b++;
                        }
                    else
                        {
                            printf("%c", (((code[a] - 'a') + key)%26) + 'a');
                            b++;
                        }
                }
            else
                {
                    printf("%c", code[a]);
                }
        }
    printf("\n");
}

代码似乎适用于密钥+1的长度。例如,我输入'aaaa'的参数

然后输入一串'bbbbb'并正确接收'bbbbb'。

但是,如果我输入相同的'aaaa'

然后输入一个比键更长的字符串+1'bbbbb'我收到'bbbbb'

我相信我的操作顺序有问题,但是试过移动括号无效。我希望有人可以指出我正确的方向,为什么我的钥匙没有正确包装。

c cs50 vigenere edx
1个回答
0
投票

像这样的代码你最大的风险是所有类似的,重复的条款。只有一个中的错误很难跟踪完成。在处理代码时对密钥进行任何处理都是低效的。

这是一个返工,在处理代码之前完全处理密钥,并尝试将处理简化为一个案例。看看它是否适合你:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main(int argc, string argv[])
{

    if (argc != 2)
    {
        fprintf(stderr, "Try again\n");
        return EXIT_FAILURE;
    }

    string key = strdup(argv[1]);

    size_t key_length = strlen(key);

    for (int x = 0; x < key_length; x++)
    {
        if (isalpha(key[x]))
        {
            if (isupper(key[x]))
            {
                key[x] = tolower(key[x]);
            }

            key[x] -= 'a';
        }
        else
        {
            fprintf(stderr, "Try again\n");
            return EXIT_FAILURE;
        }
    }

    string code = GetString();
    int code_length = strlen(code);

    for (int a = 0, b = 0; a < code_length; a++)
    {
        if (isalpha(code[a]))
        {
            int start = isupper(code[a]) ? 'A' : 'a';

            printf("%c", (((code[a] - start) + key[b++ % key_length]) % 26) + start);
        }
        else
        {
            printf("%c", code[a]);
        }
    }

    printf("\n");

    free(key);

    return EXIT_SUCCESS;
}
© www.soinside.com 2019 - 2024. All rights reserved.