目的:
1、帮助初学者了解文件的加解密过程
2、加深对文件操作fread、fwrite的的理解
3、培养大家对加解密的始终能力
1.[文件] main.c ~ 418B 下载(112)
#include
#include
#include "des.h"
int main(int argc, char *argv[])
{
int ret = 0;
//char *fromFileName = "d:/dxx_s.txt"; ¼ÓÃÜ
//char *toFileName = "d:/dxx_s_enc.txt";
char *fromFileName = "d:/dxx_s_enc.txt"; //½âÃÜ
char *toFileName = "d:/dxx_s_enc_dec.txt";
//ret = FileSymEnc(fromFileName,toFileName); ¼ÓÃÜ
ret = FileSymDec(fromFileName,toFileName);//½âÃÜ
}
2.[文件] FileSymEncDec.c ~ 4KB 下载(107)
#include
#include
#include "des.h"
//文件加密
int FileSymEnc(const char * pFromFileName,const char * pToFileName)
{
unsigned char buff[4*1024] = {0}; //一次搬运的内存
unsigned char buffOut[4*1024] = {0};
FILE *fFrom = NULL;
FILE *fTO = NULL;
int count = 0,count1 = 0,outLen = 0;
int ret = 0,rev=0;;
fFrom = fopen(pFromFileName,"rb");
if (fFrom == NULL)
{
ret = -3;
printf("open file err!");
goto END;
}
fTO = fopen(pToFileName,"wb");
if (fTO == NULL)
{
ret = -3;
printf("open file err!");
goto END;
}
while(!feof(fFrom))
{
count = fread(buff,1,sizeof(buff),fFrom);
if(count == sizeof(buff)) //读取到4k数据
{
/*
int DesEnc(
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen); 0 结果正确*/
//数据加密
rev = DesEnc_raw(buff,count,buffOut,&outLen);
if(rev != 0)
{
ret = -4;
printf("entry data err!\n");
goto END;
}
count1 = fwrite(buffOut,1,outLen,fTO);
if(outLen == count1)
{
continue;
}
else
{
ret = -1;
printf("read and write err!read:%d,write:%d\n",outLen,count1);
goto END;
}
}
else//读取到不足4k数据
{
//数据加密
rev = DesEnc(buff,count,buffOut,&outLen);
if(rev != 0)
{
ret = -4;
printf("entry data err!\n");
goto END;
}
count1 = fwrite(buffOut,1,outLen,fTO);
if(outLen == count1)
{
if(feof(fFrom))
{
break;
}
}
else
{
ret = -1;
printf("read and write err!read:%d,write:%d\n",outLen,count1);
goto END;
}
}
}
ret = 0;
END:
if (fFrom != NULL)
{
fclose(fFrom);
}
if (fTO != NULL)
{
fclose(fTO);
}
return ret;
}
//文件解密
int FileSymDec(const char * pFromFileName,const char * pToFileName)
{
unsigned char buff[4*1024] = {0}; //一次搬运的内存
unsigned char buffOut[4*1024] = {0};
FILE *fFrom = NULL;
FILE *fTO = NULL;
int count = 0,count1 = 0,outLen = 0;
int ret = 0,rev=0;;
fFrom = fopen(pFromFileName,"rb");
if (fFrom == NULL)
{
ret = -3;
printf("open file err!");
goto END;
}
fTO = fopen(pToFileName,"wb");
if (fTO == NULL)
{
ret = -3;
printf("open file err!");
goto END;
}
while(!feof(fFrom))
{
count = fread(buff,1,sizeof(buff),fFrom);
if(count == sizeof(buff)) //读取到4k数据
{
/*
int DesDec(
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen) */
//数据加密
rev = DesDec_raw(buff,count,buffOut,&outLen);
if(rev != 0)
{
ret = -4;
printf("entry data err!\n");
goto END;
}
count1 = fwrite(buffOut,1,outLen,fTO);
if(outLen == count1)
{
continue;
}
else
{
ret = -1;
printf("read and write err!read:%d,write:%d\n",outLen,count1);
goto END;
}
}
else//读取到不足4k数据
{
//数据加密
rev = DesDec(buff,count,buffOut,&outLen);
if(rev != 0)
{
ret = -4;
printf("entry data err!\n");
goto END;
}
count1 = fwrite(buffOut,1,outLen,fTO);
if(outLen == count1)
{
if(feof(fFrom))
{
break;
}
}
else
{
ret = -1;
printf("read and write err!read:%d,write:%d\n",outLen,count1);
goto END;
}
}
}
ret = 0;
END:
if (fFrom != NULL)
{
fclose(fFrom);
}
if (fTO != NULL)
{
fclose(fTO);
}
return ret;
}
3.[文件] FileSymEncDec.h ~ 308B 下载(99)
#ifndef _OPENFILESYM_H_
#define _OPENFILESYM_H_
#ifdef __cplusplus
extern "C" {
#endif
//文件加密
int FileSymEnc(const char * pFromFileName,const char * pToFileName);
//文件解密
int FileSymDec(const char * pFromFileName,const char * pToFileName);
#ifdef __cplusplus
}
#endif
#endif
4.[文件] des.c ~ 25KB 下载(166)
5.[文件] des.h ~ 624B 下载(131)
/*********************************************************
* des.h
* 用户使用des算法头文件
*
*********************************************************/
#ifndef _OPENDESS_H_
#define _OPENDESS_H_
#ifdef __cplusplus
extern "C" {
#endif
//ab\0defg
//用户使用的函数
int DesEnc(
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen);
//用户使用函数des解密
int DesDec(
unsigned char *pInData,
int nInDataLen,
unsigned char *pOutData,
int *pOutDataLen);
#ifdef __cplusplus
}
#endif
#endif