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??

Luis
  • 668
  • 1
  • 6
  • 16

1 Answers1

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

Community
  • 1
  • 1
  • half the problem i think was the confusion between PCKS8 and X509, still trying to resolve the problem – Luis Nov 12 '15 at 11:14