java - RSA - bouncycastle PEMReader returning PEMKeyPair instead of AsymmetricCipherKeyPair for reading private key -
i have function reads openssl formatted private key:
static asymmetrickeyparameter readprivatekey(string privatekeyfilename) { asymmetriccipherkeypair keypair; using (var reader = file.opentext(privatekeyfilename)) keypair = (asymmetriccipherkeypair)new pemreader(reader).readobject(); return keypair.private; }
and returns asymmetrickeyparameter used decrypt encrypted text.
below decrypt code:
public static byte[] decrypt3(byte[] data, string pemfilename) { string result = ""; try { asymmetrickeyparameter key = readprivatekey(pemfilename); rsaengine e = new rsaengine(); e.init(false, key); //byte[] cipheredbytes = getbytes(encryptedmsg); //debug.log (encryptedmsg); byte[] cipheredbytes = e.processblock(data, 0, data.length); //result = encoding.utf8.getstring(cipheredbytes); //return result; return cipheredbytes; } catch (exception e) { debug.log ("exception in decrypt3: " + e.message); return getbytes(e.message); } }
these work in c# using bouncy castle library , correct decrypted text. however, when added java, pemparser.readobject() returns object of type pemkeypair instead of asymmetriccipherkeypair , java throws exception trying cast it. checked in c# , returning asymmetriccipherkeypair.
i don't know why java behaves differently hope here can how cast object or read privatekey file , decrypt successfully. used same public , privatekey files in both c# , java code don't think error them.
here reference java version of how i'm reading privatekey:
public static string readprivatekey3(string pemfilename) throws filenotfoundexception, ioexception { asymmetriccipherkeypair keyparam = null; asymmetrickeyparameter keypair = null; pemkeypair kp = null; //privatekeyinfo pi = null; try { //var filestream = system.io.file.opentext(pemfilename); string absolutepath = ""; absolutepath = encryption.class.getprotectiondomain().getcodesource().getlocation().getpath(); absolutepath = absolutepath.substring(0, (absolutepath.lastindexof("/")+1)); string filepath = ""; filepath = absolutepath + pemfilename; file f = new file(filepath); //return filepath; filereader filereader = new filereader(f); pemparser r = new pemparser(filereader); keyparam = (asymmetriccipherkeypair) r.readobject(); return keyparam.tostring(); } catch (exception e) { return "hello: " + e.getmessage() + e.getlocalizedmessage() + e.tostring(); //return e.tostring(); //return pi; } }
the java code has been updated new api, yet ported across c#. try equivalent (but deprecated) java pemreader class. return jce keypair though (part of reason change because original version worked jce types, not bc lightweight classes).
if using pemparser, , pemkeypair, can use org.bouncycastle.openssl.jcajce.jcapemkeyconverter.getkeypair jce keypair it. ideally there bcpemkeyconverter, doesn't appear have been written yet. in case, should easy make asymmetriccipherkeypair:
pemkeypair kp = ...; asymmetrickeyparameter privkey = privatekeyfactory.createkey(kp.getprivatekeyinfo()); asymmetrickeyparameter pubkey = publickeyfactory.createkey(kp.getpublickeyinfo()); new asymmetriccipherkeypair(pubkey, privkey);
those factory classes in org.bouncycastle.crypto.util package.
Java - Rsa - Bouncycastle Pemreader Returning Pemkeypair Instead Of
ReplyDeleteAsymmetriccipherkeypair For Reading Private Key - >>>>> Download Now
>>>>> Download Full
Java - Rsa - Bouncycastle Pemreader Returning Pemkeypair Instead Of
Asymmetriccipherkeypair For Reading Private Key - >>>>> Download LINK
>>>>> Download Now
Java - Rsa - Bouncycastle Pemreader Returning Pemkeypair Instead Of
Asymmetriccipherkeypair For Reading Private Key - >>>>> Download Full
>>>>> Download LINK JZ