1

I'm tring to convert my private key in RSA into a Key from a string, but for some reason it's outputing an system error.

static private Key privatekey; try{ byte[] keyprivBytes = Base64.decode(KeypriString,0); PKCS8EncodedKeySpec specpriv = new PKCS8EncodedKeySpec(keyprivBytes); KeyFactory keyFactorypriv = KeyFactory.getInstance("RSA"); privatekey = keyFactorypriv.generatePrivate(specpriv); <--(here is where the logcat says the code is broken) }catch(UnsupportedEncodingException | InvalidKeySpecException | NoSuchAlgorithmException e){ e.printStackTrace(); } 

in which keypriString is the String where my private key is.

LogCat:

11-12 11:11:20.066 15141-15141/<pakage name> W/System.err: java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag 11-12 11:11:20.066 15141-15141/<pakage name> W/System.err: at com.android.org.conscrypt.OpenSSLKey.getPrivateKey(OpenSSLKey.java:180) 11-12 11:11:20.076 15141-15141/<pakage name> W/System.err: at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePrivate(OpenSSLRSAKeyFactory.java:64) 11-12 11:11:20.076 15141-15141/<pakage name> W/System.err: at java.security.KeyFactory.generatePrivate(KeyFactory.java:187) 11-12 11:11:20.076 15141-15141/<pakage name> W/System.err: at <pakage name>.Crypto.<init>(Crypto.java:62) 

Can anybody tell me the problem??

6
  • can you please post all your logcat message Commented Nov 10, 2015 at 9:51
  • this is all the logcat related to the problem, the rest is just some logs i did when using the encryption Commented Nov 10, 2015 at 9:55
  • but you havent mention what is the exception you get Commented Nov 10, 2015 at 9:56
  • @Anjali you were right i was using an tag so i didnt have to search all the code and forgot about it, logcat updated, sorry:/ Commented Nov 12, 2015 at 11:19
  • you can refer this link for RSA encoding and decoding: blog.brainattica.com/working-with-rsa-in-android Commented Nov 16, 2015 at 9:50

1 Answer 1

1

1 : verify step by step with encoding and then decoding, and trace each step to the output: you will see where it crashes

2 : possible errors: Base64.decode gives null because this is not a good base64 string

3 : or Are you confusing between PKCS and X509 ?

You should see Converting Strings to encryption keys and vice versa java , this: Java asymmetric encryption: preferred way to store public/private keys , this: Create PrivateKey and PublicKey from a String base64 encoding with DER format

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

1 Comment

half the problem i think was the confusion between PCKS8 and X509, still trying to resolve the problem

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.