java - Unable to load RSA public key -


i'm trying read rsa public key shown below, exception @ line 6: java.security.spec.invalidkeyspecexception: java.security.invalidkeyexception: ioexception: algid parse error, not sequence

my code:

string rsapublickey = rsapublickeystring.replace(     "-----begin rsa public key-----\n", ""); rsapublickey = rsapublickey.replace("\n-----end rsa public key-----", ""); byte[] bytes = encryptionutils.decodebase64(rsapublickey);  keyfactory keyfactory = keyfactory.getinstance("rsa"); x509encodedkeyspec keyspec = new x509encodedkeyspec(bytes); pubkey = (rsapublickey)keyfactory.generatepublic(keyspec); 

rsa public key:

-----begin rsa public key----- miibcgkcaqeawvacpi9w23mf3tbkdzz+zwrzkoaaqdr01vabu4e1pvkfj4sqdsm6 lydons789svod/xcs9y0hkkc3gtl1tsftlgcmooul9lcixlekzwkenj1yz/s7das an9tqw3bfuv/nqgbhgx81v/+7rfaed+rwfnk7a+xyl9sluzhryvvattveb2gaztw efzk2dwgkbluml8oremvfrax3bkhzjtkx4eqsjbbbdj2zxisrryoxfaa+xayegb+ 8hdllmajbcvfaigxx0cdqwer1yfl9kwd9p0nszrpsmoqvwmbmu7mstfai6aihc3n slv8kg9qv1m6xhvqy3pnew+qqtqsixklhwidaqab -----end rsa public key----- 

what doing wrong?

upd:

public static byte[] decodebase64(string data) throws encryptionexception {     try {         base64decoder decoder = new base64decoder();         return decoder.decodebuffer(data);     } catch (exception e) {         throw new encryptionexception(e);     } } 

your problem caused because public key rsapublickey object rather subjectpublickeyinfo object (see this answer description of difference). need convert 1 other before code work.

bouncycastle can conversion you. following code snippet work, although dislike 2 reasons:

  1. it uses deprecated class pemreader.

  2. it requires bouncycastle provider loaded.

code:

security.addprovider(new bouncycastleprovider());     pemreader reader = new pemreader(new stringreader(rsapublickeystring));     bcrsapublickey key = (bcrsapublickey) reader.readobject(); bytes[] = key.getencoded(); // in subjectpublickeyinfo format.  // before... 

with bouncycastle, there many ways skin cat. perhaps can find more elegant solution 1 above?


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -