When you call getEncoded()
on an RSA private key, most providers will return the private key in PKCS #8 format. This is the case with your example.
To reverse the process, use a KeyFactory
with a PKCS8EncodedKeySpec
as follows:
String key = "30820278020...1608"; // omitted for brevity
byte[] keyData = DatatypeConverter.parseHexBinary(key);
KeyFactory factory = KeyFactory.getInstance("RSA");
RSAPrivateKey privateKey = (RSAPrivateKey) factory
.generatePrivate(new PKCS8EncodedKeySpec(keyData));
Signature signature = Signature.getInstance("SHA512withRSA");
signature.initSign(privateKey);
signature.update("Hello, World".getBytes());
System.out.println(DatatypeConverter.printHexBinary(signature.sign()));