Example usage for java.security.spec DSAPublicKeySpec DSAPublicKeySpec

List of usage examples for java.security.spec DSAPublicKeySpec DSAPublicKeySpec

Introduction

In this page you can find the example usage for java.security.spec DSAPublicKeySpec DSAPublicKeySpec.

Prototype

public DSAPublicKeySpec(BigInteger y, BigInteger p, BigInteger q, BigInteger g) 

Source Link

Document

Creates a new DSAPublicKeySpec with the specified parameter values.

Usage

From source file:net.sf.keystore_explorer.crypto.csr.spkac.Spkac.java

private DSAPublicKey decodeDsaPublicKeyFromBitString(DERBitString der, BigInteger p, BigInteger q, BigInteger g)
        throws SpkacException {
    try {/*  w  w w.j  av  a  2  s.c  om*/
        BigInteger y = ASN1Integer.getInstance(der.getBytes()).getValue();

        KeyFactory keyFact = KeyFactory.getInstance("DSA");

        return (DSAPublicKey) keyFact.generatePublic(new DSAPublicKeySpec(y, p, q, g));
    } catch (GeneralSecurityException ex) {
        throw new SpkacException(res.getString("NoGenerateDsaPublicKeyFromSpkac.exception.message"), ex);
    } catch (Exception ex) {
        throw new SpkacException(res.getString("NoGenerateDsaPublicKeyFromSpkac.exception.message"), ex);
    }
}

From source file:org.opensaml.xml.security.SecurityHelper.java

/**
 * Derives the public key from either a DSA or RSA private key.
 * //from   ww  w  . ja va  2  s .c  o m
 * @param key the private key to derive the public key from
 * 
 * @return the derived public key
 * 
 * @throws KeyException thrown if the given private key is not a DSA or RSA key or there is a problem generating the
 *             public key
 */
public static PublicKey derivePublicKey(PrivateKey key) throws KeyException {
    KeyFactory factory;
    if (key instanceof DSAPrivateKey) {
        DSAPrivateKey dsaKey = (DSAPrivateKey) key;
        DSAParams keyParams = dsaKey.getParams();
        BigInteger y = keyParams.getQ().modPow(dsaKey.getX(), keyParams.getP());
        DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(y, keyParams.getP(), keyParams.getQ(),
                keyParams.getG());

        try {
            factory = KeyFactory.getInstance("DSA");
            return factory.generatePublic(pubKeySpec);
        } catch (GeneralSecurityException e) {
            throw new KeyException("Unable to derive public key from DSA private key", e);
        }
    } else if (key instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey rsaKey = (RSAPrivateCrtKey) key;
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());

        try {
            factory = KeyFactory.getInstance("RSA");
            return factory.generatePublic(pubKeySpec);
        } catch (GeneralSecurityException e) {
            throw new KeyException("Unable to derive public key from RSA private key", e);
        }
    } else {
        throw new KeyException("Private key was not a DSA or RSA key");
    }
}