debian系統(tǒng)下的OpenSSL命令行工具功能強(qiáng)大,可用于密鑰對(duì)生成、證書創(chuàng)建與管理、數(shù)據(jù)加密解密等多種任務(wù)。本文將引導(dǎo)您快速上手OpenSSL。
一、OpenSSL安裝
首先,確認(rèn)您的Debian系統(tǒng)已安裝OpenSSL。若未安裝,請(qǐng)執(zhí)行以下命令:
sudo apt update sudo apt install openssl
二、密鑰對(duì)生成
1. 生成RSA私鑰:
openssl genrsa -out private.key 2048
此命令生成2048位RSA私鑰,并保存至private.key文件。
2. 生成RSA公鑰:
openssl rsa -in private.key -pubout -out public.key
此命令從private.key提取公鑰,保存至public.key文件。
三、自簽名證書創(chuàng)建
openssl req -new -x509 -days 365 -key private.key -out certificate.crt
此命令創(chuàng)建一個(gè)有效期為365天的自簽名證書,保存至certificate.crt文件。執(zhí)行過(guò)程中,系統(tǒng)將提示您輸入國(guó)家、組織名稱等信息。
四、數(shù)據(jù)加密與解密
1. 文件加密:
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.bin
此命令使用AES-256-CBC算法加密plaintext.txt文件,加密結(jié)果保存至encrypted.bin文件。系統(tǒng)將提示您輸入密碼。
2. 文件解密:
openssl enc -d -aes-256-cbc -in encrypted.bin -out decrypted.txt
此命令使用相同密碼解密encrypted.bin文件,解密結(jié)果保存至decrypted.txt文件。
五、證書信息查看與驗(yàn)證
1. 查看證書信息:
openssl x509 -in certificate.crt -text -noout
此命令顯示證書詳細(xì)信息,包括主題、頒發(fā)者、有效期等。
2. 驗(yàn)證證書簽名:
openssl verify -CAfile ca.crt certificate.crt
此命令驗(yàn)證certificate.crt是否由ca.crt簽發(fā)。
六、證書簽名請(qǐng)求(CSR)與PKCS#12文件生成及操作
1. 生成CSR:
openssl req -new -key private.key -out certificate_signing_request.csr
此命令生成CSR文件,用于向證書頒發(fā)機(jī)構(gòu)(CA)申請(qǐng)證書。
2. 生成PKCS#12文件:
openssl pkcs12 -export -in certificate.crt -inkey private.key -out keystore.p12 -name mycert
此命令將證書和私鑰打包成PKCS#12文件,別名為mycert。
3. 從PKCS#12文件提取證書和私鑰:
-
提取證書:
openssl pkcs12 -in keystore.p12 -clcerts -nokeys -out certificate.crt
-
提取私鑰:
openssl pkcs12 -in keystore.p12 -nocerts -out private.key -nodes
以上命令涵蓋了Debian系統(tǒng)下OpenSSL的常用功能。 更多高級(jí)功能,請(qǐng)參考OpenSSL官方文檔。