当前位置:文档之家› 文件加密解决方案底层库详细说明

文件加密解决方案底层库详细说明

文件加密解决方案底层库详细说明目录一、概要设计 (1)二、详细设计 (2)(一)文件读入模块 (2)(二)加密模块 (3)(三)解密模块 (3)(四)保存目标文件模块 (3)(五)主处理函数 (4)三、文件格式 (4)(一)文件格式说明 (4)(二)文件格式详细定义 (5)四、功能实现 (6)(一)命令行 (6)1、Linux命令行 (7)2、Windows命令行 (8)(二)库 (9)(三)接口 (9)1、通用C接口 (9)2、Android JNI接口 (10)3、Lua接口 (11)4、iOS接口 (11)一、概要设计加密方与解密方共享同一个密钥Key。

因此加密方与解密方的角色是对等的。

基于对称加密体制,共支持两种算法可以选择,AES256与SM4。

子功能设计二、详细设计(一)文件读入模块读入明文模块与读入密文模块,合并成一个模块,即“读入源文件”模块。

操作都是一样的,都是将用户指定的磁盘文件——不管是密文还是明文的,读入到内存中去。

读入源文件的函数定义如下:int openSrcFile(char **buffer){FILE *myfile_src; /*源文件指针*/char filename[20]; /*文件名数组*/long file_size; /*记录文件的长度*/printf("Please input the path and filename of the file you want to process\n");scanf("%s",filename);if(!(myfile_src = fopen(filename,"rb"))){printf("ERROR!");}fseek(myfile_src,0,SEEK_END);file_size = ftell(myfile_src);fseek(myfile_src,0,SEEK_SET);*buffer = (char *)malloc(file_size);fread(*buffer,1,file_size,myfile_src); /*读入文件*/fclose(myfile_src);return file_size;(二)加密模块文件加密函数如下void encryption(char buffer[],int file_size,int key){int i;for( i = 0; i < file_size; i++)buffer[i] = buffer[i] + key; /* M=2*(P+key) */}(三)解密模块解密函数如下void decryption(char buffer[],int file_size,int key){int i;for( i = 0; i < file_size; i++)buffer[i] = buffer[i] - key;}(四)保存目标文件模块将保存密文模块与保密明文模块,合并成一个模块,即保存目标文件模块。

因为它们的操作是一样的。

void saveDstFile(char *buffer,long file_size){FILE *myfile_dst; /*源文件指针*/char filename[20]; /*文件名数组*/printf("Please input the path and filename of the file you have processed\n");scanf("%s",filename);if(!(myfile_dst = fopen(filename,"wb"))){printf("ERROR!");}fwrite(buffer,1,file_size,myfile_dst);printf("OK");fclose(myfile_dst);}(五)主处理函数主函数最后就是主函数。

void Process(int a){FILE *myfile_dst;char * buffer;int key;long file_size; /*记录文件的长度*/file_size = openSrcFile(&buffer); /*读入源文件*/printf("Please input the key (a integer) for encryption or decryption\n");scanf("%d",&key); /*用户输入密钥*/if(a == 0){/*加密状态*/encryption(buffer,file_size,key);}else{/*解密状态*/decryption(buffer,file_size,key);}saveDstFile(buffer,file_size);}三、文件格式(一)文件格式说明字段1:前面添加字节,字节数8(文件类型)字段2:前面添加字节,字段2(加密密码)元数据:前面添加字节,字段2(创建作者)前面添加字节,字段2(创建时间)前面添加字节,字段2(签名字段可选)32位,MD5正文随机加密部分(二)文件格式详细定义#ifndef G10_GOBAL_H#define G10_GOBAL_H#ifdef __cplusplusextern "C"{#endif// 512 bytes//typedef struct __fat_file_header{unsigned char sig[8];struct{unsigned char reserved1[8];unsigned char version;unsigned char algo;unsigned int file_type; // decision is or isn't encrypted data or plaintext!}metadata;// other for fat}FAT_FILE_HEADER;// private file infomationtypedef struct __private_file_header{struct{unsigned char reserved1[8];unsigned char reserved2[8];unsigned char reserved3[8];unsigned char reserved4[8];}resv;struct{unsigned char signature[32]; // hash signatureunsigned char author[32]; // user nameunsigned long long time; // date and time}sig;struct{unsigned char password[32];unsigned char question1[32];unsigned char answer1[32];unsigned char question2[32];unsigned char answer2[32];unsigned char question3[32];unsigned char answer3[32];}pwd;}PRIVATE_FILE_HEADER;// file body datatypedef struct __file_body_data{long long len;int padding;int reserved1;int reserved2;char body[0];}FILE_BODY_DATA;typedef struct __file_whole_data{FAT_FILE_HEADER fat_header;PRIVATE_FILE_HEADER private_header;FILE_BODY_DATA body_data;}FILE_WHOLE_DATA;#ifdef __cplusplus}#endif#endif /*G10_GOBAL_H*/四、功能实现(一)命令行加密格式文件,支持在Linux平台(Ubuntu,CentOS,MacOS)上加密与解密。

1、Linux命令行使用方式:ntep --helpntep 1.0.0.1Usage: ntep –-cipher[=aes256|sm4][--signature]input.file out.fileOptions:-s --signature signature function-c, --cipher[={aes256,sm4}] cipher type-p, --password[=…] password-q, --question1 password help question 暂时先不做-h, --help display this help and exit-v, --version output version information and exitntep --versionntep 1.0.0.1Copyright (C) 2015, Inc.This is software: you are don’t free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.sHome: ~/.ntepSupported algorithms:Pubkey: RSA, RSA-E, RSA-S, ELG-E, DSACipher: AES256,SM4Hash: MD5, SHA1, RIPEMD160, SHA256, SHA384, SHA512, SHA224 Compression: Uncompressed, ZIP, ZLIB, BZIP2ntep --helpntep (testDP) 1.0.0.1Usage: ntdp –-password[=…] [--verify] input.file out.file--verifyverify signature--password[=…] password--help this message--version version message应用实例:加密,加密并签名,加密但不TXT FILE(1)size is modulo padding./ntep -e -c aes256 -s -p abc -i ./md.c -o ./md.test(2)size is modulo 0./ntep -e -c aes256 -s -p abc -i ./md.c -o ./md.testRAR FILE,( 1/3 be encrypted.)(1)size is modulo padding./ntep -e -c aes256 -s -p abc -i ./formatfactory3.0.1.zip -o ./formatfactory3.0.1.zip.test (2)size is modulo 0./ntep -e -c aes256 -s -p abc -i ./formatfactory3.0.1.zip -o ./md.test使用aes256加密文件,带签名,密码为abc解密(如果有签名,会验证签名)解密,只验证签名,然后会删掉明文。

相关主题