温馨提示×

Debian中OpenSSL如何生成密钥

小樊
57
2026-09-20 21:02:25
栏目: 云计算

在 Debian 中,使用 OpenSSL 生成密钥非常常见,下面按「使用场景」给你几种常用方法。


一、生成 RSA 私钥(最常用)

1. 生成 2048 位 RSA 私钥

openssl genrsa -out rsa_private.key 2048

2. 生成带密码保护的 RSA 私钥

openssl genrsa -aes256 -out rsa_private.key 2048

会提示你输入密码。

3. 从私钥导出公钥

openssl rsa -in rsa_private.key -pubout -out rsa_public.key

二、生成 ED25519 密钥(现代、推荐)

1. 生成私钥

openssl genpkey -algorithm ed25519 -out ed25519.key

2. 导出公钥

openssl pkey -in ed25519.key -pubout -out ed25519.pub

三、生成 ECDSA 密钥

1. 生成 EC 私钥(P-256)

openssl ecparam -genkey -name prime256v1 -noout -out ec.key

2. 导出公钥

openssl ec -in ec.key -pubout -out ec.pub

四、生成用于 HTTPS 的私钥 + 自签名证书

openssl req -x509 -newkey rsa:2048 -nodes \
  -keyout key.pem -out cert.pem -days 365
  • -nodes:私钥不加密
  • 会交互式填写国家、组织等信息

五、查看密钥信息

openssl rsa -in rsa_private.key -text -noout

或

openssl pkey -in ed25519.key -text -noout

六、Debian 上确认 OpenSSL 已安装

openssl version

未安装可装:

sudo apt update
sudo apt install openssl

如果你有具体用途(如 SSH、HTTPS、JWT、CA、签名),我可以给你更精确的命令示例。

0 踩