Example usage for java.security.spec DSAPrivateKeySpec DSAPrivateKeySpec

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

Introduction

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

Prototype

public DSAPrivateKeySpec(BigInteger x, BigInteger p, BigInteger q, BigInteger g) 

Source Link

Document

Creates a new DSAPrivateKeySpec with the specified parameter values.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);// w  ww .j  a v a 2  s. com
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();

    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();

    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
    PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);/* ww  w  .j ava2s  . co  m*/
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();

    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();

    // Create the DSA key factory
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    // Create the DSA private key
    KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
    PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec);

    byte[] buffer = new byte[1024];

    Signature sig = Signature.getInstance(privateKey1.getAlgorithm());
    sig.initSign(privateKey1);
    sig.update(buffer, 0, buffer.length);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);/*from ww  w  . j av  a  2s .  c om*/
    KeyPair keypair = keyGen.genKeyPair();
    DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate();
    DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic();

    DSAParams dsaParams = privateKey.getParams();
    BigInteger p = dsaParams.getP();
    BigInteger q = dsaParams.getQ();
    BigInteger g = dsaParams.getG();
    BigInteger x = privateKey.getX();
    BigInteger y = publicKey.getY();

    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
    PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec);
    KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
    PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec);

    byte[] buffer = new byte[1024];

    Signature sig = Signature.getInstance(privateKey1.getAlgorithm());
    sig.initSign(privateKey1);
    sig.update(buffer, 0, buffer.length);

    byte[] signature = sig.sign();

    sig = Signature.getInstance(publicKey1.getAlgorithm());
    sig.initVerify(publicKey1);
    sig.update(buffer, 0, buffer.length);
    sig.verify(signature);

}

From source file:com.adito.security.pki.dsa.SshDssPrivateKey.java

/**
 * Creates a new SshDssPrivateKey object.
 *
 * @param key//from  ww w .  j  a v  a  2 s  . co m
 *
 * @throws InvalidKeyException
 */
public SshDssPrivateKey(byte[] key) throws InvalidKeyException {
    try {
        DSAPrivateKeySpec dsaKey;

        // Extract the key information
        ByteArrayReader bar = new ByteArrayReader(key);
        String header = bar.readString();

        if (!header.equals(getAlgorithmName())) {
            throw new InvalidKeyException();
        }

        BigInteger p = bar.readBigInteger();
        BigInteger q = bar.readBigInteger();
        BigInteger g = bar.readBigInteger();
        BigInteger x = bar.readBigInteger();
        dsaKey = new DSAPrivateKeySpec(x, p, q, g);

        KeyFactory kf = KeyFactory.getInstance("DSA");
        prvkey = (DSAPrivateKey) kf.generatePrivate(dsaKey);
    } catch (Exception e) {
        throw new InvalidKeyException();
    }
}

From source file:com.sshtools.j2ssh.transport.publickey.dsa.SshDssPrivateKey.java

/**
 * Creates a new SshDssPrivateKey object.
 *
 * @param key//from  w  w w  .ja  v a2  s .  c o  m
 *
 * @throws InvalidSshKeyException
 */
public SshDssPrivateKey(byte[] key) throws InvalidSshKeyException {
    try {
        DSAPrivateKeySpec dsaKey;

        // Extract the key information
        ByteArrayReader bar = new ByteArrayReader(key);

        String header = bar.readString();

        if (!header.equals(getAlgorithmName())) {
            throw new InvalidSshKeyException();
        }

        BigInteger p = bar.readBigInteger();
        BigInteger q = bar.readBigInteger();
        BigInteger g = bar.readBigInteger();
        BigInteger x = bar.readBigInteger();

        dsaKey = new DSAPrivateKeySpec(x, p, q, g);

        KeyFactory kf = KeyFactory.getInstance("DSA");
        prvkey = (DSAPrivateKey) kf.generatePrivate(dsaKey);

        log.info(prvkey.getParams().getP().toString(16));
        log.info(prvkey.getParams().getQ().toString(16));
        log.info(prvkey.getParams().getG().toString(16));
        log.info(getY().toString(16));

    } catch (Exception e) {
        throw new InvalidSshKeyException();
    }
}

From source file:net.adamcin.httpsig.testutil.KeyTestUtil.java

public static KeyPair getKeyPairFromProperties(String parentName, String keyName) {
    InputStream is = null;//from w  w  w  .  j a v  a2 s  . c  o  m
    try {
        is = KeyTestUtil.class.getResourceAsStream("/" + parentName + "/" + keyName + ".properties");
        Properties props = new Properties();
        props.load(is);
        if (TYPE_RSA.equals(props.getProperty(P_TYPE))) {
            RSAPrivateKeySpec privSpec = null;
            if (props.getProperty(RSA_P) != null && props.getProperty(RSA_Q) != null
                    && props.getProperty(RSA_U) != null) {
                privSpec = new RSAPrivateCrtKeySpec(new BigInteger(props.getProperty(RSA_N)),
                        new BigInteger(props.getProperty(RSA_E)), new BigInteger(props.getProperty(RSA_D)),
                        new BigInteger(props.getProperty(RSA_P)), new BigInteger(props.getProperty(RSA_Q)),
                        new BigInteger(props.getProperty(RSA_PE)), new BigInteger(props.getProperty(RSA_QE)),
                        new BigInteger(props.getProperty(RSA_U)));
            } else {
                privSpec = new RSAPrivateKeySpec(new BigInteger(props.getProperty(RSA_N)),
                        new BigInteger(props.getProperty(RSA_D)));
            }
            RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(new BigInteger(props.getProperty(RSA_N)),
                    new BigInteger(props.getProperty(RSA_E)));

            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec));
        } else if (TYPE_DSA.equals(props.getProperty(P_TYPE))) {
            DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(new BigInteger(props.getProperty(DSA_X)),
                    new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)),
                    new BigInteger(props.getProperty(DSA_G)));
            DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(new BigInteger(props.getProperty(DSA_Y)),
                    new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)),
                    new BigInteger(props.getProperty(DSA_G)));
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
            return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec));
        }
    } catch (Exception e) {
        LOGGER.error("Failed to read properties", e);
    } finally {
        IOUtils.closeQuietly(is);
    }

    return null;
}