文件加解密(任意文件类型的文件)

直接上代码:

支持任意类型的文件的加密和解密
可以自由修改密钥
需要修改文件路径

文件加解密.

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

int file_encrypt(const char* file1,const char* file2)
{
   
   
	int ret=0;
	unsigned char buf[4096] = {
   
    0 };//因为是以字节为单位操作内存,所以定义一个char类型的数组来存储
	unsigned char buf_crypt[4096] = {
   
    0 };//加密后的buf

	FILE* fd1 = NULL;//输入
	FILE* fd2 = NULL;//加密后

	int tmp_len=0;
	int tmp_crypt_len=0;

	fd1=  fopen(file1, "rb");
	if (fd1 == NULL)
	{
   
   
		ret = -1;
		printf("func fopen file1 err\n");
		goto END;
	}

	fd2 = fopen(file2, "wb");
	if (fd2 == NULL)
	{
   
   
		ret = -1;
		printf("func fopen  file2 err\n");
		goto END;
	}


	while(!feof(fd1))
	{
   
    
		tmp_len=fread(buf, 1, 4096, fd1);//一次读取4K数据,以单字节的形式读取,一次读取4096个,若不够4096则返回最后读取到的个数

	//	printf("tmp_len=%d\n", tmp_len);
	//	printf("buf=%s\n", buf);
		if (tmp_len != 4096)
		{
   
   
			
			ret=DesEnc(buf,tmp_len,buf_crypt,&tmp_crypt_len);						//处理小于4k的数据
			if (ret!=0)
			{
   
   
				printf("func DesEnc_raw err\n");
				goto END;
			}
			int count=fwrite(buf_crypt, 1, tmp_crypt_len, fd2);
			if (count!=tmp_crypt_len)
			{
   
   
				printf("func fwrite err\n");
				goto END;
			}

		}
		else
		{
   
   
			DesEnc_raw(buf, tmp_len, buf_crypt, &tmp_crypt_len);
			if (ret != 0)
			{
   
   
				printf("func DesEnc err\n");
				goto END;
			}
			int count = fwrite(buf_crypt, 1, tmp_crypt_len, fd2);								//以4k为一个块处理数据
			if (count != tmp_len)
			{
   
   
				printf("func fwrite err\n");
				goto END;
			}
		}
	}
	goto END;





END:
	if (fd1 != NULL)
	{
   
   
		fclose(fd1);
	}
	if (fd1 != NULL)
	{
   
   
		fclose(fd2);
	}
	return ret;
}

int file_decrypt(const char* file1, const char* file2)
{
   
   
	int ret = 0;
	unsigned char buf[4096] = {
   
    0 };//因为是以字节为单位操作内存,所以定义一个char类型的数组来存储
	unsigned char buf_crypt[4096] = {
   
    0 };//加密后的buf

	FILE* fd1 = NULL;//输入
	FILE* fd2 = NULL;//解密后

	int tmp_len = 0;
	int tmp_crypt_len = 0;

	fd1 = fopen(file1, "rb");
	if (fd1 == NULL)
	{
   
   
		ret = -1;
		printf("func fopen file1 err\n");
		goto END;
	}

	fd2 = fopen(file2, "wb");
	if (fd2 == NULL)
	{
   
   
		ret = -1;
		printf("func fopen  file2 err\n");
		goto END;
	}


	while (!feof(fd1))
	{
   
   
		tmp_len = fread(buf, 1, 4096, fd1);//一次读取4K数据,以单字节的形式读取,一次读取4096个,若不够4096则返回最后读取到的个数

		//printf("tmp_len=%d\n", tmp_len);
		//printf("buf=%s\n", buf);
		if (tmp_len != 4096)
		{
   
   

			ret = DesDec(buf, tmp_len, buf_crypt, &tmp_crypt_len);						//处理小于4k的数据
			if (ret != 0)
			{
   
   
				printf("func DesEnc_raw err\n");
				goto END;
			}
			int count = fwrite(buf_crypt, 1, tmp_crypt_len, fd2);
			if (count != tmp_crypt_len)
			{
   
   
				printf("func fwrite err\n");
				goto END;
			}

		}
		else
		{
   
   
			DesDec_raw(buf, tmp_len, buf_crypt, &tmp_crypt_len);
			if (ret != 0)
			{
   
   
				printf("func DesEnc err\n");
				goto END;
			}
			int count = fwrite(buf_crypt, 1, tmp_crypt_len, fd2);								//以4k为一个块处理数据
			if (count != tmp_len)
			{
   
   
				printf("func fwrite err\n");
				goto END;
			}
		}
	}
	goto END;





END:
	if (fd1 != NULL)
	{
   
   
		fclose(fd1);
	}
	if (fd1 != NULL)
	{
   
   
		fclose(fd2);
	}
	return ret;
}

