openssl生成RSA格式及pkcs1与pkcs8格式互相转换

openssl简介

OpenSSL 是一个开源项目,其组成主要包括一下三个组件:

  • openssl:多用途的命令行工具
  • libcrypto:加密算法库
  • libssl:加密模块应用库,实现了ssl及tls

openssl可以实现:密钥证书管理、对称加密和非对称加密等,想了解更多搜索查看官网。接下来主要围绕openssl生成RSA格式及pkcs1与pkcs8格式互相转换。

配置RSA密钥

可以参考支付宝配置密钥:https://docs.open.alipay.com/common/104740
如图:

支付宝配置密钥

生成命令使用说明:
 
  RSA密钥生成命令
  生成RSA私钥
  openssl>genrsa -out rsa_private_key.pem 1024
  生成RSA公钥
  openssl>rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
  将RSA私钥转换成PKCS8格式
  openssl>pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out pkcs8.pem
 
  注意:“>”符号后面的才是需要输入的命令。

具体操作:
openssl默认使用的是PEM格式,经过base64。

生成pem格式的私钥:

openssl genrsa -out private_key.pem 1024

[root@VM_0_2_centos sign]# openssl genrsa -out private_key.pem 1024
Generating RSA private key, 1024 bit long modulus
............................++++++
..................................................++++++
e is 65537 (0x10001)
[root@VM_0_2_centos sign]# ls
private_key.pem
[root@VM_0_2_centos sign]# cat private_key.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDH2m9FfHSMZlT1t0PfQDCNHIzvnbhq6+WlxEWTHETVKe3wHR/E
7TgqIYVRU0M1X2Jb7AKtopqer2PqS+AnTC1dUWDtxcYWdvIaYAgTEyhlvaEpX8kc
g2TW+X1VfJuw02nbimfGPcRQFVrJW2UJkAuF3jVyhy0wnfovclcEEPmaWwIDAQAB
AoGBAIY/NdWQV13kc5em5oG1xadUkVt4L+Qq19V1NWMvbxFdnAZaGfgXfU3XvI03
iAL3MsQakmX+vyxvFofRNh5Prnw7sQVu0EIH4LLDb4M7xjxQJMhwsHC6hqzYjDr2
a5UCwEggg1UTidt3a7cZgO3RGocsM0b1RF1b77fDDjgpyGdZAkEA7RcSX0ZrvX+J
u6iGylQcaJV56r4vlXqzusjn4drgLOizFLF/QkQp/MqN9zqCOvZioPv4pzNaYsBS
Wj/gOnLvHQJBANfLDqB+sljmeXlJiKX6sejWQKElOZcvasgR0/b0dPGVBfWzXEc5
ypfTpNd/Z5NfU1xtCziht2LWnNCZYWWmndcCQQCSwPM4UiiidW1V4Sz91C55dfyv
FuXxSrAxb+ZIYOjeI3Ol3XB+Hh83QFG7tFWkFZpbN1xkcl8CPTU8c8lhpH7pAkEA
zK1T7VTessXAOBNQIiZbsv3tBSu6qTSXcra+NsF9Xlq78WUzRHeDOmjLNH8qbbT/
622eNBPnTj9DM/QXVoQntwJAAjNM2+FpeC+hJvyBFCk2bSt17pmJGTfiFDgn6/zK
cTvmQC1kIb524j1hLp15ZLneBB4lhpB0mFWJsaaXioWloA==
-----END RSA PRIVATE KEY-----

生成公钥:

openssl rsa -in private_key.pem -pubout -out public_key.pem

[root@VM_0_2_centos sign]# openssl rsa -in private_key.pem -pubout -out public_key.pem
writing RSA key
[root@VM_0_2_centos sign]# ls
private_key.pem  public_key.pem
[root@VM_0_2_centos sign]# cat public_key.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDH2m9FfHSMZlT1t0PfQDCNHIzv
nbhq6+WlxEWTHETVKe3wHR/E7TgqIYVRU0M1X2Jb7AKtopqer2PqS+AnTC1dUWDt
xcYWdvIaYAgTEyhlvaEpX8kcg2TW+X1VfJuw02nbimfGPcRQFVrJW2UJkAuF3jVy
hy0wnfovclcEEPmaWwIDAQAB
-----END PUBLIC KEY-----

pkcs8格式私钥

