tags:

views:

233

answers:

1

How to read RSA Public Key in Java from xml file?

This is file format.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<RSAKeyValue>
    <Modulus>jWa96uXeSM6hUH0E/ueihtuowdte8</Modulus>
    <Exponent>BAAQ</Exponent>
</RSAKeyValue>
+2  A: 

This element is part of XMLSec, normally it's wrapped inside <KeyInfo>. Java 6 comes with XMLSec support but I doubt it has public interface to parse this single element.

This is simply Base64 encoded public key. Say you Base64-decode XML content into byte array as modBytes and expBytes. You can convert it into JCE key like this,

     KeyFactory rsaFactory = KeyFactory.getInstance("RSA");
     RSAPublicKeySpec rsaKeyspec =
        new RSAPublicKeySpec(new BigInteger(modBytes),
           new BigInteger(expBytes));
     PublicKey key = rsaFactory.generatePublic(rsaKeyspec);
ZZ Coder