int main()
{
   
   

	int ret;
	const char *file1 = "C:/Users/ky/Desktop/1.docx";
	const char* file2 = "C:/Users/ky/Desktop/5_enc.txt";

//	const char* file1 = "C:/Users/ky/Desktop/3_enc.txt";
//	const char* file2 = "C:/Users/ky/Desktop/4.txt";


//	const char* file1 = "C:/Users/ky/Desktop/5_enc.txt";
//	const char* file2 = "C:/Users/ky/Desktop/解密.docx";


//	ret = file_encrypt(file1, file2);//加密

	ret = file_decrypt(file1, file2);//解密

	if (ret != 0)
	{
   
   
		printf("func file_symsnc err");
	}


	return 0;

}


des.h

/*********************************************************
 *  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);


int DesEnc_raw(
	unsigned char* pInData,
	int            nInDataLen,
	unsigned char* pOutData,
	int* pOutDataLen);

//用户使用函数des解密
int DesDec(
	   unsigned char *pInData,
	   int            nInDataLen,
	   unsigned char *pOutData,
	   int           *pOutDataLen);

int DesDec_raw(
	unsigned char* pInData,
	int            nInDataLen,
	unsigned char* pOutData,
	int* pOutDataLen);


#ifdef __cplusplus
}
#endif

#endif

des.c 加解密的核心算法文件
#define USER_PASSWORD_KEY “abcd1234”
可以自定义八位数的密钥

/******************************************************
 *
 *  des.c
 *  common des......
 *
 ******************************************************/

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

/*********************************************************
  data type definition for Des;
**********************************************************/
#define EN0	0
#define DE1	1

#define DES_KEYBYTES	128
#define DES_KEYLONGS	32
#define DES_BLOCKLEN	8

typedef struct {
   
   
	unsigned char ek[DES_KEYBYTES];
	int	ekLen;
	unsigned char dk[DES_KEYBYTES];
	int	dkLen;
	unsigned char CbcCtx[DES_BLOCKLEN];
} DES_CTX;

typedef struct {
   
   
	unsigned char ek1[DES_KEYBYTES];
	int	ek1Len;
	unsigned char dk1[DES_KEYBYTES];
	int	dk1Len;
	unsigned char ek2[DES_KEYBYTES];
	int	ek2Len;
	unsigned char dk2[DES_KEYBYTES];
	int	dk2Len;
	unsigned char CbcCtx[DES_BLOCKLEN];
	//int	IsFirstBlock;
} DES3_CTX;


static unsigned char pc1[56] = {
   
   
	56, 48, 40, 32, 24, 16,  8,  0, 57, 49, 41, 33, 25, 17,
	 9,  1, 58, 50, 42, 34, 26, 18, 10,  2, 59, 51, 43, 35,
	62, 54, 46, 38, 30, 22, 14,  6, 61, 53, 45, 37, 29, 21,
	13,  5, 60, 52, 44, 36, 28, 20, 12,  4, 27, 19, 11,  3 };

static unsigned char pc2[48] = {
   
   
	13, 16, 10, 23,  0,  4,		 2, 27, 14,  5, 20,  9,
	22, 18, 11,  3, 25,  7, 	15,  6, 26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值