OpenSSl 加密解密 示例

2021-08-13 10:18:14 字數 1817 閱讀 2710

openssl的加密主要有三個重要的函式。看懂下面的**就基本上知道該如何使用openssl來加密了。

不過注意,要先將libssl.so.1.0和libcrypto.so.1.0檔案複製到執行的資料夾下,linux下,該命令一般為/usr/lib,否則,程式執行時會顯示找不到檔案。另外,注意先在目錄下建立mikejin.txt、mikejmw.txt、mikejout.txt三個檔案

#include 

#include

#include

#include

#include

//加密函式

encrypt_file()

//開啟儲存密文的檔案

fpout = fopen("mikejmw.txt","wb");

if(fpout==null)

//設定key和iv

for(i=0;i<24;i++)

for(i=0;i<8;i++)

//初始化ctx

evp_cipher_ctx_init(&ctx);

//設定密碼演算法、key和iv

rv = evp_encryptinit_ex(&ctx,evp_des_ede3_cbc(),null,key,iv);

if(rv != 1)

//迴圈讀取原文,加密後後儲存到密文檔案。

for(;;)

fwrite(out,1,outl,fpout);//儲存密文到檔案

}//加密結束

rv = evp_encryptfinal_ex(&ctx,out,&outl);

if(rv != 1)

fwrite(out,1,outl,fpout);//保密密文到檔案

fclose(fpin);

fclose(fpout);

evp_cipher_ctx_cleanup(&ctx);//清除evp加密上下文環境

printf("加密已完成\n");

return

true;

}//解密函式

decrypt_file()

//開啟儲存明文的檔案

fpout = fopen("mikejout.txt","wb");

if(fpout==null)

//設定key和iv

for(i=0;i<24;i++)

for(i=0;i<8;i++)

//初始化ctx

evp_cipher_ctx_init(&ctx);

//設定解密的演算法、key和iv

rv = evp_decryptinit_ex(&ctx,evp_des_ede3_cbc(),null,key,iv);

if(rv != 1)

//迴圈讀取原文,解密後後儲存到明文檔案。

for(;;)

fwrite(out,1,outl,fpout);//儲存明文到檔案

}//解密結束

rv = evp_decryptfinal_ex(&ctx,out,&outl);

if(rv != 1)

fwrite(out,1,outl,fpout);//儲存明文到檔案

fclose(fpin);

fclose(fpout);

evp_cipher_ctx_cleanup(&ctx);//清除evp加密上下文環境

printf("解密已完成\n");

return

true;

}int main()

system("cls");

switch(i)

return

0;}

openssl 實現 aes 檔案加密解密

linux 環境 基礎 安裝 openssl 加密用 安裝 python 環境 換金鑰用 bin sh py file zone aes tool py key iv in file out file 獲取金鑰k,向量v,輸入檔案i,輸出檔案o while getopts k v i o opt d...

使用tar與OpenSSL加密解密

1 使用tar對檔案壓縮加密 tar zcvf test fold openssl des3 salt k mypassword dd of test.des3 完成將得到乙個pma.des3的打包檔案,用你設定的密碼替換password。2 使用tar對加密檔案解壓 dd if test.des3...

用openssl對檔案加密及解密

openssl是乙個開源的用以實現ssl協議的產品,它主要包括了三個部分 密碼演算法庫 應用程式 ssl協議庫。openssl實現了ssl協議所需要的大多數演算法。下面我將單介紹使用openssl進行檔案的對稱加密操作。一 openssl支援的加密演算法有 aes 128 cbc aes 128 c...