OpenSSL 使用文件
OpenSSL 是一套功能強大的開源工具套件,可實現安全通訊。它支援加密、解密、SSL/TLS 協議等功能。ServBay 出廠時已經內建 OpenSSL,本文將詳細介紹 OpenSSL 的安裝、設定與使用方法。
安裝與設定
安裝
ServBay 出廠時已預裝 OpenSSL,無需額外安裝。
設定
OpenSSL 的設定檔通常位於 /Applications/ServBay/package/common/openssl/3.2
目錄下(Intel 晶片則在 /Applications/ServBay/package/common/openssl/1.1.1u
),預設設定檔為 openssl.cnf
。您可以根據需求修改設定檔,以調整 OpenSSL 的行為。
範例設定檔內容:
ini
[ req ]
default_bits = 2048
default_keyfile = privkey.pem
distinguished_name = req_distinguished_name
attributes = req_attributes
x509_extensions = v3_ca
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = US
stateOrProvinceName = State or Province Name (full name)
localityName = Locality Name (eg, city)
0.organizationName = Organization Name (eg, company)
organizationalUnitName = Organizational Unit Name (eg, section)
commonName = Common Name (eg, fully qualified host name)
commonName_max = 64
[ req_attributes ]
challengePassword = A challenge password
challengePassword_min = 4
challengePassword_max = 20
[ v3_ca ]
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid:always,issuer
basicConstraints = CA:true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
基本使用
OpenSSL 提供豐富的指令行工具,適用於各種加密與憑證管理任務。以下舉幾個基本使用示例:
產生私鑰與公鑰
產生 RSA 私鑰
bash
openssl genpkey -algorithm RSA -out private_key.pem
1
從私鑰產生公鑰
bash
openssl rsa -pubout -in private_key.pem -out public_key.pem
1
產生憑證簽署請求(CSR)
產生 CSR
bash
openssl req -new -key private_key.pem -out request.csr
1
自簽憑證
產生自簽憑證
bash
openssl req -x509 -days 365 -key private_key.pem -in request.csr -out certificate.crt
1
憑證管理
檢視憑證資訊
檢視 CSR 資訊
bash
openssl req -text -noout -verify -in request.csr
1
檢視憑證資訊
bash
openssl x509 -text -noout -in certificate.crt
1
憑證格式轉換
PEM 轉 DER
bash
openssl x509 -outform der -in certificate.pem -out certificate.der
1
DER 轉 PEM
bash
openssl x509 -inform der -in certificate.der -out certificate.pem
1
加密與解密
對稱式加密
使用 AES 加密檔案
bash
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.txt
1
使用 AES 解密檔案
bash
openssl enc -d -aes-256-cbc -in encrypted.txt -out decrypted.txt
1
非對稱式加密
使用公鑰加密檔案
bash
openssl rsautl -encrypt -inkey public_key.pem -pubin -in plaintext.txt -out encrypted.txt
1
使用私鑰解密檔案
bash
openssl rsautl -decrypt -inkey private_key.pem -in encrypted.txt -out decrypted.txt
1
常見問題
1. OpenSSL 指令無法執行
- 解決方案:請確認已正確設置 ServBay 的環境變數。若問題仍然存在,請嘗試開啟 ServBay 的「設定」-「指令行工具」,分別對
zsh
和bash
進行設定,然後重新開啟終端機。
2. 產生的憑證無效
- 解決方案:請檢查憑證請求檔(CSR)以及設定檔中的資訊是否正確。確保已填寫所有必填欄位。
3. 加密或解密失敗
- 解決方案:請確保所用密鑰與演算法相符無誤,並檢查輸入與輸出檔案路徑設定是否正確。
總結
OpenSSL 是一套強大且靈活的工具套件,適用於各式安全通訊與加密需求。透過本文件的介紹,您可以了解如何在 ServBay 中安裝、設定與運用 OpenSSL 進行憑證管理與資料加密。