I currently have a public/private key pair in as two separate strings that I am trying to convert back into public/private key format.
I am currently reading in the string from a .auth file, not a public key file and private key file so I can't use openssl pkcs8 -topk8... to get rid of the algid parse error. I am reading in the keys like so:
BufferedReader br = new BufferedReader(new FileReader(authFileName));
String publicK = br.readLine();
String privateK = br.readLine();
br.close();
From there, I am converting the files back into byte arrays like so using the Base64 Decoder like so:
byte[] privateBytes = Base64.getDecoder().decode(privateK);
byte[] publicBytes = Base64.getDecoder().decode(publicK);
Lastly I am converting those byte arrays into Public and Private keys using a key factory. Here is the code:
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateBytes));
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicBytes));
When my code tries to generate the private and public keys using the key factory, that is when it throws the InvalidKeyException algid parse error
I have looked online and noticed many people saying adding the following line to my code should fix the problem:
Security.addProvider(new BouncyCastleProvider());
But it didn't. Is there any other solution, besides bouncy castle and openssl, that I should try that worked for anyone else?