Hello! 欢迎来到小浪云!


Linux下OpenSSL命令行工具怎么用


Linux下OpenSSL命令行工具怎么用

OpensslLinux系统下功能强大的命令行加密工具,支持多种加密操作。本文将介绍一些OpenSSL命令行基本用法:

  1. 生成RSA密钥对:

    openssl genrsa -out rsa_key.pem 2048

    此命令生成一个2048位RSA私钥,保存到rsa_key.pem文件。

  2. 创建自签名证书:

    openssl req -new -x509 -key rsa_key.pem -out certificate.crt -days 365

    利用步骤1生成的私钥,创建有效期为365天的自签名证书,保存到certificate.crt。

  3. 查看证书信息:

    openssl x509 -in certificate.crt -text -noout

    显示certificate.crt证书的详细内容。

  4. 文件加密:

    openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.bin

    使用AES-256-CBC算法加密plaintext.txt,加密结果保存到encrypted.bin,系统会提示输入密码。

  5. 文件解密:

    openssl enc -d -aes-256-cbc -in encrypted.bin -out decrypted.txt

    使用相同的密码解密encrypted.bin,解密结果保存到decrypted.txt。

  6. 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参数和密钥交换所需的私钥和公钥。

  7. 使用证书进行SSL/TLS连接:

    openssl s_client -connect example.com:443 -cert client_cert.pem -key client_key.pem

    使用指定的客户端证书和私钥连接到example.com:443端口,显示SSL/TLS握手过程和服务器证书信息。

  8. 创建PKCS#12文件:

    openssl pkcs12 -export -out certificate.p12 -inkey rsa_key.pem -in certificate.crt

    将RSA私钥和证书打包成PKCS#12格式文件certificate.p12。

  9. 从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时,务必妥善保管密钥和证书文件,避免泄露。 命令中的密码和文件路径需根据实际情况修改。

相关阅读