---
title: "根据JSSE的JKS密钥库得到nginx所需的openssl格式私钥及证书"
author: "fall10"
date: "2018-01-24T09:44:31Z"
tags: []
categories: ["编程语言"]
slug: "jsse-jks-nginx-openssl"
draft: false
lastmod: "2018-01-24T10:22:06Z"
id: 2408853
url: "https://fall10.iteye.com/blog/2408853"
---

首先，源密钥库是JSSE规范的JKS格式，存在的也只是自签名证书；

其次，获取目标为openssl格式的私钥以及自签名证书，供nginx开放ssl测试用；

 

1，生成JKS格式密钥库，含自签名证书：

keytool -genkey -v -alias merrick -keyalg RSA -storetype JKS -keystore test1.jks -dname "CN=localhost,OU=merrick,O=COMPANY,L=CZ,ST=JS,C=CN" -storepass 123456 -keypass 123456 -validity 3650

 

2，把JKS格式的密钥库转为PKCS12格式的密钥库：

keytool -importkeystore -srckeystore test1.jks -destkeystore test1.p12 -srcstoretype JKS -deststoretype PKCS12 -srcstorepass 123456 -deststorepass 123456 -srcalias merrick -destalias merrick -srckeypass 123456 -destkeypass 123456 -noprompt  
查看：keytool -list -v -keystore test1.p12

 

3，导出二进制der证书：

keytool -export -alias merrick -keystore test1.p12 -storepass 123456 -file test1.cer

查看：keytool -printcert -v -file test1.cer

 

4，openssl生成文本格式私钥，无密码：

openssl pkcs12 -in test1.p12 -nocerts -nodes -out test1.key -passin pass:123456

 

5，openssl生成文本格式证书：

openssl x509 -inform der -in test1.cer -out test1.crt

 

6，nginx有关https配置：

nginx.conf配置：  
  ssl\_certificate  test1.crt;  
  ssl\_certificate\_key test1.key;

 

7，实际商业部署的需要第三方的证书认证机构签发流程的情况，以上步骤不适用。