解密qwert密码

时间:2019-04-29 05:12:10

标签: c encryption

我无法反转此代码以从qwerty解密回abc。

我不知道从哪里开始。 我试图用包含ABC的字符串替换index ... 我还尝试将密文交换为abc,并将索引更改为qwerty,但无济于事。

这是原始代码

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

int main() {
    char* ciphertext = "qwertyuiopasdfghjklzxcvbnm";    // cipher lookup

    char input[500];                                    // input buffer
    printf("Enter text: ");
    fgets(input, sizeof(input), stdin);                 // safe input from user
    input[strlen(input) - 1] = 0;                       // remove the \n (newline)
    int count = strlen(input);                          // get the string length

    char output[count];                                 // output string
    for(int i = 0; i < count; i++) {                    // loop through characters in input
        int index = ((int) input[i]) - 97;              // get the index in the cipher by subtracting 'a' (97) from the current character
        if(index < 0) {
            output[i] = ' ';                            // if index < 0, put a space to account for spaces
        }
        else {
            output[i] = ciphertext[index];              // else, assign the output[i] to the ciphertext[index]
        }
    }
    output[count] = 0;                                  // null-terminate the string

    printf("output: %s\n", output);                     // output the result
}

我的努力什么都没有做,只是重印了我输入的内容

1 个答案:

答案 0 :(得分:2)

因此,您以'a'(ascii 97)开头,并通过char_value - 97将其转换为索引为字符数组的字符串

要转换回纯文本,您需要从qa

一种方法是搜索ciphertext来找到字符(q)出现的索引,然后将97加到该索引以获取原始值。