某些语言需要pkcs8格式的私钥。 (php就不需要了)
使用openssl将刚才生成的私钥转为pkcs#8格式:
openssl pkcs8 -topk8 -inform PEM -in private_key.pem -outform PEM -nocrypt -out private_key_pkcs8.pem

[root@VM_0_2_centos sign]# openssl pkcs8 -topk8 -inform PEM -in private_key.pem -outform PEM -nocrypt -out private_key_pkcs8.pem
[root@VM_0_2_centos sign]#
[root@VM_0_2_centos sign]#  ls
private_key.pem  private_key_pkcs8.pem  public_key.pem
[root@VM_0_2_centos sign]# cat private_key_pkcs8.pem
-----BEGIN PRIVATE KEY-----
MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAMfab0V8dIxmVPW3
Q99AMI0cjO+duGrr5aXERZMcRNUp7fAdH8TtOCohhVFTQzVfYlvsAq2imp6vY+pL
4CdMLV1RYO3FxhZ28hpgCBMTKGW9oSlfyRyDZNb5fVV8m7DTaduKZ8Y9xFAVWslb
ZQmQC4XeNXKHLTCd+i9yVwQQ+ZpbAgMBAAECgYEAhj811ZBXXeRzl6bmgbXFp1SR
W3gv5CrX1XU1Yy9vEV2cBloZ+Bd9Tde8jTeIAvcyxBqSZf6/LG8Wh9E2Hk+ufDux
BW7QQgfgssNvgzvGPFAkyHCwcLqGrNiMOvZrlQLASCCDVROJ23drtxmA7dEahywz
RvVEXVvvt8MOOCnIZ1kCQQDtFxJfRmu9f4m7qIbKVBxolXnqvi+VerO6yOfh2uAs
6LMUsX9CRCn8yo33OoI69mKg+/inM1piwFJaP+A6cu8dAkEA18sOoH6yWOZ5eUmI
pfqx6NZAoSU5ly9qyBHT9vR08ZUF9bNcRznKl9Ok139nk19TXG0LOKG3Ytac0Jlh
Zaad1wJBAJLA8zhSKKJ1bVXhLP3ULnl1/K8W5fFKsDFv5khg6N4jc6XdcH4eHzdA
Ubu0VaQVmls3XGRyXwI9NTxzyWGkfukCQQDMrVPtVN6yxcA4E1AiJluy/e0FK7qp
NJdytr42wX1eWrvxZTNEd4M6aMs0fypttP/rbZ40E+dOP0Mz9BdWhCe3AkACM0zb
4Wl4L6Em/IEUKTZtK3XumYkZN+IUOCfr/MpxO+ZALWQhvnbiPWEunXlkud4EHiWG
kHSYVYmxppeKhaWg
-----END PRIVATE KEY-----

pkcs1与pkcs8格式互相转换

传统私钥格式转PKCS8在文中上面已经给出 ,这里主要是PKCS8格式私钥转换为PKCS1(传统私钥格式)
命令:openssl rsa -in pkcs8.pem -out pri_key.pem

[root@VM_0_2_centos sign]# openssl rsa -in private_key_pkcs8.pem -out pri_key.pem
writing RSA key
[root@VM_0_2_centos sign]# cat pri_key.pem
-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDH2m9FfHSMZlT1t0PfQDCNHIzvnbhq6+WlxEWTHETVKe3wHR/E
7TgqIYVRU0M1X2Jb7AKtopqer2PqS+AnTC1dUWDtxcYWdvIaYAgTEyhlvaEpX8kc
g2TW+X1VfJuw02nbimfGPcRQFVrJW2UJkAuF3jVyhy0wnfovclcEEPmaWwIDAQAB
AoGBAIY/NdWQV13kc5em5oG1xadUkVt4L+Qq19V1NWMvbxFdnAZaGfgXfU3XvI03
iAL3MsQakmX+vyxvFofRNh5Prnw7sQVu0EIH4LLDb4M7xjxQJMhwsHC6hqzYjDr2
a5UCwEggg1UTidt3a7cZgO3RGocsM0b1RF1b77fDDjgpyGdZAkEA7RcSX0ZrvX+J
u6iGylQcaJV56r4vlXqzusjn4drgLOizFLF/QkQp/MqN9zqCOvZioPv4pzNaYsBS
Wj/gOnLvHQJBANfLDqB+sljmeXlJiKX6sejWQKElOZcvasgR0/b0dPGVBfWzXEc5
ypfTpNd/Z5NfU1xtCziht2LWnNCZYWWmndcCQQCSwPM4UiiidW1V4Sz91C55dfyv
FuXxSrAxb+ZIYOjeI3Ol3XB+Hh83QFG7tFWkFZpbN1xkcl8CPTU8c8lhpH7pAkEA
zK1T7VTessXAOBNQIiZbsv3tBSu6qTSXcra+NsF9Xlq78WUzRHeDOmjLNH8qbbT/
622eNBPnTj9DM/QXVoQntwJAAjNM2+FpeC+hJvyBFCk2bSt17pmJGTfiFDgn6/zK
cTvmQC1kIb524j1hLp15ZLneBB4lhpB0mFWJsaaXioWloA==
-----END RSA PRIVATE KEY-----
[root@VM_0_2_centos sign]#

