【问题标题】:My decryptor doesn't work我的解密器不起作用
【发布时间】:2016-04-17 17:38:19
【问题描述】:

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

凯撒.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;
    }
}

Decryptor.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));
            }
    }

encryptorFunction.c

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

string encryptor(string encryptString, int key) {
    //loops through every character
    string encryptStringCopy = encryptString;
    for (int i = 0, n = strlen(encryptStringCopy); i < n; i++) {
        //converts the specific char to its ascii value
        int ascii = (int) encryptStringCopy[i];
        //makes key 31 is the same as key 5
        key = key % 26;
        //checks if it is an alphabetical letter
        if (isalpha(encryptStringCopy[i])) {
            //checks if lowercase
            if (islower(encryptStringCopy[i])) {
                //handles the corner case of being z since 26 % 26 is not 26 it's 0
                if ((ascii-96+key) == 26) {
                    ascii = 122;
                }else {
                    //takes the lowercase alphabetical numbers to the 1-26 alphabet then checks mod 26 to cycle back and adds it back.
                    ascii = ((ascii-96+key) % 26) + 96;
                }
                //else part is for uppercase case
            }else {
                //same logic as above
                if ((ascii-64+key) == 26) {
                    ascii = 90;

                }else {
                    ascii = ((ascii-64+key) % 26) + 64;
                }
            }
        }
        //converts the ascii back to a char
        char newChar = (char) ascii;
        //sets the new char into the string
        encryptStringCopy[i] = newChar;
    }
    return encryptStringCopy;
}

【问题讨论】:

  • 您是否真的打算让代码文件Decryptor.c 调用函数encryptor(),与Caesar.c 中调用的函数相同?
  • 是的,就是这样解密的。
  • 你能看出它有什么问题吗?
  • 所以你实际上是在尝试每一个密钥来破解密码?
  • 如果是这样,您每次尝试都会覆盖代码字符串。将解码写入一个不同的字符串。

标签: c encryption cryptography cs50


【解决方案1】:

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

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

【讨论】:

  • 我注意到了这一点,实际上我打算修复它,但它不起作用。
  • 你能举个例子吗?我试着做一个,但没有用。
  • 那么您尝试了什么,发生了什么? “没有工作”的含义尽可能模糊。
  • @EpicDefeater 更新:它仍然是相同的字符串,通过不同的指针访问。
  • 那我该怎么做呢?
猜你喜欢
  • 2012-09-16
  • 2012-10-11
  • 1970-01-01
  • 2018-11-22
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多