c - 将密码字母替换为 QWERTY

标签 c

我正在开发一个使用密码的程序。我需要使用的密码是 qwerty 的字母表。所以...

abcdefghijklmnopqrstuvwxyz
qwertyuiopasdfghjklzxcvbnm

程序需要获取编码 key

qwertyuiopasdfghjklzxcvbnm

并产生解码 key 。

我该如何去做呢?我过去只做过凯撒密码。

最佳答案

以下是将字符串输入转换为 qwerty 密码的 C 代码,假设您仅使用小写字母,并且对字符串使用大小为 500 的缓冲区:

#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
}

关于c - 将密码字母替换为 QWERTY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522250/

相关文章:

C 预处理器优先级

c - 为什么我在删除节点时出现段错误?

c - 使用 c 从应用程序指示器中的 GtkMenu 中删除 GtkMenuItem

c - 使用 OpenCL 传输

c - 如果 close(2) 因 EIO 而失败,文件描述符是否仍会被删除?

c - ARM-C互通

c - C中的算法

c - 带寄存器的 Stm32 定时器计数器

c - pthread_kill() 线程无效

c++ - 将静态局部变量初始化为编译期间未知的值