linux

OpenSSL命令行工具Linux使用技巧

小樊
42
2025-05-12 01:07:01
栏目: 云计算

OpenSSL是一个功能强大的命令行工具,用于加密、解密、签名和验证数据。以下是一些常用的OpenSSL命令行使用技巧:

  1. 生成密钥对

    • 生成RSA密钥对:
      openssl genrsa -out private_key.pem 2048
      
    • 从私钥生成公钥:
      openssl rsa -in private_key.pem -pubout -out public_key.pem
      
  2. 加密和解密文件

    • 对称加密(例如,使用AES-256-CBC算法):
      openssl enc -aes-256-cbc -e -in input.txt -out encrypted.txt -k secretkey
      
    • 对称解密:
      openssl enc -d -aes-256-cbc -in encrypted.txt -out decrypted.txt -k secretkey
      
  3. 生成自签名证书

    • 生成自签名证书:
      openssl req -new -x509 -key private_key.pem -out certificate.pem -days 365
      
  4. 使用技巧

    • 在加密和解密操作中使用 -pass 选项来指定密码:
      openssl enc -aes-128-cbc -e -in input.txt -out encrypted.txt -pass pass:yourpassword
      
    • 在加密时对输出进行base64编码,以便于传输:
      openssl enc -aes-128-cbc -e -in input.txt -out encrypted.txt -base64
      
    • 使用 -nosalt 选项在不使用盐值的情况下加密:
      openssl enc -aes-128-ecb -e -in input.txt -out encrypted.txt -nosalt -base64
      
  5. 查看支持的加密算法

    openssl list -cipher-commands
    
  6. 转换密钥格式

    • 将私钥从PEM格式转换为DER格式:
      openssl rsa -in private_key.pem -outform DER -out private_key.der
      
  7. 查看证书信息

    openssl x509 -in certificate.pem -text -noout
    

0
看了该问题的人还看了