如何在JAVA中使用RSAPrivateKey的密码? [英] How to use passphrase for RSAPrivateKey in JAVA?

查看:715
本文介绍了如何在JAVA中使用RSAPrivateKey的密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RSA 2048密钥加密和解密。但为了额外的安全性,我需要使用AES 128方法为RSAPrivateKey使用密码。

I encrypt and decrypt with RSA 2048 keys. But for additional security I need to use passphrase for RSAPrivateKey with a AES 128 method.

我可以生成此密钥,但我不知道如何在 JAVA

I can generate this keys, but I don't know how to use them in JAVA.

在我的代码中,我初始化私有(密码密码)密钥(公共是相同的):

In my code I initialize private (witout passphrase) key (public is the same):

String PRIVATE_KEY_FILE_RSA = "src/pri.der";
File privKeyFile = new File(PRIVATE_KEY_FILE_RSA);

// read private key DER file
DataInputStream dis = new DataInputStream(new FileInputStream(privKeyFile));
byte[] privKeyBytes = new byte[(int) privKeyFile.length()];
dis.read(privKeyBytes);
dis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");

// decode private key
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privKeyBytes);
RSAPrivateKey privKey =(RSAPrivatKey) keyFactory.generatePublic(pubSpec);

并使用:

Algorithm algorithm = Algorithm.RSA256(pubKey, privKey);
...

我需要任何信息或示例如何输入密码。

I need for any information or examples how to enter there passphrase.

推荐答案

这个解决方案对我来说更好。

This solution is better for me.

更新

如果链接不起作用,请查找 not-yet-commons-ssl

If the link is not working, look for not-yet-commons-ssl.

我用于not-yet-commons-ssl-0.3.11.jar。
例如:

I used for not-yet-commons-ssl-0.3.11.jar. For example:

//path to private key file
String PRIVATE_KEY_FILE_RSA = "C:\\Users\\Adey";
FileInputStream in = new FileInputStream(PRIVATE_KEY_FILE_RSA);
// passphrase - the key to decode private key
String passphrase = "somepass";
PKCS8Key pkcs8 = new PKCS8Key(in, passphrase.toCharArray());
byte[] decrypted = pkcs8.getDecryptedBytes();
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decrypted);
RSAPrivateKey privKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(spec);

这篇关于如何在JAVA中使用RSAPrivateKey的密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