Example usage for org.bouncycastle.crypto.generators DSAParametersGenerator DSAParametersGenerator

List of usage examples for org.bouncycastle.crypto.generators DSAParametersGenerator DSAParametersGenerator

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.generators DSAParametersGenerator DSAParametersGenerator.

Prototype

public DSAParametersGenerator() 

Source Link

Usage

From source file:com.geoxp.oss.CryptoHelperTest.java

License:Apache License

@Test
public void testSSHSignatureBlobSign_DSA() throws Exception {
    DSAKeyPairGenerator dsakpg = new DSAKeyPairGenerator();
    DSAParametersGenerator dpg = new DSAParametersGenerator();
    dpg.init(1024, 8, new SecureRandom());
    DSAParameters dsaparams = dpg.generateParameters();
    DSAKeyGenerationParameters params = new DSAKeyGenerationParameters(new SecureRandom(), dsaparams);
    dsakpg.init(params);//from  w  ww .  j a v  a 2 s.c om

    AsymmetricCipherKeyPair kp = dsakpg.generateKeyPair();

    DSAPrivateKeyParameters privParams = (DSAPrivateKeyParameters) kp.getPrivate();
    DSAPublicKeyParameters pubParams = (DSAPublicKeyParameters) kp.getPublic();

    KeySpec ks = new DSAPrivateKeySpec(privParams.getX(), privParams.getParameters().getP(),
            privParams.getParameters().getQ(), privParams.getParameters().getG());
    PrivateKey priv = KeyFactory.getInstance("DSA").generatePrivate(ks);

    ks = new DSAPublicKeySpec(pubParams.getY(), pubParams.getParameters().getP(),
            pubParams.getParameters().getQ(), pubParams.getParameters().getG());
    PublicKey pub = KeyFactory.getInstance("DSA").generatePublic(ks);

    byte[] data = PLAINTEXT.getBytes();
    byte[] sig = CryptoHelper.sshSignatureBlobSign(data, priv);

    Assert.assertTrue(CryptoHelper.sshSignatureBlobVerify(data, sig, pub));
}

From source file:dorkbox.util.crypto.CryptoDSA.java

License:Apache License

/**
 * Generates the DSA key (using RSA and SHA1)
 * <p/>/*from w w  w .ja va2 s .com*/
 * Note: this is here just for keeping track of how this is done. This should NOT be used, and instead use ECC crypto.
 */
public static AsymmetricCipherKeyPair generateKeyPair(SecureRandom secureRandom, int keyLength) {
    DSAKeyPairGenerator keyGen = new DSAKeyPairGenerator();

    DSAParametersGenerator dsaParametersGenerator = new DSAParametersGenerator();
    dsaParametersGenerator.init(keyLength, 20, secureRandom);
    DSAParameters generateParameters = dsaParametersGenerator.generateParameters();

    DSAKeyGenerationParameters params = new DSAKeyGenerationParameters(secureRandom, generateParameters);
    keyGen.init(params);
    return keyGen.generateKeyPair();
}

From source file:org.xwiki.crypto.internal.asymmetric.generator.BcDSAKeyParameterGenerator.java

License:Open Source License

/**
 * Create an instance of a DSA parameter generator using the appropriate hash algorithm.
 * @param hint hint of the hash algorithm to use.
 * @return a DSA parameter generator.//w  ww.ja  v a  2 s .c  o  m
 */
private DSAParametersGenerator getGenerator(String hint) {
    if (hint == null || hint.equals("SHA-1")) {
        return new DSAParametersGenerator();
    }

    DigestFactory factory;

    try {
        factory = manager.getInstance(DigestFactory.class, hint);
    } catch (ComponentLookupException e) {
        throw new UnsupportedOperationException("Cryptographic hash (digest) algorithm not found.", e);
    }

    if (!(factory instanceof AbstractBcDigestFactory)) {
        throw new IllegalArgumentException(
                "Requested cryptographic hash algorithm is not implemented by a factory compatible with this factory."
                        + " Factory found: " + factory.getClass().getName());
    }

    return new DSAParametersGenerator(((AbstractBcDigestFactory) factory).getDigestInstance());
}