C中的Vigenere密码

Vigenere cipher in C

我在调试我在 C 中实现的 Vigenere 密码时遇到问题。当使用文件输入(-f 标志)时出现错误,其中文件包含少于 6 个字符(+ 1 EOF),它吐出一些随机字符以及预期的输入,我无法弄清楚为什么会这样,尽管我怀疑它与我问题的第二部分有关,即在使用 fread() 时,我注意到 this

if( fread(fcontents, fsize, sizeof(char), file) != 1 ) {...}

将 运行 没有问题,而这个

if( fread(fcontents, sizeof(char), fsize, file) != 1 ) {...}

不起作用(即导致 fread() 到 return 1 并触发其下方的错误处理代码),根据 [=13= 的答案,我希望相反], 但我可能只是误解了一些东西。

我的完整代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>

#define ENC 0 //Encrypt mode
#define DEC 1 //Decrypt mode
#define INP 0 //Commandline input mode
#define FLE 1 //File input mode

typedef struct {
    char *password;
    char *file_name;
    char *input;
    int edmode;
    int ifmode;
} options;

void string_clean(char *source)
{
    char *i = source;
    char *j = source;

    while(*j != 0) {
        *i = *j++;
        if( *i != ' ' && (isupper(*i) || islower(*i)) )
            i++;
    }

    *i = 0;
}

char *ftostr(char *file_name) //Takes a file name as input and returns a char* to the files contents, returns NULL pointer on faliure. Allocated string must be freed after use by parent function to prevent memory leaks.
{
    FILE *file;
    long int fsize;
    char *fcontents;

    if( !(file = fopen(file_name, "r")) ) {
        fprintf(stderr, "Error opening file \"%s\"!\n", file_name);
        return NULL;
    }

    fseek(file, 0L, SEEK_END);
    fsize = ftell(file);
    rewind(file);

    if( !(fcontents = malloc((fsize + 1) * sizeof(char))) ) {
        fclose(file);
        fprintf(stderr, "Error allocating memory!");
        return NULL;
    }

    if( fread(fcontents, fsize, sizeof(char), file) != 1 ) { //suspected buggy line
        fclose(file);
        free(fcontents);
        fprintf(stderr, "Error copying file to memory!\n");
        return NULL;
    }

    fclose(file);
    return fcontents;
}

options parse_opts(int argc, char *argv[])
{
    int c;
    options args;

    args.edmode     = ENC; //enable encrypt mode by default
    args.ifmode     = INP; //enable commandline input mode by default
    args.file_name  = NULL;
    args.password   = NULL;
    args.input      = NULL;
    opterr          = 0;

    while((c = getopt(argc, argv, "dep:i:f:")) != -1) {
        switch(c) {
            case 'e':
                args.edmode     = ENC;
                break;
            case 'd':
                args.edmode     = DEC;
                break;
            case 'p':
                args.password   = optarg;
                break;
            case 'i':
                args.input      = optarg;
                args.ifmode     = INP;
                break;
            case 'f':
                args.file_name  = optarg;
                args.ifmode     = FLE;
                break;
            case '?':
                if(optopt == 'f' || optopt == 'p' || optopt == 'i')
                    fprintf(stderr, "Option -%c requires an argument.\n", optopt);
                else if(isprint(optopt))
                    fprintf(stderr, "Unknown option `-%c'.\n", optopt);
                else
                    fprintf(stderr, "Unknown option character `\x%x'.\n", optopt);

                fprintf(stderr, "Usage: %s (-f file_name || -i input) -p password [options]\n"
                                "Optional: -e -d\n", argv[0]);
                exit(-1);
        }
    }

    return args;
}

char *vigenere_dec(char cipher_text[], char cipher[])
{
    char *plain_text;

    string_clean(cipher_text);
    string_clean(cipher);

    int plain_text_len = strlen(cipher_text);
    int cipher_len = strlen(cipher);

    if( !(plain_text = malloc((plain_text_len + 1) * sizeof(char))) )
        return 0;

    for(int i = 0; i < cipher_len; i++) {
        if(isupper(cipher[i]))
            cipher[i] -= 'A';
        else if(islower(cipher[i]))
            cipher[i] -= 'a';
    }

    for(int i = 0, j = 0; i < plain_text_len; i++, j++) {
        if(j == cipher_len)
            j = 0;

        if(isupper(cipher_text[i]))
            cipher_text[i] -= 'A';
        else if(islower(cipher_text[i]))
            cipher_text[i] -= 'a';

        plain_text[i] = ((cipher_text[i] - cipher[j]) % 26);
        if(plain_text[i] < 0)
            plain_text[i] += 26;
        plain_text[i] += 'A';
    }
    return plain_text;
}

char *vigenere_enc(char plain[], char cipher[])
{
    char *cipher_text;

    string_clean(plain);
    string_clean(cipher);

    int plain_len   = strlen(plain);
    int cipher_len  = strlen(cipher);

    if(plain_len == 0 || cipher_len == 0)
        return NULL;

    if( !(cipher_text = malloc((plain_len + 1) * sizeof(char))) )
        return NULL;

    for(int i = 0; i < cipher_len; i++) {
        if(isupper(cipher[i]))
            cipher[i] -= 'A';
        else if(islower(cipher[i]))
            cipher[i] -= 'a';
    }

    for(int i = 0, j = 0; i < plain_len; i++, j++) {
        if(j == cipher_len)
            j = 0;

        if(isupper(plain[i]))
            plain[i] -= 'A';
        else if(islower(plain[i]))
            plain[i] -= 'a';

        cipher_text[i] = ((plain[i] + cipher[j]) % 26) + 'A';
    }
    return cipher_text;
}

int main(int argc, char *argv[])
{
    options args;
    char *output_text = NULL;

    args = parse_opts(argc, argv);

    if(args.password == NULL) {
        fprintf(stderr, "Password uninitialised!\n");
        exit(-1);
    }

    if(args.input == NULL && args.file_name == NULL) {
        fprintf(stderr, "Input stream uninitialised!\n");
        exit(-1);
    }

    if(args.ifmode == INP) {
        if(args.edmode == ENC)
            output_text = vigenere_enc(args.input, args.password);
        else if(args.edmode == DEC)
            output_text = vigenere_dec(args.input, args.password);
    } else if(args.ifmode == FLE) {
        if( !(args.input = ftostr(args.file_name)) )
            return -1;
        if(args.edmode == ENC)
            output_text = vigenere_enc(args.input, args.password);
        else if(args.edmode == DEC)
            output_text = vigenere_dec(args.input, args.password);
        free(args.input);
    }

    puts(output_text);
    free(output_text);

    return 0;
}

错误是未终止的字符串。您使用

为终止符留出了空间
if( !(plain_text = malloc(plain_text_len + 1)) )  // (simplified)

但是在你设置之后,例如

plain_text[i] += 'A';

您需要以

结束字符串
plain_text[i+1] = '[=12=]';

或当字符串完成时。

对于第二部分,您引用了另一个问题,但没有看到 fread() returns 阅读的项目数。因此,如果你交换它的 sizecount 参数,期望得到不同的结果(除非 fsize == 1)。

所以你可以使用

if( fread(fcontents, fsize, 1, file) != 1 ) {...}

或这个

if( fread(fcontents, 1, fsize, file) != fsize ) {...}

请注意,我将 sizeof(char) 更改为 1,因为根据定义,它是。