debian系统下的OpenSSL命令行工具功能强大,可用于密钥对生成、证书创建与管理、数据加密解密等多种任务。本文将引导您快速上手OpenSSL。
一、OpenSSL安装
首先,确认您的Debian系统已安装OpenSSL。若未安装,请执行以下命令:
sudo apt update sudo apt install openssl
二、密钥对生成
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文件。
三、自签名证书创建
openssl req -new -x509 -days 365 -key private.key -out certificate.crt
此命令创建一个有效期为365天的自签名证书,保存至certificate.crt文件。执行过程中,系统将提示您输入国家、组织名称等信息。
四、数据加密与解密
1. 文件加密:
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.bin
此命令使用AES-256-CBC算法加密plaintext.txt文件,加密结果保存至encrypted.bin文件。系统将提示您输入密码。
2. 文件解密:
openssl enc -d -aes-256-cbc -in encrypted.bin -out decrypted.txt
此命令使用相同密码解密encrypted.bin文件,解密结果保存至decrypted.txt文件。
五、证书信息查看与验证
1. 查看证书信息:
openssl x509 -in certificate.crt -text -noout
此命令显示证书详细信息,包括主题、颁发者、有效期等。
2. 验证证书签名:
openssl verify -CAfile ca.crt certificate.crt
此命令验证certificate.crt是否由ca.crt签发。
六、证书签名请求(CSR)与PKCS#12文件生成及操作
1. 生成CSR:
openssl req -new -key private.key -out certificate_signing_request.csr
此命令生成CSR文件,用于向证书颁发机构(CA)申请证书。
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系统下OpenSSL的常用功能。 更多高级功能,请参考OpenSSL官方文档。