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h0GZdgdhkW2YFYO\nClWYyCaqhfWOeJOjtQNgBGleXRF/4EJRNJP8wTuXVK+Vnrynq6dk2tyb77TyLRTZ\nF5ZsrQKBgQCaNIpP1tGYpYTfFny5xQ8aY5e6Bd3/aC5Ays8wYb8oAPYwH9AeqWY2\nsistzq9ARRbvwXzl45HzRKYoWVd1D0D5yOwxFOW++VD3luXH1UpEB1bvxHrpIwbD\ngg9jhO9K1yri6JGA5ENx5InrvKsqodP/llszVJysBlUeAcwMo2gjNA==

Please note that above string is private key so some characters have been purposely removed.

And I am trying to convert it in bytes:

def convertToBase64(privateKeyB64: String) = {
    val bytesArray = java.util.Base64.getDecoder.decode(privateKeyB64.replaceAll("\\r\\n|\\r|\\n", ""))
    import java.security.KeyFactory
    import java.security.spec.PKCS8EncodedKeySpec
    val spec = new PKCS8EncodedKeySpec(bytesArray)
    // create key form spec
    val keyFactory = KeyFactory.getInstance("RSA")
    keyFactory.generatePrivate(spec)

  }

The above code fails at the step: val bytesArray = java.util.Base64.getDecoder.decode(privateKeyB64.replaceAll("\\r\\n|\\r|\\n", "")) with the error:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal base64 character 5c

I am not sure what is it complaining about exactly? What does 5c mean in base64?

5
  • Looks like a similar problem: stackoverflow.com/questions/28584080/… Commented Oct 26, 2022 at 8:18
  • 5c is the ASCII code of \. I see you tried to remove \r and \n but there seems to be a \ left in the string. Commented Oct 26, 2022 at 8:21
  • Don't do that replaceAll on Base64. There is just wrong and will mess up the Base64 encoded string. Also, keep in mind strings in java are utf-16. Commented Oct 26, 2022 at 9:06
  • Your base64 has been done wrong. You need to do it right. Fiddling about with a corrupt base64 string is not going to help you Commented Oct 26, 2022 at 9:58
  • 1
    If, for some reason, you can't get it again uncorrupted, try replaceAll("\\\\n", ""); Commented Oct 26, 2022 at 10:10

1 Answer 1

4

Perhaps you should double check the encoding of your Base64 data. Base64 data can come encoded in different formats as defined in RFC 4648 and RFC 2045. Java has different implementations for three different types of encoding as documented in the java.util.Base64 JavaDoc, Basic, MIME and URL-safe. From a couple of experiments it looks like yours is in the MIME format, so the following should work:

def convertToBase64(privateKeyB64: String) = {
  val bytesArray = java.util.Base64.getMimeDecoder.decode(privateKeyB64)
  val spec = new java.security.spec.PKCS8EncodedKeySpec(bytesArray)
  java.security.KeyFactory.getInstance("RSA").generatePrivate(spec)
}

I tried to run it locally but now the problem shifts towards generating the private key, so there may be some mismatch between the key you are generating and the expected format. Perhaps it's not a valid PKCS#8?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much @stefanobaghino. You have no idea how much helpful this was to me. When I tried out your code, it gave me a new error: Algid parse error, not a sequence and then I checked stackoverflow.com/questions/6559272/… and finally its working now. Thanks a ton! just made my difficult day.
Btw, is it possible to perform perm steps programatically instead of using cli? I had to generate those variables manually through terminal commands: openssl pkcs8 -topk8 -inform PEM -in private.pem -out private_key.pem -nocrypt?
Java has APIs for that as well, you can start from docs.oracle.com/javase/tutorial/security/apisign/step2.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.