Bouncy Castle generate public key from Modulus and Exponent

2,110 views
Skip to first unread message

t...@pixelsedge.ca

unread,
Feb 11, 2015, 10:57:34 AM2/11/15
to codenameone...@googlegroups.com
I have a C# Web API service that I need to access with my mobile app. In the header I encrypt the login information using a public private key pairing. I had this working well in the simulator was getting it across very nicely.

try
{
   
byte[] mod = Base64.decode("Removed");
   
byte[] exp = Base64.decode("Removed");

   
BigInteger modulus = new BigInteger(1, mod);
   
BigInteger pubExp = new BigInteger(1, exp);

   
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
   
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
   
RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);

   
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
   
cipher.init(Cipher.ENCRYPT_MODE, key);

   
byte[] cipherData = cipher.doFinal(text.getBytes("UTF-8"));
   
String encrypted = Base64.encode(cipherData);

   
return encrypted.replace("\n", "");

} catch (Exception ex){
   
return ex.toString();
}

Once I tried to build the app for Android so I can test it on another platform I got a bunch of errors, many seemed to be pointing to this. I had issues getting this working in the first place so I posted on Stackoverflow and I was pointed towards Bouncy Castle. I am having a difficult time converting this to something that will work with Bouncy Castle, any tips or advise on where to look?

Shai Almog

unread,
Feb 11, 2015, 12:11:17 PM2/11/15
to codenameone...@googlegroups.com, t...@pixelsedge.ca
If you look for bouncy castle here you will see some samples mostly dealing with AES. Also search for bouncy castle J2ME which is what we based our port on.
E.g. see this: http://stackoverflow.com/questions/4243650/aes-encryption-decryption-with-bouncycastle-example-in-j2me

t...@pixelsedge.ca

unread,
Feb 11, 2015, 2:22:34 PM2/11/15
to codenameone...@googlegroups.com, t...@pixelsedge.ca
Thanks for example. So I would have to change the encryption of the system from RSA to AES is what I am understanding. Thats fine and I can make that work, just didn't want to have to re-write the encryption engine on both sides now that it was working. Oh well haha
Reply all
Reply to author
Forward
0 new messages