咸鱼

咸鱼是以盐腌渍后,晒干的鱼

0%

简易HTTPS 加密证书的工具

1. keytool

JDK中keytool是一个证书管理工具,可以生成“自签名证书”,多用于Java应用,如:Tomcat。

1
2
3
4
5
6
7
8
$ keytool -genkey -alias tomcat  -storetype PKCS12 -keyalg RSA -keysize 2048  -keystore keystore.p12 -validity 3650

***
*** (输入详细信息)
***

$ ls
keystore.p12 (生成p12格式证书)

2. openssl

数字证书管理工具openssl和keytool的区别: keytool没办法签发证书,而openssl能够进行签发和证书链的管理。

openssl也可以生成“自签名证书”,但不止于此,它还能够进行“签发”和“管理证书链”(双向认证)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 生成私钥文件
$ openssl genrsa -des3 -out server.key 2048
(输入安全密码)
# 生成CSR(证书签名请求)
$ openssl req -new -key server.key -out server.csr

Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Shenzhun
Locality Name (eg, city) []:Shenzhun
Organization Name (eg, company) [Internet Widgits Pty Ltd]:ApGo (公司名称)
Organizational Unit Name (eg, section) []:technology
Common Name (e.g. server FQDN or YOUR name) []:localhost (Common Name应该与域名保持一致,否则会引起浏览器警告)
Email Address []:admin@localhost

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:(可不填)
An optional company name []:(可不填)

# 生成自签名证书
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

$ ls
server.crt server.csr server.key (生成crt格式证书)

3. mkcert

mkcert 是生成本地 HTTPS 加密证书的工具,一个命令就可以生成证书,不需要任何配置。

在 Linux 上,安装 certutil

1
2
3
$ sudo apt install libnss3-tools

$ sudo yum install nss-tools

使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ mkcert -install
Created a new local CA at "/Users/filippo/Library/Application Support/mkcert" 💥
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox trust store (requires restart)! 🦊

$ mkcert example.com '*.example.org' myapp.dev localhost 127.0.0.1 ::1
Using the local CA at "/Users/filippo/Library/Application Support/mkcert" ✨

Created a new certificate valid for the following names 📜
- "example.com"
- "*.example.org"
- "myapp.dev"
- "localhost"
- "127.0.0.1"
- "::1"

The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.pem" ✅

使用方法参考