使用openssl加密檔案

2021-09-03 07:49:44 字數 2956 閱讀 7716

使用openssl加密檔案

openssl是乙個開源的用以實現ssl協議的產品,它主要包括了三個部分:密碼演算法庫、應用程式、ssl協議庫。openssl實現了ssl協議所需要的大多數演算法。

下面我將單介紹使用openssl進行檔案的對稱加密操作。

一、openssl支援的加密演算法有:

-aes-128-cbc               -aes-128-cfb               -aes-128-cfb1

-aes-128-cfb8              -aes-128-ecb               -aes-128-ofb

-aes-192-cbc               -aes-192-cfb               -aes-192-cfb1

-aes-192-cfb8              -aes-192-ecb               -aes-192-ofb

-aes-256-cbc               -aes-256-cfb               -aes-256-cfb1

-aes-256-cfb8              -aes-256-ecb               -aes-256-ofb

-aes128                    -aes192                    -aes256

-bf                        -bf-cbc                    -bf-cfb

-bf-ecb                    -bf-ofb                    -blowfish

-cast                      -cast-cbc                  -cast5-cbc

-cast5-cfb                 -cast5-ecb                 -cast5-ofb

-des                       -des-cbc                   -des-cfb

-des-cfb1                  -des-cfb8                  -des-ecb

-des-ede                   -des-ede-cbc               -des-ede-cfb

-des-ede-ofb               -des-ede3                  -des-ede3-cbc

-des-ede3-cfb              -des-ede3-ofb              -des-ofb

-des3                      -desx                      -desx-cbc

-rc2                       -rc2-40-cbc                -rc2-64-cbc

-rc2-cbc                   -rc2-cfb                   -rc2-ecb

-rc2-ofb                   -rc4                       -rc4-40

二、openssl加密指令語法:

synopsis

openssl enc -ciphername [-in filename] [-out filename] [-pass arg] [-e]

[-d] [-a] [-a] [-k password] [-kfile filename] [-k key] [-iv iv] [-p]

[-p] [-bufsize number] [-nopad] [-debug]

說明:-chipername選項:加密演算法,openssl支援的演算法在上面已經列出了,你只需選擇其中一種演算法即可實現檔案加密功能。

-in選項:輸入檔案,對於加密來說,輸入的應該是明文檔案;對於解密來說,輸入的應該是加密的檔案。該選項後面直接跟檔名。

-out選項:輸出檔案,對於加密來說,輸出的應該是加密後的檔名;對於解密來說,輸出的應該是明文檔名。

-pass選項:選擇輸入口令的方式,輸入源可以是標準輸入裝置,命令列輸入,檔案、變數等。

-e選項:實現加密功能(不使用-d選項的話預設是加密選項)。

-d選項:實現解密功能。

-a和-a選項:對檔案進行base64編譯碼操作。

-k選項:手動輸入加密金鑰(不使用該選項,openssl會使用口令自動提取加密金鑰)。

-iv選項:輸入初始變數(不使用該選項,openssl會使用口令自動提取初始變數)。

-salt選項:是否使用鹽值,預設是使用的。

-p選項:列印出加密演算法使用的加密金鑰。

三、案例:

1. 使用aes-128-cbc演算法加密檔案:

openssl enc -aes-128-cbc -in install.log -out enc.log

(注:這裡install.log是你想要加密的檔案,enc.log是加密後的檔案,回車後系統會提示你輸入口令)

2. 解密剛剛加密的檔案:

openssl enc -d -aes-128-cbc -in enc.log -out install.log

(注:enc.log是剛剛加密的檔案,install.log是解密後的檔案,-d選項實現解密功能)

3.加密檔案後使用base64格式進行編碼:

openssl enc -aes-128-cbc -in install.log -out enc.log -a

4.使用多種口令輸入方式加密:

openssl enc -des-ede3-cbc -in install.log -out enc.log -pass pass:111111

(這種方式的好處是你可以把它寫入到指令碼中,自動完成加密功能,不使用pass選項預設系統會提示輸入口令,並且確認,是需要人工操作的)

linux下使用openssl加密檔案

1.加密乙個檔案 root fxvsystem root openssl enc des e a in install.log out install.log.des enter des cbc encryption password verifying enter des cbc encrypti...

用openssl來加密檔案

使用 openssl 的常規方式是 openssl command command options arguments 在 linux 中加密檔案要加密當前工作目錄的內容 根據檔案的大小,這可能需要一點時間 tar czf openssl enc e aes256 outsecured.tar.gz...

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...