栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c语言 加密解密文件(文件操作常用函数使用)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c语言 加密解密文件(文件操作常用函数使用)

文章目录
  • 1、加密解密API使用
  • 2、文件加密解密

1、加密解密API使用

加解密源文件下载:

https://download.csdn.net/download/weixin_45715405/34263462?spm=1001.2014.3001.5501

源代码,共两个文件,des.c 、des.h,引入des.h就可以使用了

#include 
#include 
#include "des.h"
#include 

int main(void)
{
	// buf需要加密的数据
	// dst加密之后的数据
    unsigned char buf[] = "abcde";
    unsigned char dst[100] = {0};
    int dstLen = 0;
    
    
    int ret = DesEnc(buf,strlen(buf),dst,&dstLen);
    if(ret != 0 )
    {
        fprintf(stderr,"加密失败%dn",ret);
        exit(1);
    }
    printf("ret = %d,dst=%s dstlen=%dn",ret,dst,dstLen);
    
   
    int len = 0;
    ret = DesDec(dst,dstLen,buf,&len);
    if(ret != 0)
    {
        fprintf(stderr,"解密失败%dn",ret);
        exit(1);
    }
    printf("ret =%d,buf=%s len=%dn",ret,buf,len);

    return 0;
}
2、文件加密解密
#include 
#include 
#include "des.h"
#include 

#define SIZE 4096
void menu()
{
    printf("================n");
    printf("1、加密文件n");
    printf("2、解密文件n");
    printf("3、清屏n");
    printf("4、退出n");
    printf("================n");
}

void EncFile()
{   
    // 以4k大小读文件 
    FILE* rFd = NULL;
    FILE* wFd = NULL;
    char rPath[512] = {0}; // 源文件路径,需要加密
    char wPath[512] = {0}; // 目标路径,保持加密后的文件
    unsigned char srcBuf[SIZE] = {0}; // 4k
    int rLen = 0;
    unsigned char dstBuf[SIZE] = {0}; // 4k
    int wLen = 0;
    int ret = 0;

    printf("请输入需要加密的文件:");
    scanf("%s",rPath);

    printf("请输入加密后的文件:");
    scanf("%s",wPath);

    // 已读方式打开需要加密的文件
    rFd = fopen(rPath,"rb");
    if(rFd == NULL)
    {
        perror("fopen error rPath");
        goto End;
    }

    // 已写方式打开加密后的文件
    wFd = fopen(wPath,"wb");
    if(wFd == NULL)
    {
        perror("fopen error wPath");
        goto End;
    }

    // 循环读取文件
    while(1)
    {
        rLen = fread(srcBuf,1,SIZE,rFd);
        if(rLen < SIZE) // 小于4k跳出
        {
            break;
        }
        // 下来说明,读到4k数据了
        ret = DesEnc_raw(srcBuf,rLen,dstBuf,&wLen);
        if(ret !=0 )
        {
            printf("DesEnc_raw等于4k失败n");
            goto End;
        }
        // 写加密后的文件
        ret = fwrite(dstBuf,1,wLen,wFd);
        if(ret != wLen)
        {
            printf("写加密失败n");
            goto End;
        }
    }
    // 小于4k文件
    ret = DesEnc(srcBuf,rLen,dstBuf,&wLen);
    if(ret !=0 )
    {
        printf("DesEnc_raw等于4k失败n");
        goto End;
    }
    // 写加密后的文件
    ret = fwrite(dstBuf,1,wLen,wFd);
    if(ret != wLen)
    {
        printf("写加密失败n");
        goto End;
    }

End:
    if(rFd != NULL)
    {
        fclose(rFd);
    }
    if(wFd != NULL)
    {
        fclose(wFd);
    }
}

void DecFile()
{
    // 以4k大小读文件 
    FILE* rFd = NULL;
    FILE* wFd = NULL;
    char rPath[512] = {0}; // 源文件路径,需要解密
    char wPath[512] = {0}; // 目标路径,保持解密后的文件
    unsigned char srcBuf[SIZE] = {0}; // 4k
    int rLen = 0;
    unsigned char dstBuf[SIZE] = {0}; // 4k
    int wLen = 0;
    int ret = 0;

    printf("请输入需要解密的文件:");
    scanf("%s",rPath);

    printf("请输入解密后的文件:");
    scanf("%s",wPath);

    // 已读方式打开需要加密的文件
    rFd = fopen(rPath,"rb");
    if(rFd == NULL)
    {
        perror("fopen error rPath");
        goto End;
    }

    // 已写方式打开加密后的文件
    wFd = fopen(wPath,"wb");
    if(wFd == NULL)
    {
        perror("fopen error wPath");
        goto End;
    }

    // 循环读取文件
    while(1)
    {
        rLen = fread(srcBuf,1,SIZE,rFd);
        if(rLen < SIZE) // 小于4k跳出
        {
            break;
        }
        // 下来说明,读到4k数据了
        ret = DesDec_raw(srcBuf,rLen,dstBuf,&wLen);
        if(ret !=0 )
        {
            printf("DesEnc_raw等于4k失败n");
            goto End;
        }
        // 写加密后的文件
        ret = fwrite(dstBuf,1,wLen,wFd);
        if(ret != wLen)
        {
            printf("写加密失败n");
            goto End;
        }
    }
    // 小于4k文件
    ret = DesDec(srcBuf,rLen,dstBuf,&wLen);
    if(ret !=0 )
    {
        printf("DesEnc_raw等于4k失败n");
        goto End;
    }
    // 写加密后的文件
    ret = fwrite(dstBuf,1,wLen,wFd);
    if(ret != wLen)
    {
        printf("写加密失败n");
        goto End;
    }

End:
    if(rFd != NULL)
    {
        fclose(rFd);
    }
    if(wFd != NULL)
    {
        fclose(wFd);
    }
}

int main(void)
{   
    int cmd;
    while (1)
    {
        menu();
        printf("cmd:");
        scanf("%d",&cmd);
        switch (cmd)
        {
        case 1:
            EncFile();
            break;
        case 2:
            DecFile();
            break;
        case 3:
            system("clear");
            break;

        default:
            exit(0);
            break;
        }
    }
    return 0;
} 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/347857.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号