keytool和openssl生成的证书转换

keytool生成证书示例

生成私钥+证书:

keytool -genkey -alias client -keysize 2048 -validity 3650 -keyalg RSA -dname "CN=localhost" -keypass $client_passwd -storepass $client_passwd -keystore ClientCert.jks

生成文件文件ClientCert.jks。


导出证书:

~/tmp/cert# keytool -export -alias client -keystore ClientCert.jks -storepass $client_passwd -file ClientCert.crt
Certificate stored in file <ClientCert.crt>
~/tmp/cert# ll
total 8
-rw-r--r-- 1 root root 715 Jun 14 20:24 ClientCert.crt
-rw-r--r-- 1 root root 2066 Jun 14 20:21 ClientCert.jks

keytool工具不支持导出私钥。

openssl生成证书示例

生成公钥私钥:

~/tmp/cert# openssl genrsa -des3 -out private.pem 1024
Generating RSA private key, 1024 bit long modulus
.............................++++++
...........++++++
e is 65537 (0x10001)
Enter pass phrase for private.pem:
Verifying - Enter pass phrase for private.pem:
~/tmp/cert# ll
total 4
-rw-r--r-- 1 root root 963 Jun 14 20:27 private.pem

创建证书请求:

~/tmp/cert# openssl req -subj "/C=CN/ST=BJ/L=BJ/O=HW/OU=HW/CN=CL/emailAddress=xxx@xxx.com" -new -out cert.csr -key private.pem
Enter pass phrase for private.pem:
~/tmp/cert# ll
total 8
-rw-r--r-- 1 root root 664 Jun 14 20:43 cert.csr
-rw-r--r-- 1 root root 963 Jun 14 20:27 private.pem

自签发证书:

~/tmp/cert# openssl x509 -req -in cert.csr -out public.crt -outform pem  -signkey private.pem -days 3650
Signature ok
subject=/C=CN/ST=BJ/L=BJ/O=HW/OU=HW/CN=CL/emailAddress=xxx@xxx.com
Getting Private key
Enter pass phrase for private.pem:
~/tmp/cert# ll
total 12
-rw-r--r-- 1 root root 664 Jun 14 20:43 cert.csr
-rw-r--r-- 1 root root 963 Jun 14 20:27 private.pem
-rw-r--r-- 1 root root 871 Jun 14 20:44 public.crt

转换

keytool和openssl生成的证书相互之间无法识别,keytool生成的为jsk文件,openssl默认生成的为PEM格式文件。需要先转换成pkcs12格式,然后再使用对方的命令转换成需要的格式。

keytool生成的证书转换为PEM格式

~/tmp/cert# keytool -importkeystore -srcstoretype JKS -srckeystore ServerCert.jks -srcstorepass 123456 -srcalias server -srckeypass 123456 -deststoretype PKCS12 -destkeystore client.p12 -deststorepass 123456 -destalias client -destkeypass 123456 -noprompt
root@SZV1000101361:~/tmp/cert# ll
total 24
-rw-r--r-- 1 root root 664 Jun 14 20:43 cert.csr
-rw-r--r-- 1 root root 1708 Jun 14 21:01 client.p12
-rw-r--r-- 1 root root 963 Jun 14 20:27 private.pem
-rw-r--r-- 1 root root 871 Jun 14 20:44 public.crt
-rw-r--r-- 1 root root 1372 Jun 14 20:57 ServerCert.jks
-rw-r--r-- 1 root root 1682 Jun 14 20:55 server.p12

导出证书:

~/tmp/cert# openssl pkcs12 -in client.p12 -passin pass:$passwd -nokeys -out client.pem
MAC verified OK

导出私钥:

~/tmp/cert# openssl pkcs12 -in client.p12 -passin pass:$passwd -nocerts -out client.crt
MAC verified OK
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
root@SZV1000101361:~/tmp/cert# ll
total 32
-rw-r--r-- 1 root root 664 Jun 14 20:43 cert.csr
-rw-r--r-- 1 root root 1184 Jun 14 21:10 client.crt
-rw-r--r-- 1 root root 1708 Jun 14 21:01 client.p12
-rw-r--r-- 1 root root 1127 Jun 14 21:07 client.pem
-rw-r--r-- 1 root root 963 Jun 14 20:27 private.pem
-rw-r--r-- 1 root root 871 Jun 14 20:44 public.crt
-rw-r--r-- 1 root root 1372 Jun 14 20:57 ServerCert.jks
-rw-r--r-- 1 root root 1682 Jun 14 20:55 server.p12

PEM格式证书转换为jks文件

转换为pkcs12格式:

~/tmp/cert# openssl pkcs12 -export -in public.crt -inkey private.pem -out server.p12 -name server -passin pass:${passwd} -passout pass:${passwd}
~/tmp/cert# ll
total 16
-rw-r--r-- 1 root root 664 Jun 14 20:43 cert.csr
-rw-r--r-- 1 root root 963 Jun 14 20:27 private.pem
-rw-r--r-- 1 root root 871 Jun 14 20:44 public.crt
-rw-r--r-- 1 root root 1682 Jun 14 20:55 server.p12