根据结果可知和上面生成的私钥是一致的

加解密demo代码

PHP版

<?php 
$pri = "-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cRQFVrJW2UJkAuF3jVy
hy0wnfovclcEEPmaWwIDAQAB
-----END PUBLIC KEY-----";
 
 
$data = "message";
 
echo "\n<br/>\n";
openssl_public_encrypt($data, $crypted, $pub);
echo base64_encode($crypted);
 
echo "\n<br/>\n";
 
openssl_private_decrypt($crypted, $decrypted, $pri);
echo $decrypted;
?>

Java版

import java.security.KeyFactory;
import java.security.PublicKey;
 
import sun.misc.*;
 
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
 
import javax.crypto.Cipher;
 
import sun.misc.BASE64Decoder;
 
public class TestRSA {
 
    public static String PRIVATE_KEY = "-----BEGIN RSA PRIVATE KEY-----" + "\n" +
            "MIICXAIBAAKBgQCd3V5+GjpOwqNks9ProDGaZT2qURAAuAodHcUM3P1mnhOh4l4u" + "\n" +
            "VdvkXunUI7K6XDg6Xu9eL1WDQc6qKXs/OD91LS+RpkIAb2gMU8hGrNfR34izZh36" + "\n" +
            "KdfWnEQpniAiFsHsg2ddAaPtGZZ8/jpjuPq32IVZJkgyKcWqnz5aL3FMXQIDAQAB" + "\n" +
            "AoGAJFdkvGTvTVhTYwhe3dxn7uIlmwLES1YSxfcneLmbADedz2OcSNBtKZqL+9Eo" + "\n" +
            "AOzf6NgYBLei2O+aUuPvwnMBcGyijFmRoZGVEj70UP0mngyAkvY/juA60QDe5kY5" + "\n" +
            "rsqj4raHUf4iDK85nNwzXu7hIlPAn+ZmH+e2Eekwo1rmgQUCQQDJerucfBKk3qrV" + "\n" +
            "xS5NRW99lKMomIsMH55Si+rbrnk8aU3SSRSD6uil3Dytv0erLZbc9d0Nr6SmBZgO" + "\n" +
            "0qzOjXDDAkEAyJVGgHMNuMbpHnim93HAP7wcejmMx7rP4zutw4OCiHrWv3kN8WUC" + "\n" +
            "u826taRFtOuiXhQ3X8+tb0l4ifae+vJ8XwJAEcmuKqChnMCz4G+qKNRKhZHL3dep" + "\n" +
            "3wYjmjIUKBT9SyIY5sng78ybgZkyGjza/Pfna9ahD4ZESQwRYq2i6BGAtQJBAJyD" + "\n" +
            "bmnYXQKro0e1mYNHbV3OKOJueZ139bM35BTFT1uzjlIF4Y1U5lco5uHJduL/YsjK" + "\n" +
            "OZM3d/t4duggWlkyUT0CQBdsjc7eQ9roxphrW4Q/6Zwi0EQ4xxM4SgxPY1RvkKFu" + "\n" +
            "i0HdYBj5KRdIbrAbisUyQeKzB7+6oTJH5+kZAU/pxao=" + "\n" +
            "-----END RSA PRIVATE KEY-----";
     
