RSA私钥和公钥文件格式 (pkcs#1, pkcs#8, pkcs#12, pem)

2018年03月07日 11:57:22

阅读数:674
Format Name Description
PKCS #7 Cryptographic Message Syntax Standard A PKCS #7 file can be used to store certificates, which is a SignedData structure without data (just the certificates). The file name extension is usually .p7b.p7c
PKCS #8 Private-Key Information Syntax Standard. Used to carry private certificate keypairs (encrypted or unencrypted).
PKCS #12 Personal Information Exchange Syntax Standard. Defines a file format commonly used to store private keys with accompanying public key certificates, protected with a password-based symmetric key. It is the successor to PFX from Microsoft.
DER Distinguished Encoding Rules A binary format for keys or certificates. It is a message transfer syntax specified by the ITU in X.690.
PEM Privacy Enhanced Mail Base64 encoded DER certificates or keys, with additional header and footer lines.

The PEM private key format uses the header and footer lines: 
-----BEGIN RSA PRIVATE KEY----- 
-----END RSA PRIVATE KEY-----

The PEM public key format uses the header and footer lines: 
-----BEGIN PUBLIC KEY----- 
-----END PUBLIC KEY-----

The PEM certificate uses the header and footer lines: 
-----BEGIN CERTIFICATE----- 
-----END CERTIFICATE----- 

RSA Public Key file (PKCS#1)

The RSA Public key PEM file is specific for RSA keys.

It starts and ends with the tags:

  1.  
    -----BEGIN RSA PUBLIC KEY-----
  2.  
    BASE64 ENCODED DATA
  3.  
    -----END RSA PUBLIC KEY-----

Within the base64 encoded data the following DER structure is present:

  1.  
    RSAPublicKey ::= SEQUENCE {
  2.  
    modulus INTEGER, -- n
  3.  
    publicExponent INTEGER -- e
  4.  
    }

Public Key file (PKCS#8)

Because RSA is not used exclusively inside X509 and SSL/TLS, a more generic key format is available in the form of PKCS#8, that identifies the type of public key and contains the relevant data.

It starts and ends with the tags:

  1.  
    -----BEGIN PUBLIC KEY-----
  2.  
    BASE64 ENCODED DATA
  3.  
    -----END PUBLIC KEY-----

Within the base64 encoded data the following DER structure is present:

  1.  
    PublicKeyInfo ::= SEQUENCE {
  2.  
    algorithm AlgorithmIdentifier,
  3.  
    PublicKey BIT STRING
  4.  
    }
  5.  
     
  6.  
    AlgorithmIdentifier ::= SEQUENCE {
  7.  
    algorithm OBJECT IDENTIFIER,
  8.  
    parameters ANY DEFINED BY algorithm OPTIONAL
  9.  
    }

So for an RSA public key, the OID is 1.2.840.113549.1.1.1 and there is a RSAPublicKey as the PublicKey key data bitstring.

RSA Private Key file (PKCS#1)

The RSA private key PEM file is specific for RSA keys.

It starts and ends with the tags:

  1.  
    -----BEGIN RSA PRIVATE KEY-----
  2.  
    BASE64 ENCODED DATA
  3.  
    -----END RSA PRIVATE KEY-----

Within the base64 encoded data the following DER structure is present:

  1.  
    RSAPrivateKey ::= SEQUENCE {
  2.  
    version Version,
  3.  
    modulus INTEGER, -- n
  4.  
    publicExponent INTEGER, -- e
  5.  
    privateExponent INTEGER, -- d
  6.  
    prime1 INTEGER, -- p
  7.  
    prime2 INTEGER, -- q
  8.  
    exponent1 INTEGER, -- d mod (p-1)
  9.  
    exponent2 INTEGER, -- d mod (q-1)
  10.  
    coefficient INTEGER, -- (inverse of q) mod p
  11.  
    otherPrimeInfos OtherPrimeInfos OPTIONAL
  12.  
    }

Private Key file (PKCS#8)

Because RSA is not used exclusively inside X509 and SSL/TLS, a more generic key format is available in the form of PKCS#8, that identifies the type of private key and contains the relevant data.

The unencrypted PKCS#8 encoded data starts and ends with the tags:

  1.  
    -----BEGIN PRIVATE KEY-----
  2.  
    BASE64 ENCODED DATA
  3.  
    -----END PRIVATE KEY-----

Within the base64 encoded data the following DER structure is present:

  1.  
    PrivateKeyInfo ::= SEQUENCE {
  2.  
    version Version,
  3.  
    algorithm AlgorithmIdentifier,
  4.  
    PrivateKey BIT STRING
  5.  
    }
  6.  
     
  7.  
    AlgorithmIdentifier ::= SEQUENCE {
  8.  
    algorithm OBJECT IDENTIFIER,
  9.  
    parameters ANY DEFINED BY algorithm OPTIONAL
  10.  
    }

So for an RSA private key, the OID is 1.2.840.113549.1.1.1 and there is a RSAPrivateKey as the PrivateKey key data bitstring.

The encrypted PKCS#8 encoded data start and ends with the tags:

  1.  
    -----BEGIN ENCRYPTED PRIVATE KEY-----
  2.  
    BASE64 ENCODED DATA
  3.  
    -----END ENCRYPTED PRIVATE KEY-----

Within the base64 encoded data the following DER structure is present:

  1.  
    EncryptedPrivateKeyInfo ::= SEQUENCE {
  2.  
    encryptionAlgorithm EncryptionAlgorithmIdentifier,
  3.  
    encryptedData EncryptedData
  4.  
    }
  5.  
     
  6.  
    EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
  7.  
     
  8.  
    EncryptedData ::= OCTET STRING

The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo (see above).

RSA私钥和公钥文件格式 (pkcs#1, pkcs#8, pkcs#12, pem)的更多相关文章

  1. mac中使用终端生成RSA私钥和公钥文件

    1.打开终端输入:cd Desktop/   //进入桌面 2.OpenSSL   //打开 OpenSSL 3.生成私钥pem,  执行命令   genrsa -out rsa_private_ke ...

  2. 求求你们不要再用 RSA 私钥加密公钥解密了,这非常不安全!

    最近经常在网上看到有人说巨硬的 CNG(Cryptography Next Generation 即下一代加密技术) 只提供 RSA 公钥加密私钥解密,没有提供 RSA 私钥加密公钥解密,他们要自己封 ...

  3. C#.NET RSA 私钥签名 公钥验证签名

    C#.NET RSA 私钥签名 公钥验证签名 公钥验签 1.待签名字符串转为byte数组时,一般使用UTF8. 2.将私钥字符串(PKCS8或PKCS1格式)转为C#.NET的RSACryptoSer ...

  4. openssl mac中使用终端生成RSA私钥和公钥文件

    RSA密钥生成命令生成RSA私钥openssl>genrsa -out rsa_private_key.pem 1024生成RSA公钥openssl>rsa -in rsa_private ...

  5. 银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#

    前段时间做的银联支付,折腾了好久,拼凑的一些代码,有需要的朋友可以参考,本人.Net新手,不保证准确性! 这个银联手机支付没有SDK提供,技术支持也没有.Net的,真心不好搞! RSA加解密,这里有个 ...

  6. OpenSSL-Win32,rsa,私钥,公钥,1024,2048

    默认是rsa_private_key1024.pem , PEM格式私钥,C# ,PHP 用. 再生成 pkcs8 格式私钥, JAVA 用. 公钥无格式区分. 1024 的: openssl.exe ...

  7. 生成 RSA 私钥及公钥

    $ openssl # 进入 OpenSSL 程序 OpenSSL> genrsa -out rsa_private_key.pem 1024 # 生成私钥 OpenSSL> pkcs8 ...

  8. RSA私钥加密公钥解密、各种密钥格式转换

    此随笔解决RSA加解密相关的3个问题,详情可以查看源码. 1.公钥加密.私钥解密2.各种格式RSA密钥之间的转换3.不限制加密原文的长度

  9. 使用mac终端生成RSA私钥和公钥文件

    89:~ zhangwenquan$ 89:~ zhangwenquan$ openssl OpenSSL> genrsa -out rsa_private_key.pem 1024 Gener ...

随机推荐

  1. nginx日志增加cookie信息

    一.获取全部cookie信息 这个比较方便,直接在nginx.conf文件中添加$http_cookie log_format main '[$time_local] - $remote_addr:$ ...

  2. 下载外部jar包后,链接源码和javadoc.jar

    今天下载了一个Apache Common的一个jar包,对于引入源码和JavaDoc有了新的认识,在这里记录一下. Binaries是指二进制文件,包含使用的jar包.Source是指源码. xxx. ...

  3. 使用$http.post()提交数据后台接收不到

    传参方式是request payload,参数格式是json,而并非用的是form传参,所以在后台用接收form数据的方式接收参数就接收不到了. POST表单请求提交时,使用的Content-Type ...

  4. oracle的小语句

    select * from v$nls_parameters; 查询数据库中现在的常量 alter session set NLS_DATE_FORMAT='yyyy-mm-dd'; 更改日期显示方式

  5. HNOI2004 宠物收养所 (Treap)

    1285 宠物收养所 http://codevs.cn/problem/1285/  时间限制: 1 s  空间限制: 128000 KB     题目描述 Description 最近,阿Q开了一间 ...

  6. POJ 2318/2398 叉积性质

    2318 2398 题意:给出n条线将一块区域分成n+1块空间,再给出m个点,询问这些点在哪个空间里. 思路:由于只要求相对位置关系,而对具体位置不关心,那么易使用叉积性质得到相对位置关系(左侧/右侧 ...

  7. 新Linux系统配置yum源

    新的Linux系统安装好以后,yum的源还是需要配置一下的,我使用的是redhat6.6版本,同时为了不注册而使用更多的yum源的资源,也需要做一下的修改. 1. 删除redhat原有的yum源 # ...

  8. css3中新增的样式使用方法

    在PC版开发中由于IE原因,我们很少用到css3,但随着平板和智能手机进入我们的生活,以及现在越来越流行,在手机版和平板版开发中我们就可以大胆的使用了,下面我们探讨常用几个css3属性: 1.css3 ...

  9. CMDB概述(一)

    浅谈ITIL TIL即IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库)由英国政府部门CCTA(Central ...

  10. git创建新分支推送到远程

    1.创建本地分支 git branch 分支名,例如:git branch 2.0.1.20120806 注:2.0.1.20120806是分支名称,可以随便定义.   2.切换本地分支 git ch ...