3

I am trying to convert a string that I stored in the SharedPreferences to PrivateKey but I am unable to do so.

This is how I am converting the PrivateKey to String,

kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
kp = kpg.genKeyPair();
publicKey = kp.getPublic();
privateKey = kp.getPrivate();

byte[] privateKeyBytes = publicKey.getEncoded();
String privKeyStr = new String(Base64.encode(privateKeyBytes));

SharedPreferences.Editor editor = getPrefs(context).edit();
editor.putString(user + "_private_key", privKeyStr + "");
editor.commit();

And this is how I am trying to retrieve the key from the SharedPreference and converting it back to PrivateKey

String privKeyStr = getPrefs(context).getString(user + "_private_key", "no private key");
Log.d("key", privKeyStr);
byte[] sigBytes = new byte[0];
try {
    sigBytes = Base64.decode(privKeyStr.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(sigBytes);
KeyFactory keyFact = KeyFactory.getInstance("RSA");
PrivateKey privateKey = null;

try {
    privateKey = keyFact.generatePrivate(privateKeySpec);  //throws exception
} catch (InvalidKeySpecException e) {
    e.printStackTrace();
}

and this is the error that I keep receiving,

java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0c0000b9:ASN.1 encoding routines:OPENSSL_internal:WRONG_TAG

I know that questions similar to this have already been asked before, but none of them seemed to solve my problem.

Please, help me know where I am going wrong.

Vishist Varugeese
  • 1,500
  • 1
  • 17
  • 30
  • https://stackoverflow.com/questions/28218636/invalidkeyspecexception-using-public-key please check this I hope help it – Jackey kabra Apr 03 '18 at 06:33
  • Can you please give an explanation or a demonstration? It would be easier to understand. – Vishist Varugeese Apr 03 '18 at 06:42
  • Instead of `Base64.decode(privKeyStr.getBytes("UTF-8"))` use `Base64.decode(privKeyStr,Base64.DEFAULT)` Also did you `commit/apply` after `editing` ? – Sagar Apr 03 '18 at 08:39

1 Answers1

5

Please change

byte[] privateKeyBytes = publicKey.getEncoded();

with

byte[] privateKeyBytes = privateKey.getEncoded();

the rest of the code seems correct

pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • Thanks a lot! That was quite silly of me! Spent a day on this – Vishist Varugeese Apr 03 '18 at 18:55
  • I am facing another problem the private key I generated is `OpenSSLRSAPrivateCrtKey{modulus=a91429b4419419c824b7665e24fdecaf5e9d8ca0f66c102a96001dce08fb92b3e289b92f6db467b5ddd8d469463bb98dd2ce7ffa7c764e1957e1145bb6caa97181a9acf1c2df812f4cabd875b5c29e8ff2092ac03bb0f1e23f6ac6dabc5382a19394f1854804dc839956258ffa76095962ff01de323bab084e6007c786dc9411,publicExponent=10001}` . – Vishist Varugeese Apr 03 '18 at 19:15
  • but after converting the string to private key I am getting `OpenSSLRSAPrivateKey{modulus=a91429b4419419c824b7665e24fdecaf5e9d8ca0f66c102a96001dce08fb92b3e289b92f6db467b5ddd8d469463bb98dd2ce7ffa7c764e1957e1145bb6caa97181a9acf1c2df812f4cabd875b5c29e8ff2092ac03bb0f1e23f6ac6dabc5382a19394f1854804dc839956258ffa76095962ff01de323bab084e6007c786dc9411` – Vishist Varugeese Apr 03 '18 at 19:15
  • I am not getting the `publicExponent` part also I am getting `OpenSSLRSAPrivateKey` instead of `OpenSSLRSAPrivateCrtKey` – Vishist Varugeese Apr 03 '18 at 19:20
  • You should not use the internal implementation class because it changes between versions. You can cas the `PrivateKey` to `RSAPrivateKey` or `RSAPrivateCrtKey`.I suppose you can extract the public exponent of the latter. – pedrofb Apr 03 '18 at 19:52