- 1、加密解密API使用
- 2、文件加密解密
加解密源文件下载:
https://download.csdn.net/download/weixin_45715405/34263462?spm=1001.2014.3001.5501
源代码,共两个文件,des.c 、des.h,引入des.h就可以使用了
#include2、文件加密解密#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; }
#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; }



