0

I'm facing a problem when decoding a string encoded with Base64 to retrieve a public key.

Here is the method I use to decrypt:

public PublicKey decodeKyFrom64(String pub64) {
    try {
        byte[] keyBytes = Base64.decode(pub64.getBytes("utf-8"),Base64.DEFAULT);
        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory;
        try {
            keyFactory = KeyFactory.getInstance("RSA");
            try {
                PublicKey key = (PublicKey)keyFactory.generatePublic(spec);
                return key;
            } catch (InvalidKeySpecException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return null;
}

And to encrypt:

public String[] genKyForNewUser() {
    KeyPairGenerator kpg;
    KeyPair kp;
    PublicKey publicKey;
    PrivateKey privateKey;
    try {
        kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        kp = kpg.genKeyPair();
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();
        String privKy = Base64.encodeToString(privateKey.getEncoded(), Base64.DEFAULT);
        String pubKy = Base64.encodeToString(publicKey.getEncoded(), Base64.DEFAULT);
        Log.d("Kript","Public: "+publicKey);
        Log.d("Kript","Private: "+privateKey);
        Log.d("Kript","PubKy: "+pubKy);
        Log.d("Kript","PrivKy: "+privKy);
        String[] kys = {pubKy,privKy};
        return kys;
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

And this is the exception I got:

09-14 00:36:56.293: W/System.err(27217): java.security.spec.InvalidKeySpecException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
09-14 00:36:56.343: W/System.err(27217):    at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:101)
09-14 00:36:56.343: W/System.err(27217):    at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
09-14 00:36:56.343: W/System.err(27217):    at java.security.KeyFactory.generatePublic(KeyFactory.java:171)
09-14 00:36:56.343: W/System.err(27217):    at com.probes.sm0ke.SmoKript.decodeKyFrom64(SmoKript.java:118)
09-14 00:36:56.343: W/System.err(27217):    at com.probes.sm0ke.GestureChooser.actOnChoose(GestureChooser.java:55)
09-14 00:36:56.343: W/System.err(27217):    at com.probes.sm0ke.GestureChooser.onGesturePerformed(GestureChooser.java:46)
09-14 00:36:56.343: W/System.err(27217):    at android.gesture.GestureOverlayView.fireOnGesturePerformed(GestureOverlayView.java:729)
09-14 00:36:56.343: W/System.err(27217):    at android.gesture.GestureOverlayView.access$400(GestureOverlayView.java:55)
09-14 00:36:56.343: W/System.err(27217):    at android.gesture.GestureOverlayView$FadeOutRunnable.run(GestureOverlayView.java:744)
09-14 00:36:56.353: W/System.err(27217):    at android.os.Handler.handleCallback(Handler.java:733)
09-14 00:36:56.353: W/System.err(27217):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-14 00:36:56.353: W/System.err(27217):    at android.os.Looper.loop(Looper.java:136)
09-14 00:36:56.353: W/System.err(27217):    at android.app.ActivityThread.main(ActivityThread.java:5001)
09-14 00:36:56.353: W/System.err(27217):    at java.lang.reflect.Method.invokeNative(Native Method)
09-14 00:36:56.353: W/System.err(27217):    at java.lang.reflect.Method.invoke(Method.java:515)
09-14 00:36:56.353: W/System.err(27217):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-14 00:36:56.353: W/System.err(27217):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-14 00:36:56.353: W/System.err(27217):    at dalvik.system.NativeStart.main(Native Method)
09-14 00:36:56.353: W/System.err(27217): Caused by: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
09-14 00:36:56.353: W/System.err(27217):    at com.android.org.conscrypt.NativeCrypto.d2i_PUBKEY(Native Method)
09-14 00:36:56.353: W/System.err(27217):    at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:99)
09-14 00:36:56.353: W/System.err(27217):    ... 17 more

I really don't know why this has happened. I save the Base64 string generated for both private and public key in a SQLite database. Then I retrieve the Base64 string from the database, decode it and try to get the public key. But it doesn't work. Can anyone help me to understand the reason for this exception? When i tried the first time, I passed the generated Base64 string straight to the function to decode it, only to try, and it worked. But if I saved it in the database and then get it to decode, it doesn't work anymore.

Please help me.

ChuongPham
  • 4,761
  • 8
  • 43
  • 53
El Probes
  • 3
  • 1
  • 2

1 Answers1

0

I had the same problem, try to do this:

String pubKey = "your_modulus";
String exponent = "your_exponent";

byte[] keyBytes = Base64.decode(pubKey,Base64.DEFAULT);
byte[] exponentByte = Base64.decode(exponent,Base64.DEFAULT);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(keyBytes), new BigInteger(exponentByte));
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);

See: Android-Java RSA decrypt

Community
  • 1
  • 1