Example usage for java.security.interfaces DSAParams getP

List of usage examples for java.security.interfaces DSAParams getP

Introduction

In this page you can find the example usage for java.security.interfaces DSAParams getP.

Prototype

public BigInteger getP();

Source Link

Document

Returns the prime, p .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);/* w  w  w  .java 2 s. 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();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);// ww  w.  j av  a 2s .c  o  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();

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

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);/*ww  w.  ja  v  a2 s  .c  o  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();

    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
    PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    keyGen.initialize(1024);//from  w  w  w .  ja  v  a 2 s .c  o  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();

    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);/*from ww w. j a  v  a 2 s  .  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  w w  w  .  jav  a2s .  c o  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();

    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.znsx.util.licence.LicenceUtil.java

/**
 * ?DSA??p,q,g,j,x,y//from   w w  w . ja  v a 2  s  . c  o  m
 * 
 * @param seed
 *            ??
 * @throws Exception
 * @author huangbuji
 *         <p />
 *         Create at 2014-2-8 ?4:45:26
 */
@SuppressWarnings("restriction")
public static void genKey(String seed) throws Exception {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA");
    SecureRandom random = new SecureRandom();
    random.setSeed(seed.getBytes("utf8"));
    keygen.initialize(1024, random);

    KeyPair keyPair = keygen.generateKeyPair();
    DSAPublicKeyImpl publicKey = (DSAPublicKeyImpl) keyPair.getPublic();
    DSAPrivateKey privateKey = (DSAPrivateKey) keyPair.getPrivate();
    DSAParams dsaParams = privateKey.getParams();
    Base64 base64 = new Base64();
    String p = new String(base64.encode(dsaParams.getP().toByteArray()), "utf8");
    String q = new String(base64.encode(dsaParams.getQ().toByteArray()), "utf8");
    String g = new String(base64.encode(dsaParams.getG().toByteArray()), "utf8");
    String x = new String(base64.encode(privateKey.getX().toByteArray()), "utf8");
    String y = new String(base64.encode(publicKey.getY().toByteArray()), "utf8");
    System.out.println("P: " + p);
    System.out.println("Q: " + q);
    System.out.println("G: " + g);
    System.out.println("X: " + x);
    System.out.println("Y: " + y);

    String publicKeyString = new String(base64.encode(publicKey.getEncoded()), "utf8");
    String privateKeyString = new String(base64.encode(privateKey.getEncoded()), "utf8");
    System.err.println("public: " + publicKeyString);
    System.err.println("private: " + privateKeyString);

    File publicFile = new File("D:/binPublic.ky");
    File privateFile = new File("D:/binPrivate.ky");
    FileOutputStream out = new FileOutputStream(publicFile);
    out.write(publicKey.getEncoded());
    out.flush();
    out.close();
    out = new FileOutputStream(privateFile);
    out.write(privateKey.getEncoded());
    out.flush();
    out.close();
}

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

public static byte[] dumpKeyBlob(PublicKey publicKey) {
    ByteArrayOutputStream byteOs = new ByteArrayOutputStream();

    try {//from w w w .j  a  v a2s.  c  om
        if (publicKey instanceof RSAPublicKey) {
            RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
            DataOutputStream dos = new DataOutputStream(byteOs);
            dos.writeInt("ssh-rsa".getBytes().length);
            dos.write("ssh-rsa".getBytes());
            dos.writeInt(rsaPublicKey.getPublicExponent().toByteArray().length);
            dos.write(rsaPublicKey.getPublicExponent().toByteArray());
            dos.writeInt(rsaPublicKey.getModulus().toByteArray().length);
            dos.write(rsaPublicKey.getModulus().toByteArray());
        } else if (publicKey instanceof DSAPublicKey) {
            DSAPublicKey dsaPublicKey = (DSAPublicKey) publicKey;
            DSAParams dsaParams = dsaPublicKey.getParams();

            DataOutputStream dos = new DataOutputStream(byteOs);
            dos.writeInt("ssh-dss".getBytes().length);
            dos.write("ssh-dss".getBytes());
            dos.writeInt(dsaParams.getP().toByteArray().length);
            dos.write(dsaParams.getP().toByteArray());
            dos.writeInt(dsaParams.getQ().toByteArray().length);
            dos.write(dsaParams.getQ().toByteArray());
            dos.writeInt(dsaParams.getG().toByteArray().length);
            dos.write(dsaParams.getG().toByteArray());
            dos.writeInt(dsaPublicKey.getY().toByteArray().length);
            dos.write(dsaPublicKey.getY().toByteArray());
        } else {
            throw new IllegalArgumentException("Not a supported public key: " + publicKey);
        }
    } catch (IOException e) {
        // shouldn't happen
        LOGGER.error("failed to dump public key blob", e);
    }
    return byteOs.toByteArray();
}

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

private ASN1Sequence createPublicKeyAndChallenge() throws SpkacException {
    ASN1EncodableVector publicKeyAlgorithm = new ASN1EncodableVector();
    publicKeyAlgorithm.add(new ASN1ObjectIdentifier(getPublicKeyAlg().oid()));

    if (getPublicKey() instanceof RSAPublicKey) {
        publicKeyAlgorithm.add(DERNull.INSTANCE);
    } else {//from  w  w  w  . j  ava 2  s . com
        DSAParams dsaParams = ((DSAPublicKey) getPublicKey()).getParams();

        ASN1EncodableVector dssParams = new ASN1EncodableVector();
        dssParams.add(new ASN1Integer(dsaParams.getP()));
        dssParams.add(new ASN1Integer(dsaParams.getQ()));
        dssParams.add(new ASN1Integer(dsaParams.getG()));

        publicKeyAlgorithm.add(new DERSequence(dssParams));
    }

    ASN1EncodableVector spki = new ASN1EncodableVector();
    spki.add(new DERSequence(publicKeyAlgorithm));
    spki.add(encodePublicKeyAsBitString(getPublicKey()));

    ASN1EncodableVector publicKeyAndChallenge = new ASN1EncodableVector();
    publicKeyAndChallenge.add(new DERSequence(spki));
    publicKeyAndChallenge.add(new DERIA5String(getChallenge()));
    return new DERSequence(publicKeyAndChallenge);
}

From source file:com.netscape.cmsutil.crypto.CryptoUtil.java

public static X509Key convertPublicKeyToX509Key(PublicKey pubk) throws InvalidKeyException {
    X509Key xKey;//from  w  w  w. j a  v  a2s .  c  o  m

    if (pubk instanceof RSAPublicKey) {
        RSAPublicKey rsaKey = (RSAPublicKey) pubk;

        xKey = new netscape.security.provider.RSAPublicKey(new BigInt(rsaKey.getModulus()),
                new BigInt(rsaKey.getPublicExponent()));
    } else if (pubk instanceof PK11ECPublicKey) {
        byte encoded[] = pubk.getEncoded();
        xKey = CryptoUtil.getPublicX509ECCKey(encoded);
    } else {
        // Assert.assert(pubk instanceof DSAPublicKey);
        DSAPublicKey dsaKey = (DSAPublicKey) pubk;
        DSAParams params = dsaKey.getParams();

        xKey = new netscape.security.provider.DSAPublicKey(dsaKey.getY(), params.getP(), params.getQ(),
                params.getG());
    }
    return xKey;
}