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


加密

#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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM