Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read RSA Private and Public Keys from XML

 
Martin Hermosilla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic