我的解密器不起作用

时间:2016-04-17 17:38:19

标签: c encryption cryptography cs50

我为pset2创建了两个东西,一个caesar密码加密器和解密器。我的加密器工作,它被称为caesar.c,但我的解密器(decryptor.c)不起作用,我无法弄清楚为什么?这是我的代码。

Caesar.c

 pairs.filter{case (key, value) => key.length < 3} //error: value length is not a member of Any
 pairs.filter(_._1 != "")  //no error, just still keeps the empty keys, too   

Decryptor.c

#include <cs50.h> 
#include <string.h> 
#include <stdio.h> 
#include <math.h> 
#include <stdlib.h> 
#include <ctype.h>
#include <string.h>
#include "encryptorFunction.c"


int main(int argc, string argv[]) {
    //checks for enough args
    if (argc >= 2) {
        //converts the second arg to an integer
        int k = atoi(argv[1]);
        string stringToBeEncrypted = GetString();
        printf("%s\n", encryptor(stringToBeEncrypted, k));
    } else {
        printf("Please add a key argument\n");
        return 1;
    }
}

encryptorFunction.c

    #include <cs50.h> 
    #include <string.h> 
    #include <stdio.h> 
    #include <math.h> 
    #include <stdlib.h> 
    #include <ctype.h>
    #include "encryptorFunction.c"

    // almost same as caesar


    int main(void) {
         const string stringToBeEncrypted = GetString();
            // string stringToBeEncrypted = GetString();
            //checks all possible decryptions
            for(int k=1;k<26;k++) {
                printf("%s\n", encryptor(stringToBeEncrypted, k));
            }
    }

1 个答案:

答案 0 :(得分:2)

您的encryptor()函数会修改输入字符串(参数encryptString)。因此,在相同输入上重复调用它将产生意外结果。

该函数应该创建一个新的字符串来返回,而不是修改它传入的字符串。