OpenSSL是一個強大的加密工具,可以在Ubuntu上使用命令行界面進行操作。以下是一些基本的OpenSSL命令行用法:
生成RSA密鑰對:
openssl genpkey -algorithm RSA -out rsa_key.pem -pkeyopt rsa_keygen_bits:2048
這將生成一個2048位的RSA私鑰,并保存在rsa_key.pem
文件中。
從私鑰生成公鑰:
openssl rsa -pubout -in rsa_key.pem -out rsa_key.pub
這將從私鑰rsa_key.pem
生成對應的公鑰,并保存在rsa_key.pub
文件中。
加密文件:
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.bin -pass pass:yourpassword
使用AES-256-CBC算法加密plaintext.txt
文件,并將加密后的數據保存到encrypted.bin
文件中。加密時需要提供密碼yourpassword
。
解密文件:
openssl enc -d -aes-256-cbc -in encrypted.bin -out decrypted.txt -pass pass:yourpassword
使用相同的密碼yourpassword
解密encrypted.bin
文件,并將解密后的數據保存到decrypted.txt
文件中。
生成自簽名證書:
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out certificate.crt -days 365 -nodes
這將生成一個自簽名的X.509證書,有效期為365天,并將私鑰保存在key.pem
文件中,證書保存在certificate.crt
文件中。
查看證書信息:
openssl x509 -in certificate.crt -text -noout
這將顯示certificate.crt
證書的詳細信息。
生成CSR(證書簽名請求):
openssl req -new -key rsa_key.pem -out csr.pem
這將生成一個CSR文件csr.pem
,用于向證書頒發機構(CA)申請證書。
驗證證書:
openssl verify -CAfile ca_certificate.crt certificate.crt
這將驗證certificate.crt
證書是否由ca_certificate.crt
CA頒發。
這些只是OpenSSL命令行工具的一些基本用法。OpenSSL功能非常強大,支持多種加密算法和協議,可以用于各種安全相關的任務。在使用時,請確保理解每個命令的參數和選項,以避免安全風險。