c语言移位加解密

#include <stdio.h>  
  
// 移位加密函数  
void encryptFile(const char* inputPath, const char* outputPath, int key) {  
    FILE* inputFile = fopen(inputPath, "rb");  
    FILE* outputFile = fopen(outputPath, "wb");  
    char buffer[1024];  // 定义一个缓冲区用于读取和写入数据块  
    size_t bytesRead;  
  
    while (1) {  
        bytesRead = fread(buffer, sizeof(char), sizeof(buffer), inputFile);  
        if (bytesRead <= 0) {  
            break;  // 读取结束或发生错误  
        }  
        for (size_t i = 0; i < bytesRead; i++) {  
            buffer[i] = buffer[i] + key;  // 使用加法进行移位加密  
            if (buffer[i] > 127) {  // 处理溢出情况,确保仍在ASCII码范围内  
                buffer[i] = buffer[i] - 128;  
            }  
        }  
        fwrite(buffer, sizeof(char), bytesRead, outputFile);  // 将加密后的数据块写入输出文件  
    }  
  
    fclose(inputFile);  
    fclose(outputFile);  
}  
  
// 移位解密函数  
void decryptFile(const char* inputPath, const char* outputPath, int key) {  
    encryptFile(inputPath, outputPath, -key);  // 解密与加密使用相同的操作,但使用负的移位数  
}  
  
int main() {  
    const char* inputFilePath = "input.txt";  
    const char* encryptedFilePath = "encrypted.txt";  
    const char* decryptedFilePath = "decrypted.txt";  
    int shiftKey = 200;  // 移位加密所使用的密钥  
  
    // 加密文件  
    encryptFile(inputFilePath, encryptedFilePath, shiftKey);  
  
    // 解密文件  
    decryptFile(encryptedFilePath, decryptedFilePath, shiftKey);  
  
    return 0;  
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值