使用openssl中aes算法加密解密文件

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includeopenssl/evp.h#includestdio.h#defineN1024#defineIN//AesËã·¨ecbģʽ¼ÓÃÜÎļþ/**********************************************************************º¯ÊýÃû³Æ£ºAesEncryptFileº¯Êý¹¦ÄÜ£º¼ÓÃÜÎļþ´¦Àí¹ý³Ì£º1.¸ù¾ÝÑ¡ÔñµÄÃÜÂëËã·¨ÒÔ¼°¿ÚÁÉú³ÉkeyºÍiv¡£2.°ÑÎļþͷдÈëÃÜÎÄÎļþ3.Ñ­»·¶ÁÈ¡Ô­ÎÄÎļþÊý¾Ý¼ÓÃܺ󱣴浽ÃÜÎÄÎļþ·¾¶ÖС£²ÎÊý˵Ã÷£ºszSrc:[IN]char*£¬´ý¼ÓÃܵÄÔ­ÎÄÎļþ·¾¶szTarget:[IN]char*£¬¼ÓÃܺóµÄÃÜÎÄÎļþ±£´æ·¾¶key:[IN]unsignedchar*,¿ÚÁîiType:[IN]int,¼ÓÃÜÀàÐÍ£¨128»ò256£©·µ»ØÖµ£º³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø0************************************************************************/intAesEncryptFile(INchar*szSrc,INchar*szTarget,INunsignedchar*key,INintiType){unsignedcharukey[EVP_MAX_KEY_LENGTH];unsignedchariv[EVP_MAX_IV_LENGTH];unsignedcharin[N];intinl;//ÊäÈëÊý¾Ý´óСunsignedcharout[N];intoutl;//Êä³öÊý¾Ý´óСintisSuccess;FILE*fpIn;FILE*fpOut;EVP_CIPHER_CTXctx;//evp¼ÓÃÜÉÏÏÂÎÄ»·¾³constEVP_CIPHER*cipher;fpIn=fopen(szSrc,rb);if(fpIn==NULL){printf(fopenszSrcfailed);return0;}fpOut=fopen(szTarget,w+);if(fpOut==NULL){printf(fopenszTargetfailed);fclose(fpIn);return0;}//Ñ¡ÔñËã·¨if(iType==128){cipher=EVP_aes_128_ecb();}elseif(iType==256){cipher=EVP_aes_256_ecb();}else{printf(iTypeshouldbe128or256.);fclose(fpIn);fclose(fpOut);return0;}//Éú³ÉukeyºÍivintlen=sizeof(key);EVP_BytesToKey(cipher,EVP_md5(),NULL,(constunsignedchar*)key,len-1,1,ukey,iv);//³õʼ»¯ctx£¬¼ÓÃÜËã·¨³õʼ»¯EVP_CIPHER_CTX_init(&ctx);isSuccess=EVP_EncryptInit_ex(&ctx,cipher,NULL,ukey,iv);if(!isSuccess){printf(EVP_EncryptInit_ex()failed);EVP_CIPHER_CTX_cleanup(&ctx);fclose(fpIn);fclose(fpOut);return0;}//¼ÓÃÜÎļþfor(;;){inl=fread(in,1,N,fpIn);if(inl=0)break;isSuccess=EVP_EncryptUpdate(&ctx,out,&outl,in,inl);if(!isSuccess){printf(EVP_EncryptInit_ex()failed);EVP_CIPHER_CTX_cleanup(&ctx);fclose(fpIn);fclose(fpOut);return0;}fwrite(out,1,outl,fpOut);}isSuccess=EVP_EncryptFinal_ex(&ctx,out,&outl);if(!isSuccess){printf(EVP_EncryptInit_ex()failed);EVP_CIPHER_CTX_cleanup(&ctx);fclose(fpIn);fclose(fpOut);return0;}fwrite(out,1,outl,fpOut);printf(¼ÓÃܳɹ¦\n);EVP_CIPHER_CTX_cleanup(&ctx);fclose(fpIn);fclose(fpOut);return1;}//AesËã·¨ecbģʽ½âÃÜÎļþ/**********************************************************************º¯ÊýÃû³Æ£ºAesDecryptFileº¯Êý¹¦ÄÜ£º½âÃÜÎļþ´¦Àí¹ý³Ì£º1.¸ù¾ÝÑ¡ÔñµÄÃÜÂëËã·¨ÒÔ¼°¿ÚÁÉú³ÉkeyºÍiv¡£2.°ÑÎļþͷдÈëÃÜÎÄÎļþ3.Ñ­»·¶ÁÈ¡Ô­ÎÄÎļþÊý¾Ý¼ÓÃܺ󱣴浽ÃÜÎÄÎļþ·¾¶ÖС£²ÎÊý˵Ã÷£ºszSrc:[IN]char*£¬´ý½âÃܵÄÃÜÎÄÎļþ·¾¶szTarget:[IN]char*£¬½âÃܺóµÄ½âÃÜÎļþ±£´æ·¾¶key:[IN]unsignedchar*,¿ÚÁîiType:[IN]int,¼ÓÃÜÀàÐÍ£¨128»ò256£©·µ»ØÖµ£º³É¹¦·µ»Ø1£¬·ñÔò·µ»Ø0************************************************************************/intAesDecryptFile(INchar*szSrc,INchar*szTarget,INunsignedchar*key,INintiType){unsignedcharukey[EVP_MAX_KEY_LENGTH];unsignedchariv[EVP_MAX_IV_LENGTH];unsignedcharin[N];intinl;//ÊäÈëÊý¾Ý´óСunsignedcharout[N];intoutl;//Êä³öÊý¾Ý´óСintisSuccess;FILE*fpIn;FILE*fpOut;EVP_CIPHER_CTXctx;//evp¼ÓÃÜÉÏÏÂÎÄ»·¾³constEVP_CIPHER*cipher;fpIn=fopen(szSrc,rb);if(fpIn==NULL){printf(fopenszSrcfailed);return0;}fpOut=fopen(szTarget,w+);if(fpOut==NULL){printf(fopenszTargetfailed);fclose(fpIn);return0;}//Ñ¡ÔñËã·¨if(iType==128){cipher=EVP_aes_128_ecb();}elseif(iType==256){cipher=EVP_aes_256_ecb();}else{printf(iTypeshouldbe128or256.);fclose(fpIn);fclose(fpOut);return0;}//Éú³ÉukeyºÍivintlen=sizeof(key);EVP_BytesToKey(cipher,EVP_md5(),NULL,(constunsignedchar*)key,len-1,1,ukey,iv);//³õʼ»¯ctx£¬¼ÓÃÜËã·¨³õʼ»¯EVP_CIPHER_CTX_init(&ctx);isSuccess=EVP_DecryptInit_ex(&ctx,cipher,NULL,ukey,iv);if(!isSuccess){printf(EVP_DecryptInit_ex()failed);EVP_CIPHER_CTX_cleanup(&ctx);fclose(fpIn);fclose(fpOut);return0;}//¼ÓÃÜÎļþfor(;;){inl=fread(in,1,N,fpIn);if(inl=0)break;isSuccess=EVP_DecryptUpdate(&ctx,out,&outl,in,inl);if(!isSuccess){printf(EVP_EncryptInit_ex()failed);EVP_CIPHER_CTX_cleanup(&ctx);fclose(fpIn);fclose(fpOut);return0;}fwrite(out,1,outl,fpOut);}isSuccess=EVP_DecryptFinal_ex(&ctx,out,&outl);if(!isSuccess){printf(EVP_DecryptInit_ex()failed);EVP_CIPHER_CTX_cleanup(&ctx);fclose(fpIn);fclose(fpOut);return0;}fwrite(out,1,outl,fpOut);printf(½âÃܳɹ¦\n);EVP_CIPHER_CTX_cleanup(&ctx);fclose(fpIn);fclose(fpOut);return1;}intmain(){char*Src=C:\\DocumentsandSettings\\JOKE\\×ÀÃæ\\1.txt;char*TargetEnc=C:\\DocumentsandSettings\\JOKE\\×ÀÃæ\\1.txt.enc;char*TargetDec=C:\\DocumentsandSettings\\JOKE\\×ÀÃæ\\1.txt.dec;unsignedcharkey[32]=abcdefg;intrv=AesEncryptFile(Src,TargetEnc,key,128);if(rv!=1){printf(AesEncryptFile()failed);return1;}rv=AesDecryptFile(TargetEnc,TargetDec,key,128);if(rv!=1){printf(AesDecryptFile()failed);return1;}return0;}

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功