admin管理员组

文章数量:1639675

文章目录

  • 1、加密\解密API使用
  • 2、文件加密\解密

1、加密\解密API使用

加解密源文件下载:

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

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

#include <stdio.h>
#include <string.h>
#include "des.h"
#include <stdlib.h>

int main(void)
{
	// buf需要加密的数据
	// dst加密之后的数据
    unsigned char buf[] = "abcde";
    unsigned char dst[100] = {0};
    int dstLen = 0;
    
    /* 
    加密enc
    参数:
	    buf需要加密数据
	    strlen(buf)加密数据长度
	    dst 加密之后
	    dstLen加密之后的密文长度
	返回值:
		成功:0 失败:不为0
    */
    int ret = DesEnc(buf,strlen(buf),dst,&dstLen);
    if(ret != 0 )
    {
        fprintf(stderr,"加密失败%d\n",ret);
        exit(1);
    }
    printf("ret = %d,dst=%s dstlen=%d\n",ret,dst,dstLen);
    
   /* 
    解密des
    参数:
	    dst 密文
	    dstLen 密文参数
	    buf 解密之后的明文存放地方
	    len 解密之后的明文长度
	返回值:
		成功:0 失败:不为0
    */
    int len = 0;
    ret = DesDec(dst,dstLen,buf,&len);
    if(ret != 0)
    {
        fprintf(stderr,"解密失败%d\n",ret);
        exit(1);
    }
    printf("ret =%d,buf=%s len=%d\n",ret,buf,len);

    return 0;
}

2、文件加密\解密

#include <stdio.h>
#include <string.h>
#include "des.h"
#include <stdlib.h>

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

本文标签: 文件函数常用语言操作