File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Read RSA Private and Public Keys from XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Read RSA Private and Public Keys from XML" Watch "Read RSA Private and Public Keys from XML" New topic
Author

Read RSA Private and Public Keys from XML

Martin Hermosilla
Greenhorn

Joined: Nov 02, 2008
Posts: 1
Hi Guys,

I've been spending lot of time on try to figureout why, standard code snipet fails. The thing is:

I have an XML file, and I'm reading a Private Key and a Public Key stored there:
I have generated those keys using openssl

<RSASK>-----BEGIN RSA PRIVATE KEY-----
MIIBOQIBAAJBALuRDb36Xqu+zmRvZbNlnmjTigWa/mxfp4zXzDh1GRXc3XeWiXHs
xzgU86ahsuyxgz/kb56ERjgToSZcLyqZ+0UCAwEAAQJAUnZycTy7FIz91qA3OUkK
/RFL1C0RgHoJgt15VrT8w2QnXZh/Mn2WqJrMXsICGt7VRrN2HeUcvFv7BHGOBDfr
HQIhAOJuQ61cycEpTSfUicxp/gOtl1DmXOOuMvar30Q8UAy7AiEA1A+JEXRCnfFp
cGGm4Ip+ZqdOs//6J8CFqeWtzp3hl/8CIEFJKZpCtqlLD0P8s/6MUQEefBKHWSEM
iQU2aRlzimEhAiBx+2+3XAA6mPoNl890vbw3PRKGmb5CA69UpMmJVz9czQIgScne
tYmV1CnrVAeRk+NNtuUbHR3zfE/p8mqBizCWo1w=
-----END RSA PRIVATE KEY-----
</RSASK>

<RSAPUBK>-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALuRDb36Xqu+zmRvZbNlnmjTigWa/mxf
p4zXzDh1GRXc3XeWiXHsxzgU86ahsuyxgz/kb56ERjgToSZcLyqZ+0UCAwEAAQ==
-----END PUBLIC KEY-----
</RSAPUBK>

The code which loads both XML tags is:



Ok here is the problem if I use the bouncy castle provider, kFactory.generatePrivate(privSpec); reads the Private key but generates a corrupt private key, when I test the byte[] obtained from prvkey.getEncoded() differs faraway from the original private key in byte[] (llave_priv)

If I use the default provider (setting KeyFactory kFactory = KeyFactory.getInstance("RSA"); ) got this exception:




java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:175)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:342)
at cl.sii.dte.Autorizacion.setDOMElement(Autorizacion.java:282)
at cl.itplus.FacturaITPlus.caf.CargarAutorizacion.cargarAutorizacionfromFile(CargarAutorizacion.java:56)
at cl.itplus.FacturaITPlus.caf.CargarAutorizacion.main(CargarAutorizacion.java:89)
Caused by: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:344)
at sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:350)
at sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:74)
at sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:58)
at sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:274)
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:171)
... 4 more


Please I really need help on this, it has been a big headache try to read those Keys!

BTW, Public Key works fine in all modes, I have no problems with Public Keys.

Thanks in Advance

-Martin

[ November 02, 2008: Message edited by: Martin Hermosilla ]
[ November 03, 2008: Message edited by: Martin Hermosilla ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read RSA Private and Public Keys from XML
 
Similar Threads
need to generate RSA keys from txt files
Encryption & Decryption Problem with "JDK default" provider
problem with RSA..... folks please help!
Store PrivateKey PublicKey in DB
How to store PublicKey and PrivateKey in database??