【无标题】

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#define MAX_CHAR 256 // 定义字符数组的最大长度void encryptFile(const char* inputFile, const char* outputFile, int shift) {
    FILE* input = fopen(inputFile, "r");
    if (input == NULL) {
        printf("Failed to open input file.\n");
        exit(1);
    }
    FILE* output = fopen(outputFile, "w");
    if (output == NULL) {
        printf("Failed to create output file.\n");
        fclose(input);
        exit(1);
    }
    char ch;
    while ((ch = fgetc(input)) != EOF) {
        if (isalpha(ch)) { // 如果是字母,则进行移位加密
            ch = ((ch - 'A' + shift) % 26) + 'A';
        }
        fputc(ch, output); // 将加密后的字符写入输出文件
    }
    fclose(input);
    fclose(output);
}
void decryptFile(const char* inputFile, const char* outputFile) {
    FILE* input = fopen(inputFile, "r");
    if (input == NULL) {
        printf("Failed to open input file.\n");
        exit(1);
    }
    FILE* output = fopen(outputFile, "w");
    if (output == NULL) {
        printf("Failed to create output file.\n");
        fclose(input);
        exit(1);
    }
    char ch;
    while ((ch = fgetc(input)) != EOF) {
        if (isalpha(ch)) { // 如果是字母,则进行移位解密
            ch = ((ch - 'A' - 300 + 26) % 26) + 'A';
        }
        fputc(ch, output); // 将解密后的字符写入输出文件
    }
    fclose(input);
    fclose(output);
}
int main() {
    const char* inputFile = "input.txt"; // 输入文件名
    const char* encryptedFile = "encrypted.txt"; // 加密后的文件名
    const char* decryptedFile = "decrypted.txt"; // 解密后的文件名
    int shift = 300; // 移位数
    // 对文件进行加密操作
    encryptFile(inputFile, encryptedFile, shift);
    printf("Encryption completed.\n");
    // 对文件进行解密操作,还原为原始内容
    decryptFile(encryptedFile, decryptedFile);
    printf("Decryption completed.\n");
    return 0;
}

 

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值