Reputation: 726
I got private key which I would like to export to .pem format Problem is that, when I call privateKey.getEncoded() I get null. But I can get all info like exponent, modulus and so one (all from interface RSAPrivateKey). privateKey is org.mozilla.jss.pkcs11.PK11RSAPrivateKey Object.
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;
}
How to export that key to pem file?
Upvotes: 1
Views: 1618
Reputation: 726
Method I found in PKCS12Export, and it propably works: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);
}
Upvotes: 1