10
我想生成口令加密的专用密钥PKCS8格式,我尝试使用此代码:如何使用用密码加密的私钥生成RSA密钥对?
String password = "123456";
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);
KeyPair key = gen.generateKeyPair();
PrivateKey privateKey = key.getPrivate();
PublicKey publicKey = key.getPublic();
FileOutputStream pvt = new FileOutputStream("d:\\pvt123456.der");
try {
pvt.write(privateKey.getEncoded());
pvt.flush();
} finally {
pvt.close();
}
FileOutputStream pub = new FileOutputStream("d:\\pub123456.der");
try {
pub.write(publicKey.getEncoded());
pub.flush();
} finally {
pub.close();
}
但我不知道如何使用3DES加密密码与OpenSSL的格式兼容。
'随机随机=新的随机();'应该是'SecureRandom r andom = new SecureRandom();' – Carsten 2013-09-26 01:19:43
谢谢你的建议Carsten! – Hrzio 2014-06-26 22:41:37
从JDK 8中,应该使用'SecureRandom的随机= SecureRandom.getInstanceStrong();',以确保您使用的是强大的执行由甲骨文本文档的建议:https://docs.oracle.com/javase/tutorial /security/apisign/step2.html – CrashproofCode 2015-08-05 20:06:28