//文件解密--解密api调用 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include "des.h" #define SRCFILE "F:/config/outfile.txt" #define OUTFILE "F:/config/decrypt.txt" int decltype_file(const char *srcfile , const char *outfile) { int ret = 0; int srclen = 0; int outlen = 0; unsigned char filebuf[4 * 1024] = {0}; unsigned char outbuf[4 * 1024] = { 0 }; FILE *srcfp; FILE *outfp; srcfp = fopen(srcfile,"rb"); if (srcfp == NULL) { ret = -1; printf("srcfile = fopen(srcfile)\n"); return ret; } outfp = fopen(outfile,"wb"); if (outfp == NULL) { ret = -2; printf("outfp = fopen(outfile)\n"); return ret; } while (!feof(srcfp)) { srclen = fread(filebuf,1,4096,srcfp); if (feof(srcfp)) //是否读到了末尾 { break; } //解密 /*int DesDec_raw( unsigned char *pInData, int nInDataLen, unsigned char *pOutData, int *pOutDataLen)*/ ret = DesDec_raw(filebuf,srclen,outbuf,&outlen); if (ret != 0) { printf("DesDec_raw(filebuf,srclen,outbuf,&outlen)\n"); } //把解密的文案写到 decrypt.txt 中 ret = fwrite(outbuf,1,outlen,outfp); if (ret != outlen) { ret = -3; printf("fwrite(outbuf,1,outlen,outfp) error\n"); return ret; } ret = 0; } //把剩余的部分解密 /*int DesDec( unsigned char *pInData, int nInDataLen, unsigned char *pOutData, int *pOutDataLen)*/ ret = DesDec(filebuf,srclen,outbuf,&outlen); if (ret != 0) { printf("DesDec(srcfile,srclen,outbuf,&outlen) error:%d\n",ret); } ret = fwrite(outbuf,1,outlen,outfp); if (ret != outlen) { ret = -5; printf("fwrite(outbuf, 1, outlen, outfp) error\n"); return ret; } ret = 0; fclose(srcfp); fclose(outfp); return ret; } int main() { int ret = 0; const char *srcfile = SRCFILE; const char *outfile = OUTFILE; ret = decltype_file(srcfile,outfile); if (ret != 0) { printf("decltype_file(srcfile,outfile) error:%d\n",ret); } printf("解密成功!\n"); return 0; }
//问价加密--api借口的调用 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #include "des.h" #define SRCFILE "F:/config/srcfile.txt" #define OUTFILE "F:/config/outfile.txt" int encryption_file(const char *src , const char *out) { int ret = 0; int pinlen = 0; int poutlen; FILE *srcfp; FILE *outfp; unsigned char inbuf[4 * 1024]; unsigned char outbuf[4 * 1024]; srcfp = fopen(src,"rb"); //打开一个二进制文件 if (srcfp == NULL) { ret = -1; printf("srcfp = fopen(src):error\n"); return ret; } outfp = fopen(out,"w+b"); if (outfp == NULL) { ret = -2; printf("outfp = fopen(src):error\n"); return ret; } //不断的取文件 while (!feof(srcfp)) { //读取部分数据 pinlen = fread(inbuf,1,4096,srcfp); //pinlen-->读取的总数,如果pinlen!=4096,失败 if (feof(srcfp)) { break; } /*int DesEnc_raw( unsigned char *pInData, int nInDataLen, unsigned char *pOutData, int *pOutDataLen)*/ ret = DesDec_raw(inbuf,pinlen,outbuf,&poutlen); if (ret != 0) { printf("DesDec_raw(inbuf,pinlen,outbuf,&poutlen) error:%d\n",ret); return ret; } //加密完成的数据放到file2中 ret = fwrite(outbuf,1,poutlen,outfp); if (ret != poutlen) { ret = -2; printf("加密文件写入失败!fwrite(outbuf,1,poutlen,outfp)"); return ret; } ret = 0; } //对剩余的部分 小于4K 进行加密 /*int DesEnc( unsigned char *pInData, int nInDataLen, unsigned char *pOutData, int *pOutDataLen)*/ ret = DesEnc(inbuf, pinlen, outbuf, &poutlen); if (ret != 0) { printf("DesEnc(inbuf, pinlen, outbuf, poutlen) error:%d\n",ret); return ret; } ret = fwrite(outbuf,1,poutlen,outfp); if (ret != poutlen) { ret = -5; printf("fwrite() error\n"); return ret; } ret = 0; fclose(srcfp); fclose(outfp); return ret; } int main() { const char *srcfile = SRCFILE; //源文件 const char *outfile = OUTFILE; //加密后的文件 int ret = 0; ret = encryption_file(srcfile,outfile); if (ret != 0) { printf("encryption_file error:%d\n",ret); return 0; } printf("文件加密完成\n"); return 0; }
相关文件配置:
链接:https://pan.baidu.com/s/1irALQnQzDqaa-8ZBRAasjg
提取码:chen