PSET 2:Vigenere Cipher部分起作用吗?

时间:2019-08-03 11:12:48

标签: c cs50 vigenere

我创建了以下代码来回答CS50x PSET2:V​​igenere,它在某种程度上可以工作,但是在运行check50时,出现以下一些错误:

:) vigenere.c exists.
:) vigenere.c compiles.
:) encrypts "a" as "a" using "a" as keyword
:( encrypts "barfoo" as "caqgon" using "baz" as keyword - output not valid ASCII text 
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword - output not valid ASCII text 
:) encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword
:( encrypts "world!$?" as "xoqmd!$?" using "baz" as keyword- output not valid ASCII text 
:( encrypts "hello, world!" as "iekmo, vprke!" using "baz" as keyword- output not valid ASCII text 
:) handles lack of argv[1]
:) handles argc > 2
:( rejects "Hax0r2" as keyword - timed out while waiting for program to exit 

似乎正在发生的情况是,该键包含一个高值(即z / Z),这会导致代码跳至下一行,而漏掉看起来是随机序列的内容。例如。在字符串的第一个单词中,它错过了第三个字符,然后在第二个单词中,它错过了第三个和第4个字符,然后第三个单词则错过了第一个字符。我只是不明白发生了什么。

我已经使用printf确保在运行时设置和传递给函数的所有变量都是正确的。函数本身返回正确的响应(对Hax0r2的验证除外)。我已经尝试通过将结果与在线vigenere密码工具进行比较来进行调试。

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

int Validate1(int argc);
int Validate2(string argv);
void Cypher(string x);
void KeyCalc(string argv);

string MESSAGE;
int LENGTH;
int *KEY;
int COUNTER = 0;

int main(int argc, string argv[])
{
    //Check if right amount of arguments are supplied
    int Val1 = Validate1(argc);

    if (Val1 == 0)
    {
        //Check if argument is a string of chars
        int Val2 = Validate2(argv[1]);

        if (Val2 == 0)
        {
            //get the string length
            LENGTH = strlen(argv[1]);

            //Dynamically update KEY array length
            KEY = (int *)malloc(LENGTH * sizeof(*KEY));
            if (KEY == NULL)
            {
                fprintf(stderr, "malloc failed\n");   
            }

            //calculate the key
            KeyCalc(argv[1]);

            //get the message from the user to be encrypted
            MESSAGE = get_string("plaintext: ");
            printf("ciphertext: ");

            //encrypt message from user
            Cypher(argv[1]);
            free(KEY);
            return 0;
        }
        else
        {
            //validation failed
            printf("Usage: ./vigenere keyword\n");
            return 1;
        }
    }
    else
    {
        //validation failed
        printf("Usage: ./vigenere keyword\n");
        return 1;
    }
}

//Validate the number of arguments supplied
int Validate1(int argc)
{
    if (argc != 2)
    {
        return 1;
    }
    else
    {
        return 0;   
    }
}

//Validate the argument is a string
int Validate2(string argv)
{
    int k = 0;

    //loop through all characters in argument line string and check if alphabetic 
    for (int i = 0; i < LENGTH; i++)
    { 
        if isalpha(argv[i])
        {
            //Do Nothing
        }
        else
        {
            k++; 
        }
    }

    //k counts the number of non-alphabetic characters, so if > 0 then invalid input
    if (k > 0)
    {
        return 1;
    }
    else
    {
        return 0;    
    }
}


void Cypher(string x)
{
    //identify the length of the message to be coded
    int Mlength = strlen(MESSAGE);

    //identify the length of the key
    int Slen = strlen(x);

    //cycle through all characters in message supplied by user
    for (int i = 0; i < Mlength; i++)
    {
        // loop through key
        if (COUNTER > Slen - 1)
        {
            COUNTER = 0;
        }
        //check if the character is alphabetic
        if (isalpha(MESSAGE[i]))
        {
            //convert the character to ASCII int value
            char l = MESSAGE[i];

            //add key value to message value and wrap around ascii mapping
            if (isupper(MESSAGE[i]))
            {
                l = l + KEY[COUNTER];
                if (l > 'Z')
                {
                    l = l - 26;    
                }
            }
            else
            {
                l = l + KEY[COUNTER];
                if (l > 'z')
                {
                    l = l - 26;    
                }    
            }

            //convert value back into character and store in array
            MESSAGE[i] = (char) l;
            // print character 
            printf("%c", MESSAGE[i]);
            COUNTER++;
        }
        else
        {
            //character is 'numeric' or 'symbol' or 'space' just display it
            printf("%c", MESSAGE[i]);
        }
    }
    printf("\n");
}

void KeyCalc(string argv)
{
    //convert key entry to values A/a = 0 to Z/z = 26
    for (int i = 0; i < LENGTH; i++)
    {
        char k = argv[i];
        if (islower(argv[i]))
        {
            KEY[i] = k - 'a'; 
        }
        else
        {
            KEY[i] = k - 'A'; 
        }      
    }    
}
  • 使用“ baz”作为关键字将“ barfoo”加密为“ caqgon”
  • 使用“ BaZ”作为关键字将“ BaRFoo”加密为“ CaQGon”
  • 加密“ world!$?”为“ xoqmd!$?”使用“ baz”作为关键字
  • 加密“你好,世界!”如“ iekmo,vprke!”使用“ baz”作为关键字
  • 拒绝将“ Hax0r2”作为关键字

1 个答案:

答案 0 :(得分:2)

根据凯撒pset的规格:

  

... Caesar的算法(即密码)通过以下方式加密邮件   将每个字母“旋转” k个位置。更正式地说,如果p是一些   纯文本(即未加密的消息),p i 是   p,而k是秘密密钥(即非负整数),则每个   密文c中的字母c i 计算为

     

c i =(p i + k)%26

此算法(无论在哪种情况下)都无法做到这一点:

 l = l + KEY[COUNTER];
                if (l > 'Z')
                {
                    l = l - 26;    
                }

This walkthrough从9:30开始,是如何实施“转变”的良好入门。

此代码中出现问题的最直接原因是,此l = l + KEY[COUNTER];可能会产生ascii range之外的结果。在CS50实现中,char默认为 signed char。因此,例如,“ r” +“ z”(如用“ baz”加密的“ barfoo”中所示)将产生-117。