我得到了私钥,我想将其导出为.pem格式问题是,当我调用privateKey.getEncoded()时,我会得到null。但我可以获得所有的信息,比如指数,模数等等(都来自RSAPrivateKey接口)。privateKey是org.mozilla.jss.pkcs1.PK11RSPrivateKey对象。
public String exportPrivateKey(PrivateKey privateKey) throws Throwable {
byte[] encoded = privateKey.getEncoded();//this is null:<
String body = DatatypeConverter.printBase64Binary(encoded);
return RSA_PRIVATE_HEADER + body + RSA_PRIVATE_FOOTER;
}
如何将该密钥导出到pem文件?
我在PKCS12Export中找到的方法,它可以正常工作:p
private byte[] getEncodedKey(PrivateKey var1) throws Exception {
CryptoManager var2 = CryptoManager.getInstance();
CryptoToken var3 = var2.getInternalKeyStorageToken();
KeyGenerator var4 = var3.getKeyGenerator(KeyGenAlgorithm.DES3);
SymmetricKey var5 = var4.generate();
KeyWrapper var6 = var3.getKeyWrapper(KeyWrapAlgorithm.DES3_CBC_PAD);
byte[] var7 = new byte[]{(byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1, (byte)1};
IVParameterSpec var8 = new IVParameterSpec(var7);
var6.initWrap(var5, var8);
byte[] var9 = var6.wrap(var1);
Cipher var10 = var3.getCipherContext(EncryptionAlgorithm.DES3_CBC_PAD);
var10.initDecrypt(var5, var8);
return var10.doFinal(var9);
}