文件CaesarShift加密

时间:2014-09-03 23:25:31

标签: c file encryption

我试图使用ceasarshift来加密文件,正在创建一个名为.enc的新文件,但它是空的。 这是我的代码:

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

char* getFileExtension(const char*);

int main(int argc, char *argv[])
{
    const int shift = (int)argv[1];
    int byte;
    const char *fileName = (char*)argv[2];
    char *fileExtension = getFileExtension(fileName);
    char *newFileName = (char*)fileName;
    FILE *f_in;
    FILE *f_out;
    f_in = fopen(fileName, "r");
    if (strcmp(fileExtension, "enc") == 0)
    {
        // We want to decrypt the file
        strcat(newFileName, ".dec");
        f_out = fopen(newFileName, "w");
        while ((byte = fgetc(f_in) != EOF))
        {
            fputc(byte - shift, f_out);
        }
    }
    else
    {
        // We want to encrypt the file
        strcat(newFileName, ".enc");
        f_out = fopen(newFileName, "w");
        while ((byte = fgetc(f_in) != EOF))
        {
            fputc(byte + shift, f_out);
        }
    }
    fclose(f_in);
    fclose(f_out);
    return 0;
}

char* getFileExtension(const char *fileName)
{
    char *extension;
    int foundExtension = 0;
    while (*fileName)
    {
        if (foundExtension == 1)
        {
            *extension++ = *fileName++;
        }
        if (*fileName == '.')
        {
            foundExtension = 1;
        }
        fileName++;
    }
    return extension;
}

我制作了一个名为CryptoFile的txt文件,其中包含以下文字:

This is a crypto test file !

这是我在运行exe时在控制台中发送的参数:

FileCaesarShift.exe 15 CryptoFile

因此转移为15,加密/解密的文件被称为&#34; CryptoFile&#34; 虽然正在创建一个名为CryptoFile.enc的文件,但它只是空的。 有人能告诉我我做错了吗?

好的,我发现我需要传递CryptoFile.txt,包括&#34; .txt&#34;但我希望将其从将要创建的新文件的名称中删除,而不是创建CryptoFile.txt.enc我只想要CryptoFile.enc所以我创建了一个removeExtension函数,但我的程序崩溃,这里是新的代码:

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

char* getFileExtension(const char*);
void removeFileExtension(char*);

int main(int argc, char *argv[])
{
    const int shift = (int)argv[1];
    int byte;
    const char *fileName = (char*)argv[2];
    char *fileExtension = getFileExtension(fileName);
    char *newFileName = (char*)fileName;
    removeFileExtension(newFileName);
    printf("newfilename value is %s", *newFileName);
    FILE *f_in;
    FILE *f_out;
    f_in = fopen(fileName, "r");
    if (strcmp(fileExtension, "enc") == 0)
    {
        // We want to decrypt the file
        strcat(newFileName, ".dec");
        f_out = fopen(newFileName, "w");
        while ((byte = fgetc(f_in)) != EOF)
        {
            fputc(byte - shift, f_out);
        }
    }
    else
    {
        // We want to encrypt the file
        strcat(newFileName, ".enc");
        f_out = fopen(newFileName, "w");
        while ((byte = fgetc(f_in)) != EOF)
        {
            printf("byte is %d\n", byte);
            fputc(byte + shift, f_out);
        }
    }
    fclose(f_in);
    fclose(f_out);
    return 0;
}

char* getFileExtension(const char *fileName)
{
    char *extension;
    int foundExtension = 0;
    while (*fileName)
    {
        if (foundExtension == 1)
        {
            *extension++ = *fileName++;
        }
        if (*fileName == '.')
        {
            foundExtension = 1;
        }
        fileName++;
    }
    return extension;
}

void removeFileExtension(char *fileName)
{
    while (*fileName)
    {
        if (*fileName == '.')
        {
            *fileName == '\0';
            break;
        }
        fileName++;
    }
}

最新编辑:

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

void copyFileExtension(char*, char*);
int getFileNameLengthWithoutExtension(char*);
int getFileExtensionLength(char*);
void copyFileNameWithoutExtension(char*, char*);

int main(int argc, char *argv[])
{
    int shift = atoi(argv[1]);
    int byte;
    char *fileName = (char*)argv[2];
    char *fileExtension = malloc(getFileExtensionLength(fileName) + 1);
    copyFileExtension(fileExtension, fileName);
    char *newFileName = malloc(getFileNameLengthWithoutExtension(fileName) + 5);
    copyFileNameWithoutExtension(newFileName, fileName);
    FILE *f_in;
    FILE *f_out;
    f_in = fopen(fileName, "r");
    if (strcmp(fileExtension, "enc") == 0)
    {
        // We want to decrypt the file
        puts("dec");
        strcat(newFileName, ".dec");
        f_out = fopen(newFileName, "w");
        while ((byte = fgetc(f_in)) != EOF)
        {
            fputc(byte - shift, f_out);
        }
    }
    else
    {
        puts("enc");
        // We want to encrypt the file
        strcat(newFileName, ".enc");
        f_out = fopen(newFileName, "w");
        while ((byte = fgetc(f_in)) != EOF)
        {
            fputc(byte + shift, f_out);
        }
    }
    fclose(f_in);
    fclose(f_out);
    return 0;
}

void copyFileExtension(char *fileExtension, char *fileName)
{
    char *token = strtok(fileName, ".");
    token = strtok(NULL, ".");
    strcpy(fileExtension, token);
}

int getFileNameLengthWithoutExtension(char *fileName)
{
    if (*fileName && *fileName != '.')
    {
        return 1 + getFileNameLengthWithoutExtension(++fileName);
    }
    return 0;
}

int getFileExtensionLength(char *fileName)
{
    int foundExt = 0;
    int len = 0;
    while(*fileName)
    {
        if (foundExt == 1)
        {
            len++;
        }
        if (*fileName == '.')
        {
            foundExt = 1;
        }
        fileName++;
    }
    printf("ext len is %d\n", len);
    return len;
}

void copyFileNameWithoutExtension(char* dest, char *source)
{
    char *fileNameWithoutExtension = strtok(source, ".");
    strcpy(dest, fileNameWithoutExtension);
}

1 个答案:

答案 0 :(得分:0)

以下是一些可以快速浏览的事情:

  • const int shift = (int)argv[1]不会将输入参数转换为整数值15.而是将char*中的argv[1]指针(即地址)转换为int并且将该值(通常非常大)分配给shift。要将输入参数实际转换为int,请考虑使用atoi
  • 您没有为newFileName分配内存,后来写入可能超过argv[2]指向的“常量”输入newFileName的分配长度(导致未定义的行为)。
  • 假设程序在此之前没有崩溃,=!=之间的运算符优先级规则使得while循环参数等同于byte = (fgetc(f_in) != EOF)。所以在你到达EOF之前byte == 1。如果您的移位为15,则最终会创建一个充满不可读控制字符的文件(ascii character 16)。鉴于第一个项目符号中不可预知的shift,谁知道实际上要写入文件的内容。

虽然这些可能不是您的具体错误,但以下情况也可能导致问题:

  • 在使用文件之前,您也没有检查文件是否已成功打开
  • 您是否考虑过接近有效字符末尾的字母会发生什么?