You say that the last line throws an exception, i.e.
PrivateKey privKey = kf.generatePrivate(keySpec);
Key tuning works on the line, i.e.
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
So, the actual problem is with an array of encoded bytes. You did System.out after byte [] encoded = Base64.decode(privKeyPEM); and looked what an exit is.
I understand that if the message is in MIME format, then after certain characters it adds a combination of carriage return and new line, so the lines are not too long for the email system or wherever you use it.
In the final test, String has some "\ n" in the source code that you are using. Youβve gotten rid of the other text in the line below,
String privKeyPEM = test.replace("-----BEGIN RSA PRIVATE KEY-----\n", ""); privKeyPEM = privKeyPEM.replace("-----END RSA PRIVATE KEY-----", "");
But look at the line,
"MIIEpAIBAAKCAQEAvcCH8WsT1xyrZqq684VPJzOF3hN5DNbowZ96Ie//PN0BtRW2\n" + // and so on "-----END RSA PRIVATE KEY-----";
it may have a few "\ n" on the left, which may lead to some unwanted characters appearing when creating keys. Try System.out again and see what the encoded byte array looks like, and before that check String privKeyPEM and see if there is an extra character left in it.
Hope it will be.
source share