to does this, we need to download 2 jar, which list below:
Bouncycastle.openssl + commons-codec-1.10
If you want to compile the app from Android source code, you'll need to write your own "android.mk"
Packagecom. Example. Getpubkey;Import Java. IO. StringWriter;Import Java. Math. BigInteger;Import Java. Security. Keyfactory;Import Java. Security. PublicKey;Import Java. Security. Interfaces. Rsapublickey;Import Java. Security. Spec. Rsapublickeyspec;Import Java. Security. Spec. X509EncodedKeySpec;Import Android. App. Activity;Import Android. OS. Bundle;Import Android. Util. Log;Import Android. View. Menu;Import Android. View. MenuItem;Import Android. View. View;Import Android. View. View. Onclicklistener;Import Android. Widgets. Button;import org. Apache. Commons. Codec. Binary. Hex;import org. Bouncycastle. OpenSSL.*;public class Mainactivity extends Activity {String key_n ="F22f26ef784475df476e15e3f6595329a7b4f8ad02ffb7c2c8f24baae2c6f5363c0a6f2288935c244d541bfcbfd0fd2382160672d10e8ed362a5 9661311094f8263810112e4a67d44a07527c514dbd2f4af7f23852645489f191802d96d12f65825a6029eedc8c284bcd2732355bf9848cabe82aef1bb 4a60fa7b0e8eb8478fa1dddc36f4eeb6e7e952306e88ebddb593db1b538bde8230a8edc1237575370a8ebc8c3c64588b49397e2cad4b707b4b66515c3 Bf0f78b6b27dfdf0c28cd5fdc4fc91383681f6ab2f39d524df96c3277ca5d855c890ce331f655ddec5e37c52bf2ad30ec1026b117d5dae899b25b87c5 ca23cd005bcc970a3f33605591aeb0755 ";String key_e ="00010001";Button BTN1 = null;static String PubKey = null;@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);BTN1 = (Button) This. Findviewbyid(R. ID. BTN1);Btn1. Setonclicklistener(New Onclicklistener () {@Override public void OnClick (View v) {try { PubKey = Convertpublickey (key_n,key_e);} catch (Exception e) {//TODO auto-generated catch block E. Printstacktrace();} Log. D("Getpub", PubKey);} });} private String Convertpublickey (string modulus, string exponent) throws Exception {PublicKey pub;StringWriter writer = new StringWriter ();BigInteger data_n = new BigInteger (Hex. Decodehex(Modulus. ToCharArray()));BigInteger data_e = new BigInteger (Hex. Decodehex(Exponent. ToCharArray())); /* Use the n,e to generate the Opensslrsapublickey * /Rsapublickeyspec spec = new Rsapublickeyspec (Data_n, Data_e);Keyfactory factory = Keyfactory. getinstance("RSA");Pub = Factory. Generatepublic(spec); /* Use the pemwriter to generate the PEM format key * /Pemwriter pemwriter = new Pemwriter (writer);Pemwriter. WriteObject(pub);Pemwriter. Flush();Pemwriter. Close();return writer. toString();}}
[Android] How to get PEM format public key from modulus and exponent