首页 > 解决方案 > 从 .txt 字符中减去添加的数字不会反向加密?

问题描述

当加密文本文件,然后使用“字节密钥”运行算法以撤消加密时,它只是更改了混乱的文本,实际上并没有反转任何内容。有什么我缺少的概念吗?

void encrypt(char filePath[],int key) {
    FILE * file;
    char byte;
    file = fopen(filePath,"r+");
    while( (byte = fgetc(file)) != EOF) {
        fputc(byte+key,file);
    }
    fclose(file);
}

标签: cencryption

解决方案


这是我的新加密功能的外观

void encrypt(char filePath[],int key) {
    FILE * fileR;
    FILE * fileW;
    char dest[500],src[4];
    int byte;
    fileR = fopen(filePath,"r");
    //Combines filePath with ext for unique filename
    strcpy(dest,filePath);
    strcpy(src,ext);
    fileW = fopen(strcat(dest,src),"w");
    while( (byte = fgetc(fileR)) != EOF) {
        fputc(byte+key,fileW);
    }
    fclose(fileR);
    fclose(fileW);
}

它现在按预期工作。


推荐阅读