您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 电子商务 > 使用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;}
本文标题:使用openssl中aes算法加密解密文件
链接地址:https://www.777doc.com/doc-4787505 .html