首页 > 其他 > 详细

文件操作(二进制文件加密解密)

时间:2019-07-22 00:11:43      阅读:441      评论:0      收藏:0      [点我收藏+]

加密

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

void code(char *p,size_t n)
{
    size_t i;
    for(i = 0; i < n; i++)
    {
        p[i] += 3;
    }
}

int main()
{
    FILE *p1 = fopen("./a.txt","r");
    FILE *p2 = fopen("./b.txt","w");
    char buf[1024] = {0};
    
    while(!feof(p1))
    {
        memset(buf,0,sizeof(buf));
        size_t res = fread(buf,sizeof(char),sizeof(buf),p1);
        code(buf,res);
        fwrite(buf,sizeof(char),res,p2);
    }
    fclose(p1);
    fclose(p2);
    return 0;
}

解密:

/***
decode.c
***/
#include<stdio.h>
#include<string.h>

void decode(char *p,size_t n)
{
    size_t i;
    for(i = 0; i < n; i++)
    {
        p[i] -= 3;
    }
}

int main()
{
    FILE *p1 = fopen("./b.txt","rb");
    FILE *p2 = fopen("./c.txt","wb");
    char buf[1024] = {0};
    
    while(!feof(p1))
    {
        memset(buf,0,sizeof(buf));
        size_t res = fread(buf,sizeof(char),sizeof(buf),p1);
        decode(buf,res);
        fwrite(buf,sizeof(char),res,p2);
    }
    fclose(p1);
    fclose(p2);
    return 0;
}

 

文件操作(二进制文件加密解密)

原文:https://www.cnblogs.com/wanghao-boke/p/11222972.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!