Vigenere 密码解码无法正常工作

Vigenere cipher decoding not working properly

我正在编写一个 vigenere 密码,用作可加载内核模块的一部分。因此,我不能使用字符串库。这就是为什么我包含单独的循环来获取密钥和输入长度的原因。 include 语句和 main 仅包含用于测试。

每当我使用不是全部大写的键时,程序都会由于地址清理程序错误而崩溃。只有当我包含应该将键输入转换为大写的代码时才会发生这种情况。我不确定这是哪里不正确,因为我转换为大写的方法与我过去在堆栈溢出中看到的相同。如果没有这些行,它将 运行 正常并且不会崩溃。

此外,解码阶段没有按预期工作,它给出了不正确的解码消息。我能想到的唯一可能的问题是我允许输入包含 space 的地方,但我认为这不是问题,因为我只是在字符串中添加 space。我非常感谢您提供一些关于我在这段代码中出错的地方的意见。 尝试转换为大写时的完整错误是:

==25694==ERROR: AddressSanitizer: BUS on unknown address 0x00010c98ae80 (pc 0x00010c989ac2 bp 0x7ffee3277610 sp 0x7ffee3277440 T0)
    #0 0x10c989ac1 in encrypt (vig:x86_64+0x100001ac1)
    #1 0x10c98aca1 in main (vig:x86_64+0x100002ca1)
    #2 0x7fff5c419ef8 in start (libdyld.dylib:x86_64+0x16ef8)

==25694==Register values:
rax = 0x000000010c98ae80  rbx = 0x00007ffee3277440  rcx = 0x000000010c98ae4b  rdx = 0x0000000000000004  
rdi = 0x000000010c98ae80  rsi = 0x000000010c98ae4b  rbp = 0x00007ffee3277610  rsp = 0x00007ffee3277440  
 r8 = 0x00001000219315d0   r9 = 0x0000000000000004  r10 = 0x0000000000000000  r11 = 0x0000000000000000  
r12 = 0x0000000000000000  r13 = 0x0000000000000000  r14 = 0x0000000000000000  r15 = 0x0000000000000000  
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: BUS (vig:x86_64+0x100001ac1) in encrypt
==25694==ABORTING
Abort trap: 6

这是我的代码:

#include<stdio.h>
#include<stdlib.h>
char* encrypt(char* input, char* key)
{
    int keyLength = 0;
    int inputLength = 0;
    int i;
    int j;
    for(i = 0; key[i] != '[=11=]'; i++) //Get the length of the key
    {
        keyLength++;
    }
    for(i = 0; input[i] != '[=11=]'; i++) //Get the length of the input
    {
        inputLength++;
    }
    for (i = 0; i < keyLength; i++)
    {
        if(key[i] >= 'a' && key[i] <= 'z')
        {
             key[i] = key[i] - 'a' + 'A';
        }
    }

    char* encryptedMessage = (char *)malloc((inputLength+1)*sizeof(char)); //Malloc for the encrypted message
    char fixedKey[inputLength + 1];
    if(inputLength < keyLength)
    {
        for(i =0; i < inputLength; i++)
        {
            fixedKey[i] = key[i];
        }
    }
    for(i = 0, j = 0; i < inputLength; ++i, ++j) //If the key length is shorter than message length, loop the key to correct length
    {
        printf("Entered Loop\n");
        if(j == keyLength)
            j = 0;
        fixedKey[i] = key[j];
    }
    fixedKey[i] = '[=11=]';
    for(i = 0; i < inputLength; ++i) //Encryption
    {
        if(input[i] == ' ')
        {
            encryptedMessage[i] = ' ';
            continue;
        }
        encryptedMessage[i] = ((input[i] + fixedKey[i]) % 26) + 'A';
    }
    encryptedMessage[i] = '[=11=]';
    return encryptedMessage;
}
char* decrypt(char* input, char* key)
{
    int keyLength = 0;
    int inputLength = 0;
    int i;
    int j;
    for(i = 0; key[i] != '[=11=]'; i++) //Get the length of the key
    {
        keyLength++;
    }
    for(i = 0; input[i] != '[=11=]'; i++) //Get the length of the input
    {
        inputLength++;
    }
    for (i = 0; i <keyLength; i++)
    {
        if(key[i] >= 'a' && key[i] <= 'z')
        {
            key[i] = key[i] - 'a' + 'A';
        }
    }
    char* decryptedMessage = (char *)malloc((inputLength+1)*sizeof(char));
    char fixedKey[inputLength + 1];
    if(inputLength < keyLength)
    {
        for(i =0; i < inputLength; i++)
        {
            fixedKey[i] = key[i];
        }
    }
    for(i = 0, j = 0; i < inputLength; ++i, ++j) //Fix the key length if needed
    {
        if(j == keyLength)
            j = 0;
        fixedKey[i] = key[j];
    }
    fixedKey[i] = '[=11=]';
    for(i = 0; i < inputLength; ++i) //Decryption
    {
        if(input[i] == ' ')
        {
            decryptedMessage[i] = ' ';
            continue;
        }
        decryptedMessage[i] = (((input[i] - fixedKey[i]) + 26) % 26) + 'A';
    }
    decryptedMessage[i] = '[=11=]';
    return decryptedMessage;
}
int main()
{
    char* encrypted = encrypt("The quick brown fox jumps over lazy dogs","key");
    char* decrypted = decrypt(encrypted,"key");
    printf("Encrypted string is: %s\nDecrypted String is: %s\n",encrypted,decrypted);
    return 0;
}

您正在尝试修改字符串文字。

您称 encrypted 为:

encrypt("The quick brown fox jumps over lazy dogs","key");

然后在encrypt中修改第二个参数:

key[i] = key[i] - 'a' + 'A';

字符串文字是只读的,尝试修改它们会调用 undefined behavior.

制作 key 的本地副本并在执行 upper/lower 大小写转换时修改它。