PEMReader最新的使用方法
来源:互联网 发布:2017程序员猝死 编辑:程序博客网 时间:2024/06/06 03:15
参考链接:http://bouncy-castle.1462172.n4.nabble.com/Problem-using-PEMReader-to-read-openssl-generated-private-key-td4382977.html
最近用到PEMReader,在官网下载最新的jar包之后,他的用法和以前不一样了,记录如下:
新版:
问题:
final PemObject privatePemObject = new PEMReader(new
FileReader(new
File("/Users/edalquist/tmp/crypt/my.wisc.edu-private.pem"))).readPemObject();
final PemObject publicPemObject = new PEMReader(new
FileReader(new
File("/Users/edalquist/tmp/crypt/my.wisc.edu-public.pem"))).readPemObject();
final AsymmetricKeyParameter publicKey =
PublicKeyFactory.createKey(publicPemObject.getContent());
final AsymmetricKeyParameter privateKey =
PrivateKeyFactory.createKey(privatePemObject.getContent());
The publicKey loads correctly but the privateKey fails with the
following stack trace:
java.lang.ClassCastException: org.bouncycastle.asn1.ASN1Integer cannot
be cast to org.bouncycastle.asn1.ASN1Sequence
at org.bouncycastle.asn1.pkcs.PrivateKeyInfo.<init>(Unknown Source)
at org.bouncycastle.asn1.pkcs.PrivateKeyInfo.getInstance(Unknown
Source)
at org.bouncycastle.crypto.util.PrivateKeyFactory.createKey(Unknown
Source)
at
org.jasig.tokencrypt.TokenEncryptDecryptTest.testEncrypt(TokenEncryptDecryptTest.java:70)
回答:
An OpenSSL RSA private key is not encoded as a PrivateKeyInfo object,
it's just encoded as an RSAPrivateKey pkcs structure, without the
surrounding wrapper.
Under the current beta you could create the private key info as:
new PrivateKeyInfo(new
AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()),
RSAPrivateKey.getInstance(privatePemObject.getContent()));
You may need to use ASN1Object.fromByteArray() to convert the content in
earlier ones.
旧版:
import org.bouncycastle.openssl.PEMReader;
//load client private key
PEMReader mReader = new PEMReader(new InputStreamReader( new FileInputStream("/storage/qiyi-key.pem")));
KeyPair mkey =(KeyPair) mReader.readObject();
PrivateKey mPkey = (PrivateKey)mkey.getPrivateKey();
- PEMReader最新的使用方法
- CocoaPods的最新使用方法
- IOS pod最新的安装和使用方法
- cocoapods最新使用方法
- cocoapods最新使用方法
- 最新automake使用方法
- Tomcat与ant组合使用方法(最新tomcat5的使用)
- ios 中数组和字典的最新使用方法研究
- 最新微信记录恢复工具MMRecovery的下载与使用方法
- 2017最最最最新的Android发布aar给别人使用方法
- MySQL 最新商业版备份工具使用方法
- JRebel最新破解版及使用方法
- iOS开发--语言国际化-最新使用方法
- JRebel最新破解版及使用方法
- 最新phpstorm与webstrom破解汉化使用方法
- 编译及安装最新的qt/x11 4.3.4,qtopia 4.3.1 ,qtopia-core-opensource-src-4.3.3以及使用方法
- 的使用方法
- 红旗桌面4.0正式版最新使用方法和问题解答100例
- 7:---(3)CSS3中的变形--缩放 scale()
- static静态变量
- 二叉树的创建与三种遍历(先序,中序,后序)
- Androidの矢量图形之VectorDrawable研究
- 数据库概述(发展历程,关系数据库/非关系数据库/SQL)
- PEMReader最新的使用方法
- linux 下安装jdk8和Jetty9.4.6
- svn: Aborting commit: 'E:\myeclipse\workplaces\……“ remains in conflict错误的解决方法
- 解决 Illegal mix of collations (utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE)
- android 实现淘宝授权功能
- apache性能测试工具ab使用详解
- Unity 5.x标准物理材质包
- Electron+NodeJS+Node-Serialport安装备忘
- 理解矩阵(三)