{"action":"created","issue":{"url":"https://api.github.com/repos/rtyley/spongycastle/issues/14","labels_url":"https://api.github.com/repos/rtyley/spongycastle/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/rtyley/spongycastle/issues/14/comments","events_url":"https://api.github.com/repos/rtyley/spongycastle/issues/14/events","html_url":"https://github.com/rtyley/spongycastle/issues/14","id":88965640,"number":14,"title":"Retrieve ECC Public Key from Base64 encoded string","user":{"login":"ankitpopli1891","id":1699793,"avatar_url":"https://avatars.githubusercontent.com/u/1699793?v=3","gravatar_id":"","url":"https://api.github.com/users/ankitpopli1891","html_url":"https://github.com/ankitpopli1891","followers_url":"https://api.github.com/users/ankitpopli1891/followers","following_url":"https://api.github.com/users/ankitpopli1891/following{/other_user}","gists_url":"https://api.github.com/users/ankitpopli1891/gists{/gist_id}","starred_url":"https://api.github.com/users/ankitpopli1891/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ankitpopli1891/subscriptions","organizations_url":"https://api.github.com/users/ankitpopli1891/orgs","repos_url":"https://api.github.com/users/ankitpopli1891/repos","events_url":"https://api.github.com/users/ankitpopli1891/events{/privacy}","received_events_url":"https://api.github.com/users/ankitpopli1891/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-17T09:22:29Z","updated_at":"2015-06-19T04:23:36Z","closed_at":"2015-06-19T04:22:58Z","body":"I've been trying to create an instance of `java.security.PublicKey` using a Base64 encoded ECC public key.\r\n\r\n### MainActivity.java\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n\r\n        try {\r\n            byte[] data = decodePublicKey(\"AsIAEFjzIcX+Kvhe8AmLoGUc8aYAEAwf5ecREGZ2u4RLxQuav/A=\");\r\n            PublicKey publicKey = loadPublicKey(\"secp128r1\", data);\r\n\r\n            Log.d(TAG, publicKey.toString());\r\n        } catch (SQLException | IOException | GeneralSecurityException e) {\r\n            Log.e(TAG, e.getMessage(), e);\r\n        }\r\n    }\r\n\r\n    private byte[] decodePublicKey(String s) throws UnsupportedEncodingException {\r\n        return Base64.decode(s, Base64.DEFAULT);\r\n    }\r\n\r\n    public PublicKey loadPublicKey(String curve, byte[] data)\r\n            throws SQLException, IOException, GeneralSecurityException {\r\n        Log.d(TAG, Arrays.toString(data));\r\n        // [2, -62, 0, 16, 88, -13, 33, -59, -2, 42, -8, 94, -16, 9, -117, -96, 101, 28, -15, -90, 0, 16, 12, 31, -27, -25, 17, 16, 102, 118, -69, -124, 75, -59, 11, -102, -65, -16]\r\n        Log.d(TAG, \"Length :\" + String.valueOf(data.length));\r\n\r\n        KeyFactory factory = KeyFactory.getInstance(\"ECDSA\", \"SC\");\r\n        ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec(curve);\r\n        ECCurve eccCurve = spec.getCurve();\r\n        Log.d(TAG, \"Curve: \" + eccCurve);\r\n\r\n        EllipticCurve ellipticCurve = EC5Util.convertCurve(eccCurve, spec.getSeed());\r\n\r\n        // decoding point fails, \r\n        // line no 66.\r\n        ECPoint point = ECPointUtil.decodePoint(ellipticCurve, data);\r\n        ECParameterSpec params = EC5Util.convertSpec(ellipticCurve, spec);\r\n\r\n        ECPublicKeySpec keySpec = new ECPublicKeySpec(point, params);\r\n        return factory.generatePublic(keySpec);\r\n    }\r\n\r\n### Logcat:\r\n    \r\n    Process: com.example.eccdemo, PID: 21151\r\n    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eccdemo/com.example.eccdemo.MainActivity}: java.lang.IllegalArgumentException: Incorrect length for compressed encoding\r\n            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)\r\n            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)\r\n            at android.app.ActivityThread.access$900(ActivityThread.java:147)\r\n            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)\r\n            at android.os.Handler.dispatchMessage(Handler.java:102)\r\n            at android.os.Looper.loop(Looper.java:135)\r\n            at android.app.ActivityThread.main(ActivityThread.java:5254)\r\n            at java.lang.reflect.Method.invoke(Native Method)\r\n            at java.lang.reflect.Method.invoke(Method.java:372)\r\n            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)\r\n            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)\r\n     Caused by: java.lang.IllegalArgumentException: Incorrect length for compressed encoding\r\n            at org.spongycastle.math.ec.ECCurve.decodePoint(ECCurve.java:349)\r\n            at org.spongycastle.jce.ECPointUtil.decodePoint(ECPointUtil.java:52)\r\n            at com.example.eccdemo.MainActivity.loadPublicKey(MainActivity.java:66)\r\n            at com.example.eccdemo.MainActivity.onCreate(MainActivity.java:45)\r\n            at android.app.Activity.performCreate(Activity.java:5933)\r\n            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)\r\n            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)\r\n            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)\r\n            at android.app.ActivityThread.access$900(ActivityThread.java:147)\r\n            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)\r\n            at android.os.Handler.dispatchMessage(Handler.java:102)\r\n            at android.os.Looper.loop(Looper.java:135)\r\n            at android.app.ActivityThread.main(ActivityThread.java:5254)\r\n            at java.lang.reflect.Method.invoke(Native Method)\r\n            at java.lang.reflect.Method.invoke(Method.java:372)\r\n            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)\r\n            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)\r\n\r\n\r\n## Further Inspection:\r\n\r\nIn Logcat, upon printing the decoded bytes, they actually differ from the one on the server:\r\n\r\n        Log.d(TAG, Arrays.toString(data));\r\n        [2, -62, 0, 16, 88, -13, 33, -59, -2, 42, -8, 94, -16, 9, -117, -96, 101, 28, -15, -90, 0, 16, 12, 31, -27, -25, 17, 16, 102, 118, -69, -124, 75, -59, 11, -102, -65, -16]\r\n\r\nIn python console:\r\n\r\n    In [131]: [_ for _ in ap.public_key.tobytes()]\r\n    Out[131]: [2, 194, 0, 16, 88, 243, 33, 197, 254, 42, 248, 94, 240, 9, 139, 160, 101, 28, 241, 166, 0, 16, 12, 31, 229, 231, 17, 16, 102, 118, 187, 132, 75, 197, 11, 154, 191, 240]\r\n\r\nIt would be great if someone can explain the reason to this anomaly, and also help me out on the small snippets to get `PublicKey` and `PrivateKey` instances from the respective encoded Strings.\r\n"},"comment":{"url":"https://api.github.com/repos/rtyley/spongycastle/issues/comments/113362395","html_url":"https://github.com/rtyley/spongycastle/issues/14#issuecomment-113362395","issue_url":"https://api.github.com/repos/rtyley/spongycastle/issues/14","id":113362395,"user":{"login":"ankitpopli1891","id":1699793,"avatar_url":"https://avatars.githubusercontent.com/u/1699793?v=3","gravatar_id":"","url":"https://api.github.com/users/ankitpopli1891","html_url":"https://github.com/ankitpopli1891","followers_url":"https://api.github.com/users/ankitpopli1891/followers","following_url":"https://api.github.com/users/ankitpopli1891/following{/other_user}","gists_url":"https://api.github.com/users/ankitpopli1891/gists{/gist_id}","starred_url":"https://api.github.com/users/ankitpopli1891/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ankitpopli1891/subscriptions","organizations_url":"https://api.github.com/users/ankitpopli1891/orgs","repos_url":"https://api.github.com/users/ankitpopli1891/repos","events_url":"https://api.github.com/users/ankitpopli1891/events{/privacy}","received_events_url":"https://api.github.com/users/ankitpopli1891/received_events","type":"User","site_admin":false},"created_at":"2015-06-19T04:23:36Z","updated_at":"2015-06-19T04:23:36Z","body":"Turns out the public key was corrupt.\r\n\r\nThanks"}}