标签:pem android jar package app
To do 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 will need to write your own “Android.mk”
package com.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.widget.Button;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.openssl.*;
public class MainActivity extends Activity {
String key_N = "f22f26ef784475df476e15e3f6595329a7b4f8ad02ffb7c2c8f24baae2c6f5363c0a6f2288935c244d541bfcbfd0fd2382160672d10e8ed362a59661311094f8263810112e4a67d44a07527c514dbd2f4af7f23852645489f191802d96d12f65825a6029eedc8c284bcd2732355bf9848cabe82aef1bb4a60fa7b0e8eb8478fa1dddc36f4eeb6e7e952306e88ebddb593db1b538bde8230a8edc1237575370a8ebc8c3c64588b49397e2cad4b707b4b66515c3bf0f78b6b27dfdf0c28cd5fdc4fc91383681f6ab2f39d524df96c3277ca5d855c890ce331f655ddec5e37c52bf2ad30ec1026b117d5dae899b25b87c5ca23cd005bcc970a3f33605591aeb0755";
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
标签:pem android jar package app
原文地址:http://blog.csdn.net/spuyan/article/details/46551733