    public static String PRIVATE_KEY_PKCS8 = "-----BEGIN PRIVATE KEY-----" + "\n" +
            "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ3dXn4aOk7Co2Sz" + "\n" +
            "0+ugMZplPapREAC4Ch0dxQzc/WaeE6HiXi5V2+Re6dQjsrpcODpe714vVYNBzqop" + "\n" +
            "ez84P3UtL5GmQgBvaAxTyEas19HfiLNmHfop19acRCmeICIWweyDZ10Bo+0Zlnz+" + "\n" +
            "OmO4+rfYhVkmSDIpxaqfPlovcUxdAgMBAAECgYAkV2S8ZO9NWFNjCF7d3Gfu4iWb" + "\n" +
            "AsRLVhLF9yd4uZsAN53PY5xI0G0pmov70SgA7N/o2BgEt6LY75pS4+/CcwFwbKKM" + "\n" +
            "WZGhkZUSPvRQ/SaeDICS9j+O4DrRAN7mRjmuyqPitodR/iIMrzmc3DNe7uEiU8Cf" + "\n" +
            "5mYf57YR6TCjWuaBBQJBAMl6u5x8EqTeqtXFLk1Fb32UoyiYiwwfnlKL6tuueTxp" + "\n" +
            "TdJJFIPq6KXcPK2/R6stltz13Q2vpKYFmA7SrM6NcMMCQQDIlUaAcw24xukeeKb3" + "\n" +
            "ccA/vBx6OYzHus/jO63Dg4KIeta/eQ3xZQK7zbq1pEW066JeFDdfz61vSXiJ9p76" + "\n" +
            "8nxfAkARya4qoKGcwLPgb6oo1EqFkcvd16nfBiOaMhQoFP1LIhjmyeDvzJuBmTIa" + "\n" +
            "PNr89+dr1qEPhkRJDBFiraLoEYC1AkEAnINuadhdAqujR7WZg0dtXc4o4m55nXf1" + "\n" +
            "szfkFMVPW7OOUgXhjVTmVyjm4cl24v9iyMo5kzd3+3h26CBaWTJRPQJAF2yNzt5D" + "\n" +
            "2ujGmGtbhD/pnCLQRDjHEzhKDE9jVG+QoW6LQd1gGPkpF0husBuKxTJB4rMHv7qh" + "\n" +
            "Mkfn6RkBT+nFqg==" + "\n" +
            "-----END PRIVATE KEY-----";
 
    public static String PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----" + "\n" +
            "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCd3V5+GjpOwqNks9ProDGaZT2q" + "\n" +
            "URAAuAodHcUM3P1mnhOh4l4uVdvkXunUI7K6XDg6Xu9eL1WDQc6qKXs/OD91LS+R" + "\n" +
            "pkIAb2gMU8hGrNfR34izZh36KdfWnEQpniAiFsHsg2ddAaPtGZZ8/jpjuPq32IVZ" + "\n" +
            "JkgyKcWqnz5aL3FMXQIDAQAB" + "\n" +
            "-----END PUBLIC KEY-----";
     
    public static void main(String[] args) {
         
        String text = "message";
         
        try {
            String priStr = PRIVATE_KEY_PKCS8.replaceAll("-----BEGIN PRIVATE KEY-----\n", "");
            priStr = priStr.replaceAll("-----END PRIVATE KEY-----", "");
            byte [] privateKeyBytes = new BASE64Decoder().decodeBuffer(priStr);
            PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
             
            Cipher cipher = Cipher.getInstance("RSA"); 
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
             
            byte[] crypted = cipher.doFinal(text.getBytes());
             
            System.out.println(new BASE64Encoder().encode(crypted));
             
             
            //  decrypt blow.
             
            String pubStr = PUBLIC_KEY.replaceAll("-----BEGIN PUBLIC KEY-----\n", "");
            pubStr = pubStr.replaceAll("-----END PUBLIC KEY-----", "");
            byte [] publicKeyBytes = new BASE64Decoder().decodeBuffer(pubStr);
            X509EncodedKeySpec  pubKeySpec = new X509EncodedKeySpec(publicKeyBytes);
            keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
             
            cipher = Cipher.getInstance("RSA"); 
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
             
            byte[] bytes = cipher.doFinal(crypted);
            System.out.println(new String(bytes, "ISO-8859-1"));
             
             
        } catch (Exception e) {
            e.printStackTrace();
        }
         
    }
 
}

需要注意的是,java的代码中的private key必须使用pkcs#8格式的。
java加密 php解密 也是没问题的!
感谢大家的阅读,也希望能转发并关注我的公众号(帝都小崔Cayden)或搜索code_cayden

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,194评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,058评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,780评论 0 346
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,388评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,430评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,764评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,907评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,679评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,122评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,459评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,605评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,270评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,867评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,734评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,961评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,297评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,472评论 2 348

推荐阅读更多精彩内容