导入到jks中:

~/tmp/cert# keytool -importkeystore -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass ${passwd} -alias server -deststorepass ${passwd} -destkeypass ${passwd} -destkeystore ServerCert.jks
~/tmp/cert# ll
total 20
-rw-r--r-- 1 root root 664 Jun 14 20:43 cert.csr
-rw-r--r-- 1 root root 963 Jun 14 20:27 private.pem
-rw-r--r-- 1 root root 871 Jun 14 20:44 public.crt
-rw-r--r-- 1 root root 1372 Jun 14 20:57 ServerCert.jks
-rw-r--r-- 1 root root 1682 Jun 14 20:55 server.p12

keytool和openssl生成的证书转换的更多相关文章

  1. openssl生成ssl证书

    openssl生成ssl证书 x509证书一般会用到三类文,key,csr,crt. Key 是私用密钥openssl格,通常是rsa算法. Csr 是证书请求文件,用于申请证书.在制作csr文件的时 ...

  2. 使用openssl生成SSL证书完全参考手册

    一般来说,配置HTTPS/SSL的步骤为: 1.生成足够强度的私钥.需要考虑:算法,广泛采用的一般是RSA.键长度,RSA默认为512,一般应选择2048.密码,虽然私钥不一定要加密存储,但是加密存储 ...

  3. openssl生成https证书

    openssl生成https证书 分类: 其它2009-09-03 16:20 452人阅读 评论(0) 收藏 举报 includemoduleaccessapachessl服务器 openssl生成 ...

  4. 使用openssl 生成免费证书

    阅读目录 一:什么是openssl? 它的作用是?应用场景是什么? 二:使用openssl生成免费证书 回到顶部 一:什么是openssl? 它的作用是?应用场景是什么? 即百度百科说:openssl ...

  5. openssl 生成免费证书

    原文链接:https://www.cnblogs.com/tugenhua0707/p/10927722.html 一:什么是openssl? 它的作用是?应用场景是什么? 即百度百科说:openss ...

  6. OpenSSL 生成自定义证书

    前言 本文用来记录通过OpenSSL生成自定义证书并在浏览器设置可信任 准备 Linux CentOS7 系统 nginx 1.12.2 Windows 10 IE 11 chrome 71 Open ...

  7. 用Keytool和OpenSSL生成和签发数字证书

    一)keytool生成私钥文件(.key)和签名请求文件(.csr),openssl签发数字证书      J2SDK在目录%JAVA_HOME%/bin提供了密钥库管理工具Keytool,用于管理密 ...

  8. 利用keytool、openssl生成证书文件

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/52225073 本文出自: [HansChen的博客] 用openssl指令逐步生成各 ...

  9. openssl生成https证书、转换证书格式的各种相关操作

    第一步:生成 private key.csr等文件 我们可能需要输入以下信息(交互式): --- Country Name (2 letter code) [AU]:US State or Provi ...

随机推荐

  1. UVa 10561 Treblecross (SG函数)

    题意:给定上一行字符串,其中只有 X 和 . 并且没有连续的三个 X,两个玩家要分别在 . 上放 X,如果出现三个连续的 X,则该玩家胜利,现在问你先手胜还是败,如果是胜则输出第一步可能的位置. 析: ...

  2. Jersey RESTful WebService框架学习(四)使用@FormParam

    前端 <form action="/Jersey/api/1.0/my/form" method="post"> <input type=&q ...

  3. 20171126--fragment的小项目

    1.在使用fragment时候,初始化的时候报了两个错误,解决方法如下文所示:https://www.2cto.com/kf/201706/650158.html 其实一共报了两个错误: androi ...

  4. Matlab绘图添加直角坐标轴

    绘制y=x^2,并添加直角坐标轴. clear;clc;close all %% 绘制方程 x = -10:0.01:10; figure; y = x.^2-30; plot(x,y,'k','li ...

  5. leetcode-[3]Max Points on a Line

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line 思 ...

  6. java基础-day22

    第11天  多线程 今日内容介绍 u  多线程概述 u  线程实现 u  多线程安全问题产生 & 解决方案 第1章    多线程概述 学习多线程之前,我们先要了解几个关于多线程有关的概念. A ...

  7. Android webview 退出时关闭声音 4.视频全屏 添加cookie

    全屏问题,可以参考 http://bbs.csdn.net/topics/390839259,点击 webView = (WebView) findViewById(R.id.webView); vi ...

  8. PAT甲级 1130. Infix Expression (25)

    1130. Infix Expression (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Give ...

  9. B - Avoiding a disaster

    Description Percy likes to be punctual. So much so that he always keeps three watches with him, so t ...

  10. Android-Java-Lock

    此篇博客已售票例子为例,所以首先看一个synchronized(同步锁机制)的案例 synchronized(同步锁机制)的案例 package android.java.thread19; /** ...