java der格式_读取DER格式java中的私钥

我有以下代码来读取PKCS#8格式的私钥

public void encryptHash(String hashToEncrypt, String pathOfKey, String Algorithm) {

FileInputStream fis = null;

byte[] encodedKey = null;

try {

File f = new File(pathOfKey);

encodedKey = new byte[(int)f.length()];

fis = new FileInputStream(f);

fis.read(encodedKey);

fis.close();

KeyFactory kf = KeyFactory.getInstance("RSA");

PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));

Signature rsaSigner = Signature.getInstance("SHA1withRSA");

rsaSigner.initSign(privateKey);

fis = new FileInputStream(hashToEncrypt);

BufferedInputStream bis = new BufferedInputStream(fis);

byte[] buffer = new byte[1024];

int len = 0;

while ((len = bis.read(buffer)) >= 0) {

try {

rsaSigner.update(buffer, 0, len);

} catch (SignatureException ex) {

Logger.getLogger(DataEncryptor.class.getName()).log(Level.SEVERE, null, ex);

}

}

bis.close();

byte[] signature = rsaSigner.sign();

System.out.println(new String(signature));

} catch (SignatureException ex) {

Logger.getLogger(DataEncryptor.class.getName()).log(Level.SEVERE, null, ex);

} catch (InvalidKeyException ex) {

Logger.getLogger(DataEncryptor.class.getName()).log(Level.SEVERE, null, ex);

} catch (InvalidKeySpecException ex) {

Logger.getLogger(DataEncryptor.class.getName()).log(Level.SEVERE, null, ex);

} catch (FileNotFoundException ex) {

Logger.getLogger(DataEncryptor.class.getName()).log(Level.SEVERE, null, ex);

} catch (IOException ex) {

Logger.getLogger(DataEncryptor.class.getName()).log(Level.SEVERE, null, ex);

} catch (NoSuchAlgorithmException ex) {

Logger.getLogger(DataEncryptor.class.getName()).log(Level.SEVERE, null, ex);

} finally {

try {

fis.close();

} catch (IOException ex) {

Logger.getLogger(DataEncryptor.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

但我得到以下例外.

dic 09, 2011 1:59:59 PM firmaelectronica.DataEncryptor encryptHash

Grave: null

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : DER input, Integer tag error

at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)

at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)

at firmaelectronica.DataEncryptor.encryptHash(DataEncryptor.java:40)

at firmaelectronica.FirmaElectronica.main(FirmaElectronica.java:39)

Caused by: java.security.InvalidKeyException: IOException : DER input, Integer tag error

at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:361)

at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:367)

at sun.security.rsa.RSAPrivateCrtKeyImpl.(RSAPrivateCrtKeyImpl.java:91)

at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:75)

at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:316)

at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:213)

... 3 more

知道什么是错的吗?我试过OpenSSL openssl pkcs8 -inform DER -in aaa010101aaa_FIEL.key -out aaa010101aaa_FIEL_key.pem并且它可以工作但是当我想读取DER格式的密钥时它只发送该异常.

解决方法:

首先,我必须取消保护密钥,如下所示

openssl pkcs8 -inform DER -in myderPassProtectedPrivate.key -outform PEM -out myPEMPrivate.key

它告诉我我的密码,然后我有文件myPEMPrivate.key一旦完成这个继续摆脱密码保护密钥如下

openssl pkcs8 -topk8 -nocrypt -in myPEMPrivate.key -outform DER -out myNotAnyMoreProtectedPrivate.key

有了这个,我现在能够使用上面的代码加载密钥.如果我们想在java中使用传递保护密钥,则建议使用密钥库.

附:我试图避免使用openssl pkcs8 -topk8 -nocrypt -inform der -in myderPassProtectedPrivate.key -outform der -out myDERNoPassProtectedPrivate.key来摆脱保护密钥的密码的两个步骤,但我不知道为什么我有错误解密密码时出错我使用的是WinOpenSSL,这也就是我收到错误的原因.

标签:java,rsa,private-key

来源: https://codeday.me/bug/20190521/1146988.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值