Openssl是Linux系統下功能強大的命令行加密工具,支持多種加密操作。本文將介紹一些OpenSSL命令行基本用法:
-
生成RSA密鑰對:
openssl genrsa -out rsa_key.pem 2048
此命令生成一個2048位RSA私鑰,保存到rsa_key.pem文件。
-
創建自簽名證書:
openssl req -new -x509 -key rsa_key.pem -out certificate.crt -days 365
利用步驟1生成的私鑰,創建有效期為365天的自簽名證書,保存到certificate.crt。
-
查看證書信息:
openssl x509 -in certificate.crt -text -noout
顯示certificate.crt證書的詳細內容。
-
文件加密:
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.bin
使用AES-256-CBC算法加密plaintext.txt,加密結果保存到encrypted.bin,系統會提示輸入密碼。
-
文件解密:
openssl enc -d -aes-256-cbc -in encrypted.bin -out decrypted.txt
使用相同的密碼解密encrypted.bin,解密結果保存到decrypted.txt。
-
Diffie-Hellman密鑰交換:
openssl dhparam -out dhparams.pem 2048 openssl genpkey -paramfile dhparams.pem -out dh_key.pem openssl pkey -in dh_key.pem -pubout -out dh_pub.pem
生成Diffie-Hellman參數和密鑰交換所需的私鑰和公鑰。
-
使用證書進行SSL/TLS連接:
openssl s_client -connect example.com:443 -cert client_cert.pem -key client_key.pem
使用指定的客戶端證書和私鑰連接到example.com:443端口,顯示SSL/TLS握手過程和服務器證書信息。
-
創建PKCS#12文件:
openssl pkcs12 -export -out certificate.p12 -inkey rsa_key.pem -in certificate.crt
將RSA私鑰和證書打包成PKCS#12格式文件certificate.p12。
-
從PKCS#12文件提取證書和私鑰:
openssl pkcs12 -in certificate.p12 -out certificate.pem -clcerts -nokeys openssl pkcs12 -in certificate.p12 -out private_key.pem -nocerts -nodes
分別提取證書和私鑰,保存到certificate.pem和private_key.pem。
安全提示: 使用OpenSSL時,務必妥善保管密鑰和證書文件,避免泄露。 命令中的密碼和文件路徑需根據實際情況修改。