PvkConvert.java is a simply utility Java class which reads a PKCS#8 encoded PrivateKeyInfo file, instantiates a RSAPrivateCrtKey instance using an RSA Keyfactory, extracts the RSA private key components and writes:
java PvkConvert PKCS#8PrivateKeyInfo_FileJava also supports easily instantiating private keys directly from Java keystore files. Since Java generally reads/writes multi-byte data in big-endian format, but the PRIVATEKEYBLOB has members formatted in little-endian order, the parsing in Java must reformat the data and reverse byte arrays.
The class contains five static methods:
private static byte privatekeyinfoToPrivatekeyblob(byte encodedPrivkey, int keyspec)
private static byte toPublickeyblob(byte modulus, int pubexp, int keyspec)
private static String privatekeyinfoToXMLRSAPriKey(byte encodedPrivkey)
private static String privatekeyinfoToXMLRSAPubKey(byte encodedPrivkey)
private static void dumpRSAPrivatekey(byte encodedPrivkey)
The first method returns a CryptoAPI PRIVATEKEYBLOB from the PKCS#8 PrivateKeyInfo (byte encodedPrivkey).
The second method returns a CryptoAPI PUBLICKEYBLOB from the already acquired modulus (with bytes already reversed) and public exponent.
The PRIVATEKEYBLOB file is constructed according to the PRIVATEKEYBLOB specification.
The PUBLICKEYBLOB file is constructed according to the PUBLICKEYBLOB specification.
The third and fourth methods return an XML-encoded RSA private and public key respectively, for .NET 1.1+ usage.
The fifth method simply dumps the RSA private key components as decimal numbers, and the corresponding (big-endian) byte array values.
Java 2 v1.5+ also supports creating more secure PKCS#8 EncryptedPrivateKeyInfo encoding from PKCS#8 unencrypted PrivateKeyInfo data. For an example using PBEWithSHA1AndDESede encryption see PKCS #8 EncryptedPrivateKeyInfo encryption (PBE).
See also: CryptoAPI PRIVATEKEYBLOB to Java PrivateKey converter