Example usage for org.bouncycastle.asn1.x509 DSAParameter DSAParameter

List of usage examples for org.bouncycastle.asn1.x509 DSAParameter DSAParameter

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 DSAParameter DSAParameter.

Prototype

public DSAParameter(BigInteger p, BigInteger q, BigInteger g) 

Source Link

Usage

From source file:dorkbox.build.util.jar.JarSigner.java

License:Apache License

private static void writeDsaKeysToFile(DSAPrivateKeyParameters wimpyPrivateKey,
        DSAPublicKeyParameters wimpyPublicKey, File wimpyKeyRawFile) throws IOException, FileNotFoundException {

    DSAParameters parameters = wimpyPublicKey.getParameters(); // has to convert to DSAParameter so encoding works.
    byte[] publicKeyBytes = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
            new DSAParameter(parameters.getP(), parameters.getQ(), parameters.getG()).toASN1Primitive()),
            new ASN1Integer(wimpyPublicKey.getY())).getEncoded();
    // SAME AS://from  w w  w  . j  av  a  2  s.c  o m
    //        Certificate[] certificates = Launcher.class.getProtectionDomain().getCodeSource().getCertificates();
    //        if (certificates.length != 1) {
    //            // WHOOPS!
    //            Exit.FailedSecurity("Incorrect certificate length!");
    //        }
    //
    //        Certificate certificate = certificates[0];
    //        PublicKey publicKey = certificate.getPublicKey();
    //        byte[] publicKeyBytes = publicKey.getEncoded();
    //
    //        digest.reset();
    //        digest.update(publicKeyBytes, 0, publicKeyBytes.length);
    //        hashPublicKeyBytes = digest.digest();

    parameters = wimpyPrivateKey.getParameters();
    byte[] privateKeyBytes = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
            new DSAParameter(parameters.getP(), parameters.getQ(), parameters.getG()).toASN1Primitive()),
            new ASN1Integer(wimpyPrivateKey.getX())).getEncoded();

    // write public length to bytes.
    byte[] publicKeySize = new byte[] { (byte) (publicKeyBytes.length >>> 24),
            (byte) (publicKeyBytes.length >>> 16), (byte) (publicKeyBytes.length >>> 8),
            (byte) (publicKeyBytes.length >>> 0) };

    ByteArrayOutputStream keyOutputStream = new ByteArrayOutputStream(
            4 + publicKeyBytes.length + privateKeyBytes.length);

    keyOutputStream.write(publicKeyBytes, 0, publicKeyBytes.length);
    keyOutputStream.write(privateKeyBytes, 0, privateKeyBytes.length);
    keyOutputStream.write(publicKeySize, 0, publicKeySize.length); // mess with people staring at the keys (store length at the end).

    displayByteHash(publicKeyBytes);

    // write out the file
    OutputStream outputStream = new FileOutputStream(wimpyKeyRawFile);
    keyOutputStream.writeTo(outputStream);
    Sys.close(outputStream);
}

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

License:Apache License

@Test
public void DsaJceSerializaion() throws IOException {

    AsymmetricCipherKeyPair generateKeyPair = CryptoDSA
            .generateKeyPair(new SecureRandom(entropySeed.getBytes()), 1024);
    DSAPrivateKeyParameters privateKey = (DSAPrivateKeyParameters) generateKeyPair.getPrivate();
    DSAPublicKeyParameters publicKey = (DSAPublicKeyParameters) generateKeyPair.getPublic();

    // public key as bytes.
    DSAParameters parameters = publicKey.getParameters();
    byte[] bs = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
            new DSAParameter(parameters.getP(), parameters.getQ(), parameters.getG()).toASN1Primitive()),
            new ASN1Integer(publicKey.getY())).getEncoded();

    parameters = privateKey.getParameters();
    byte[] bs2 = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa,
            new DSAParameter(parameters.getP(), parameters.getQ(), parameters.getG()).toASN1Primitive()),
            new ASN1Integer(privateKey.getX())).getEncoded();

    DSAPrivateKeyParameters privateKey2 = (DSAPrivateKeyParameters) PrivateKeyFactory.createKey(bs2);
    DSAPublicKeyParameters publicKey2 = (DSAPublicKeyParameters) PublicKeyFactory.createKey(bs);

    // test via signing
    byte[] bytes = "hello, my name is inigo montoya".getBytes();

    BigInteger[] signature = CryptoDSA.generateSignature(privateKey, new SecureRandom(entropySeed.getBytes()),
            bytes);//from   w  ww .j a  v a 2 s  . c  o m

    boolean verify1 = CryptoDSA.verifySignature(publicKey, bytes, signature);

    if (!verify1) {
        fail("failed signature verification");
    }

    boolean verify2 = CryptoDSA.verifySignature(publicKey2, bytes, signature);

    if (!verify2) {
        fail("failed signature verification");
    }

    // now reverse who signs what.
    BigInteger[] signatureB = CryptoDSA.generateSignature(privateKey2, new SecureRandom(entropySeed.getBytes()),
            bytes);

    boolean verifyB1 = CryptoDSA.verifySignature(publicKey, bytes, signatureB);

    if (!verifyB1) {
        fail("failed signature verification");
    }

    boolean verifyB2 = CryptoDSA.verifySignature(publicKey2, bytes, signatureB);

    if (!verifyB2) {
        fail("failed signature verification");
    }
}