Example usage for org.bouncycastle.jce.provider JCERSAPrivateCrtKey getPublicExponent

List of usage examples for org.bouncycastle.jce.provider JCERSAPrivateCrtKey getPublicExponent

Introduction

In this page you can find the example usage for org.bouncycastle.jce.provider JCERSAPrivateCrtKey getPublicExponent.

Prototype

public BigInteger getPublicExponent() 

Source Link

Document

return the public exponent.

Usage

From source file:nl.uva.vlet.grid.ssl.PrivateKeyReader.java

License:Apache License

public static byte[] getEncoded(PrivateKey inKey) {
    JCERSAPrivateCrtKey key;
    if (inKey instanceof JCERSAPrivateCrtKey)
        key = (JCERSAPrivateCrtKey) inKey;
    else/*from  w w  w.jav a  2 s . co  m*/
        throw new IllegalArgumentException((new StringBuilder()).append("Argument was:").append(inKey)
                .append(" Expected: JCERSAPrivateCrtKey").toString());
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DEROutputStream dOut = new DEROutputStream(bOut);
    RSAPrivateKeyStructure info = new RSAPrivateKeyStructure(key.getModulus(), key.getPublicExponent(),
            key.getPrivateExponent(), key.getPrimeP(), key.getPrimeQ(), key.getPrimeExponentP(),
            key.getPrimeExponentQ(), key.getCrtCoefficient());
    try {
        dOut.writeObject(info);
        dOut.close();
    } catch (IOException e) {
        throw new RuntimeException("Error encoding RSA public key");
    }
    return bOut.toByteArray();
}

From source file:org.gluu.oxtrust.action.ManageCertificateAction.java

License:MIT License

private KeyPair getKeyPair(String fileName) {
    KeyPair pair = null;/*from ww w  .jav a 2 s.  c o m*/
    JCERSAPrivateCrtKey privateKey = null;
    PEMReader r = null;
    FileReader fileReader = null;

    File keyFile = new File(getTempCertDir() + fileName.replace("crt", "key"));
    if (keyFile.isFile()) {
        try {
            fileReader = new FileReader(keyFile);
            r = new PEMReader(fileReader, new PasswordFinder() {
                public char[] getPassword() {
                    // Since keys are stored without a password this
                    // function should not be called.
                    return null;
                }
            });

            Object keys = r.readObject();
            if (keys == null) {
                log.error(" Unable to read keys from: " + keyFile.getAbsolutePath());
                return null;
            }

            if (keys instanceof KeyPair) {
                pair = (KeyPair) keys;
                log.debug(keyFile.getAbsolutePath() + "contains KeyPair");
            } else if (keys instanceof JCERSAPrivateCrtKey) {

                privateKey = (JCERSAPrivateCrtKey) keys;
                log.debug(keyFile.getAbsolutePath() + "contains JCERSAPrivateCrtKey");
                BigInteger exponent = privateKey.getPublicExponent();
                BigInteger modulus = privateKey.getModulus();

                RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(modulus, exponent);
                PublicKey publicKey = null;
                try {
                    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

                    publicKey = keyFactory.generatePublic(publicKeySpec);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                pair = new KeyPair(publicKey, privateKey);
            } else {
                log.error(keyFile.getAbsolutePath() + " Contains unsupported key type: "
                        + keys.getClass().getName());
                return null;
            }

        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return null;
        } finally {
            try {
                r.close();
                fileReader.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return null;
            }
        }
    } else {
        log.error("Key file does not exist : " + keyFile.getAbsolutePath());
    }
    log.debug("KeyPair successfully extracted from: " + keyFile.getAbsolutePath());
    return pair;
}