利用openssl中AES加密文件

代码如下,编译采用:gcc -o encrypt_function encrypt_function.c -lcrypto -lm -g。运行的是:./encrypt_function filename. 这篇文章之后,我还会对其他openssl的加密AES API进行测试,希望给大家提供帮助.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <openssl/aes.h>
//判断文件大小
long filesize(FILE *stream)
{
	long curpos, length;
	curpos = ftell(stream);
	fseek(stream, 0L, SEEK_END);
	length = ftell(stream);
	fseek(stream, curpos, SEEK_SET);
	return length;
}

int decrypt_function(unsigned char *encrypt_string, unsigned char* decrypt_string,int len,char* key_string){
	 AES_KEY aes;
    unsigned char iv[AES_BLOCK_SIZE];        // init vector
	int i;
    	
    for (i=0; i<AES_BLOCK_SIZE; ++i) {
       iv[i] = 0;
    }
	if (AES_set_decrypt_key(key_string, 128, &aes) < 0) {
        fprintf(stderr, "Unable to set decryption key in AES\n");
        exit(-1);
    }
	
    // decrypt
    AES_cbc_encrypt(encrypt_string, decrypt_string, len, &aes, iv, 
            AES_DECRYPT);
}

void decrypt(char *fileIn, char *fileOut, unsigned char *key) {
	int i,count = 0;
	char * inBuffer=malloc(AES_BLOCK_SIZE);
	char * outBuffer=malloc(AES_BLOCK_SIZE);
	memset(inBuffer,0,16);
	memset(outBuffer,0,16);
	FILE *inFile = fopen(fileIn, "rb");
	FILE *outFile = fopen(fileOut, "wb");
	
	int buffer_size = 50000;
	while(buffer_size%16)
		buffer_size++;
		
	char * data;
	char * decrypted_data;
	
	int Lengthoffile= filesize(inFile);
	int newsize = Lengthoffile;
	while(newsize%16)
		newsize++;
	int readins = ceil((double)Lengthoffile/buffer_size);
	if(readins ==1){
		data = (char *)malloc(sizeof(char)*newsize);
		decrypted_data = (char *)malloc(sizeof(char)*newsize);
		memset(data,0,newsize);
		memset(decrypted_data,0,newsize);
	}else{
		data = (char *)malloc(sizeof(char)*buffer_size);
		decrypted_data = (char *)malloc(sizeof(char)*buffer_size);
		memset(data,0,buffer_size);
		memset(decrypted_data,0,buffer_size);
	}
	
	int n = 0;
	int total = 0,extra = 0,lea = 0,total_write = 0;
	while(n < readins){
		if(total < Lengthoffile && total + buffer_size < Lengthoffile){
			count= fread(data, 1, buffer_size, inFile);
			total += count;
		}else if(total < Lengthoffile && total + buffer_size > Lengthoffile){
			extra = fread(data, sizeof(char), buffer_size, inFile);
			if(extra%AES_BLOCK_SIZE==0){
				lea = extra;
			}else{
				lea=(extra/16 + 1)*16;
				for(i = extra;i< lea;i++){
					data[i] = '0';
				}
			}
			count = lea;
			total += lea;
			
		}else if(total == Lengthoffile){
			lea=(Lengthoffile/AES_BLOCK_SIZE + 1)*AES_BLOCK_SIZE;
			for(i = Lengthoffile;i< lea;i++){
				data[i] = '0';
			}
			count = lea;
			total += lea;
		}
		decrypt_function(data, decrypted_data, count,key);
		fwrite(decrypted_data, 1, count, outFile);
		n++;
	}

	fclose(inFile);
	fclose(outFile);
}

int encrypt_function(char * input, unsigned char* encrypt_string,int len,char *key_string) {
    AES_KEY aes;
    unsigned char iv[AES_BLOCK_SIZE];        // init vector
	unsigned int i;
    
    // Set encryption key
    for (i=0; i<AES_BLOCK_SIZE; ++i) {
        iv[i] = 0;
    }
    if (AES_set_encrypt_key(key_string, 128, &aes) < 0) {
        fprintf(stderr, "Unable to set encryption key in AES\n");
        exit(-1);
    }
	char * input_string = (char *)malloc(sizeof(char)*len);
	memset(input_string,0,len);
	strncpy(input_string,input,len);
    if (encrypt_string == NULL||input_string==NULL) {
        fprintf(stderr, "Unable to allocate memory for encrypt_string\n");
        exit(-1);
    }
	
	if(len%AES_BLOCK_SIZE){
		printf("len is error.\n");
		exit(0);
	}
    // encrypt (iv will change)
    AES_cbc_encrypt(input_string, encrypt_string, len, &aes, iv, AES_ENCRYPT);
	
    return 0;
}

void encrypt(char *fileIn, char *fileOut, unsigned char *key) {
	int i,count = 0;
	char * inBuffer=malloc(AES_BLOCK_SIZE);
	char * outBuffer=malloc(AES_BLOCK_SIZE);
	memset(inBuffer,0,16);
	memset(outBuffer,0,16);
	FILE *inFile = fopen(fileIn, "rb");
	FILE *outFile = fopen(fileOut, "wb");
	
	int buffer_size = 50000;
	while(buffer_size%16)
		buffer_size++;
		
	char * data;
	char * encrypted_data;
	
	int Lengthoffile= filesize(inFile);
	int newsize = Lengthoffile;
	while(newsize%16)
		newsize++;
	int readins = ceil((double)Lengthoffile/buffer_size);
	if(readins ==1){
		data = (char *)malloc(sizeof(char)*newsize);
		encrypted_data = (char *)malloc(sizeof(char)*newsize);
		memset(data,0,newsize);
		memset(encrypted_data,0,newsize);
	}else{
		data = (char *)malloc(sizeof(char)*buffer_size);
		encrypted_data = (char *)malloc(sizeof(char)*buffer_size);
		memset(data,0,buffer_size);
		memset(encrypted_data,0,buffer_size);
	}
	
	int n = 0;
	int total = 0,extra = 0,lea = 0,total_write = 0;
	while(n < readins){
		if(total < Lengthoffile && total + buffer_size < Lengthoffile){
		count= fread(data, 1, buffer_size, inFile);
		total +=count;
		}else if(total < Lengthoffile && total + buffer_size > Lengthoffile){
			extra = fread(data, sizeof(char), buffer_size, inFile);
			if(extra%AES_BLOCK_SIZE==0){
				lea = extra;
			}else{
				lea=(extra/AES_BLOCK_SIZE + 1)*AES_BLOCK_SIZE;
				for(i = extra;i< lea;i++){
					data[i] = '0';
				}
			}
			count = lea;
			total +=lea;
		}else if(total == Lengthoffile){
			lea=(Lengthoffile/AES_BLOCK_SIZE + 1)*AES_BLOCK_SIZE;
			for(i = Lengthoffile;i< lea;i++){
				data[i] = '0';
			}
			count= lea;
			total += lea;
		}
		encrypt_function(data, encrypted_data, count,key);
		total_write+=fwrite(encrypted_data, 1, count, outFile);
		n++;
	}
	printf("count = %d\n",total_write);
  fclose(inFile);
  fclose(outFile);
}

void main(int argc,char ** argv){
	
	char output_1[50]={0};
	char output_2[50]={0};
	char * key = "123456";
	sprintf(output_1,"%s.encrypt",argv[1]);
	sprintf(output_2,"%s.temp",argv[1]);
	encrypt(argv[1],output_1,key);
	//decrypt(output_1,output_2,key);
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值