{"id":6847,"date":"2014-05-02T01:27:50","date_gmt":"2014-05-02T01:27:50","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/02\/rsa-encryption-decryption-crashes-on-android-2-2-duplicate-collection-of-common-programming-errors\/"},"modified":"2014-05-02T01:27:50","modified_gmt":"2014-05-02T01:27:50","slug":"rsa-encryption-decryption-crashes-on-android-2-2-duplicate-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/02\/rsa-encryption-decryption-crashes-on-android-2-2-duplicate-collection-of-common-programming-errors\/","title":{"rendered":"RSA encryption\/decryption crashes on android 2.2 [duplicate]-Collection of common programming errors"},"content":{"rendered":"<p>i&#8217;m developing an android app. Unfortunately i have a compatibility problem. My source code works with firmware 4.1, but crashes with devices with firmware 2.2. The error seems to be a &#8220;ClassNotFoundException&#8221; caused by the ObjectInputStream.<\/p>\n<p>My sourcecode for the encryption and decryption:<\/p>\n<pre><code>private Key getPublicKey() {\n    try {\n        InputStream fis = activity.getResources().openRawResource(R.raw.publick);\n        ObjectInputStream ois = new ObjectInputStream(fis);\n        Key RSApublicKey = (Key) ois.readObject();\n        return RSApublicKey;\n    }\n    catch (FileNotFoundException e) {\n        e.printStackTrace();\n    }\n    catch (IOException e) {\n        e.printStackTrace();\n    }\n    catch (ClassNotFoundException e) {\n        e.printStackTrace();\n    }\n    return null;\n}\n\nprivate Key getPrivateKey() {\n    try {\n        InputStream fis = activity.getResources().openRawResource(R.raw.privatek);\n        ObjectInputStream ois = new ObjectInputStream(fis);\n        Key RSAprivateKey = (Key) ois.readObject();\n        return RSAprivateKey;\n    }\n    catch (FileNotFoundException e) {\n        e.printStackTrace();\n    }\n    catch (IOException e) {\n        e.printStackTrace();\n    }\n    catch (ClassNotFoundException e) {\n        Log.e(\"error\", \"Error: \" + e);\n        e.printStackTrace();\n    }\n    return null;\n}\n\npublic byte[] encrypt(String data) {\n    byte[] encrypted = null;\n    try {\n        Cipher cipher = Cipher.getInstance(\"RSA\");\n        cipher.init(Cipher.ENCRYPT_MODE, getPublicKey());\n        encrypted = cipher.doFinal(data.getBytes());\n    }\n    catch (IllegalBlockSizeException e) {\n            e.printStackTrace();\n    }\n    catch (BadPaddingException e) {\n        e.printStackTrace();\n    }\n    catch (InvalidKeyException e) {\n        e.printStackTrace();\n    }\n    catch (NoSuchAlgorithmException e) {\n        e.printStackTrace();\n    }\n    catch (NoSuchPaddingException e) {\n        e.printStackTrace();\n    }\n    catch (NoSuchProviderException e) {\n        e.printStackTrace();\n    }\n    return encrypted;\n}\n\npublic String decrypt(byte[] encrypted) {\n    byte[] decrypted = null;\n    try {\n        Cipher cipher;\n        cipher = Cipher.getInstance(\"RSA\");\n        cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());\n        decrypted = cipher.doFinal(encrypted);\n    }\n    catch (NoSuchAlgorithmException e) {\n        e.printStackTrace();\n    }\n    catch (NoSuchPaddingException e) {\n        e.printStackTrace();\n    }\n    catch (InvalidKeyException e) {\n        e.printStackTrace();\n    }\n    catch (IllegalBlockSizeException e) {\n        e.printStackTrace();\n    }\n    catch (BadPaddingException e) {\n        e.printStackTrace();\n    }\n    catch (NoSuchProviderException e) {\n        e.printStackTrace();\n    }\n    return new String(decrypted);\n}\n<\/code><\/pre>\n<p>The error in logcat:<\/p>\n<pre><code>03-23 10:13:19.706: E\/Error(427): Error: java.lang.ClassNotFoundException: com.android.org.bouncycastle.jce.provider.JCERSAPrivateCrtKey\n<\/code><\/pre>\n<p>Do you have any ideas, how i could get it work with android 2.2?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>i&#8217;m developing an android app. Unfortunately i have a compatibility problem. My source code works with firmware 4.1, but crashes with devices with firmware 2.2. The error seems to be a &#8220;ClassNotFoundException&#8221; caused by the ObjectInputStream. My sourcecode for the encryption and decryption: private Key getPublicKey() { try { InputStream fis = activity.getResources().openRawResource(R.raw.publick); ObjectInputStream ois [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6847","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6847"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6847\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}