c - c中加密和解密文件

标签 c linux

这里我有一个用 C 语言加密和解密文件的简单代码,如下所示,

#include <stdio.h>
int Encrypt(char * FILENAME, char * NEW_FILENAME)    
{
    FILE *inFile;   //Declare inFile
    FILE *outFile;  //Declare outFile
    char Byte;
    char newByte;
    int n;
    int i=0;

    inFile = fopen(FILENAME,"rb");
    outFile = fopen(NEW_FILENAME, "w");

    if(inFile == NULL || outfile == NULL){
        printf("Error in opening file");
            return 1;
    } else {
        printf("\nFile Opened, Encrypting");
        while(1){
                while ( !feof( inFile ) ){
                    Byte=fgetc(inFile);
                    newByte=Byte+25;
                    fputc(newByte,outFile);
                } 
            printf("End of File");
            break;
        }
        fclose(inFile);
        fclose(outFile);
    }
}

int Decrypt (char *FILENAME, char *NEW_FILENAME)
{
    FILE *inFile; //Declare inFile
    FILE *outFile; //Declare outFile

    char Byte;
    char newByte;
    int i=0;

    inFile = fopen(FILENAME,"rb");
    outFile = fopen(NEW_FILENAME, "w");

    if(inFile == NULL || outfile == NULL){
        printf("Error in opening file");
        return 1;
    } else {
        printf("File Opened, Decrypting");
        while(1){
            printf(".");
            while ( !feof( inFile ) ){
                Byte=fgetc(inFile);
                newByte=Byte-25;
                fputc(newByte,outFile);
            } 
            printf("End of File");
            break;
        }
        fclose(inFile);
        fclose(outFile);
    }
}

int main()
{
    char encFile[200];
    char newencFile[200];
    char decFile[200];
    char newdecFile[200];

    int choice;

    printf("Enter 1 to Encrypt  / 2 to Decrypt");
    scanf("%d",&choice);

    switch(choice)
    {
    case 1:
        printf("Enter the Source Filename:  ");
        scanf("%s",encFile);
        printf("Enter the Destination Filename:   ");
        scanf("%s",newencFile);
        Encrypt(encFile, newencFile);
        break;
    case 2:
        printf("Enter the Source Filename:   ");
        scanf("%s",decFile);
        printf("Enter the Destination Filename:   ");
        scanf("%s",newdecFile);
        Decrypt(decFile, newdecFile);
        break;
    }
    return 0;
}

此代码有效,但在文件末尾它还包含 "ÿæ" 这样的字符,因此它无法在 linux 的默认文本编辑器中打开,并且无法存储私有(private)详细信息。我想在 discript 文件中得到相同的东西。 请提供帮助,在此先感谢。

最佳答案

您的代码不正确。您可能希望加密文件与原始文件大小相同,但事实并非如此。错误代码是:

    while(1){
        printf(".");
        while ( !feof( inFile ) ){
            Byte=fgetc(inFile);
            newByte=Byte+25;
            fputc(newByte,outFile);
         } 
         printf("End of File");
         break;
    }

它必须是:

while ((Byte = fgetc(inFile)) != EOF) {
  newByte = Byte + 25;
  if (fputc(newByte, outFile) == EOF) {
    /* ERROR */
  }
}
printf("End of File\n");

请注意,printf 语句以换行符 (\n) 结尾。这是您应该遵循的风格。否则,由于缓冲,输出可能不会立即显示。

更多细节:feof 函数只检查是否设置了文件结束标记。但是当 fgetc 函数注意到它已经到达文件末尾时,这个标记还没有被设置。相反,fgetc 函数在这种情况下返回 EOF

哦,请更改变量的类型。

char Byte; /* this is wrong. */
int Byte; /* this is correct. */

有关详细信息,请参阅任何不错的 C 编程入门书籍。

关于c - c中加密和解密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6858073/

相关文章:

c - 在**可移植 C** 中,如何启动将命令的标准输入连接到启动程序的标准输出的命令?

linux - 如何解析 CURL 命令结果的输出?

regex - sed 正则表达式地址范围

python - 缺少 DBus .service 文件

linux - 如何在 Linux shell 上关闭打开(已删除)的文件描述符

c - EM_JS 无法导出 emscripten 中的函数

c - C 中的 Xview 内存缓冲区

c - 执行memmove(void *dst,const void *src,size_t len)后内存位置src包含什么?

c - 为什么在 printf 中需要强制转换?

linux - "find"命令中的未知谓词错误