利用openssl中AES加密文件
2013-08-21 09:27
246 查看
代码如下,编译采用: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); }
相关文章推荐
- Linux下利用openssl对文件进行加密和解密
- 使用openssl中的加密函数AES、RC4、RSA对文件加密的一个例子
- OpenSSL AES, RSA 算法加密本地文件
- 利用OpenSSL库对Socket传输进行安全加密(RSA+AES)
- 利用openssl的AES和RSA完成对数据的加密解密
- 利用OpenSSL库对Socket传输进行安全加密(RSA+AES)
- 使用openssl中的加密函数AES、RC4、RSA对文件加密的一个例子
- 利用OpenSSL库对Socket传输进行安全加密(RSA+AES)
- 利用openssl的AES加密解密数据明文(ECB模式)
- 使用openssl中的加密函数AES、RC4、RSA对文件加密的一个例子
- 利用OpenSSL库对Socket传输进行安全加密(RSA+AES)
- linux下面C 利用openssl的AES库加密,解密
- Linux下利用openssl对文件进行加密和解密
- 使用tar与OpenSSL加密解密打包文件
- openssl之aes加密
- AES加密之openssl使用(2)
- 利用OpenSSL生成证书文件的总结
- AES加密和解密——使用openssl编程
- php利用openssl实现RSA非对称加密签名