Example usage for javax.crypto.interfaces DHPublicKey getY

List of usage examples for javax.crypto.interfaces DHPublicKey getY

Introduction

In this page you can find the example usage for javax.crypto.interfaces DHPublicKey getY.

Prototype

BigInteger getY();

Source Link

Document

Returns the public value, y.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88"
            + "11111111111111111111111111111111111111111111111111"
            + "11111111111111111111111111111111111111111111111111"
            + "11111111111111111111111111111111111111111111111111"
            + "11111111111111111111111111111111111111111111111111" + "2F78C7";
    BigInteger base = BigInteger.valueOf(2);
    BigInteger modulus = new BigInteger(s, 16);
    DHParameterSpec skipParameterSpec = new DHParameterSpec(modulus, base);

    KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH");
    kpg1.initialize(skipParameterSpec);/*from ww  w.  java 2  s . c o m*/
    KeyPair kp1 = kpg1.generateKeyPair();

    KeyAgreement ka1 = KeyAgreement.getInstance("DH");
    DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate();
    DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic();
    ka1.init(privateKey1);
    System.out.println("1 is using " + publicKey1.getY() + " for its public key");
    KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH");
    kpg2.initialize(skipParameterSpec);
    KeyPair kp2 = kpg2.generateKeyPair();

    KeyAgreement ka2 = KeyAgreement.getInstance("DH");
    DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate();
    DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic();
    ka2.init(privateKey2);
    System.out.println("2 is using " + publicKey2.getY() + " for its public key");
    ka1.doPhase(publicKey2, true);
    byte[] sharedKey1 = ka1.generateSecret();
    System.out.println("1 is using " + new BigInteger(1, sharedKey1) + " for its shared key");

    ka2.doPhase(publicKey1, true);
    byte[] sharedKey2 = ka2.generateSecret();
    System.out.println("2 is using " + new BigInteger(1, sharedKey2) + " for its shared key");
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    String s = "F488FD584E49DBCD20B49DE49107366B336C380D451D0F7C88"
            + "11111111111111111111111111111111111111111111111111"
            + "11111111111111111111111111111111111111111111111111"
            + "11111111111111111111111111111111111111111111111111"
            + "11111111111111111111111111111111111111111111111111" + "2F78C7";
    BigInteger base = BigInteger.valueOf(2);
    BigInteger modulous = new BigInteger(s, 16);
    DHParameterSpec skipParameterSpec = new DHParameterSpec(modulous, base);

    KeyPairGenerator kpg1 = KeyPairGenerator.getInstance("DH");
    kpg1.initialize(skipParameterSpec);//from w w  w.java 2  s .  c  o m
    KeyPair kp1 = kpg1.generateKeyPair();

    KeyAgreement ka1 = KeyAgreement.getInstance("DH");
    DHPrivateKey privateKey1 = (DHPrivateKey) kp1.getPrivate();
    DHPublicKey publicKey1 = (DHPublicKey) kp1.getPublic();
    ka1.init(privateKey1);
    System.out.println("1 is using " + publicKey1.getY() + " for its public key");

    KeyPairGenerator kpg2 = KeyPairGenerator.getInstance("DH");
    kpg2.initialize(skipParameterSpec);
    KeyPair kp2 = kpg2.generateKeyPair();

    KeyAgreement ka2 = KeyAgreement.getInstance("DH");
    DHPrivateKey privateKey2 = (DHPrivateKey) kp2.getPrivate();
    DHPublicKey publicKey2 = (DHPublicKey) kp2.getPublic();
    ka2.init(privateKey2);
    System.out.println("2 is using " + publicKey2.getY() + "for its public key");
    // Use the KeyAgreement object of 1 to generate its shared key
    ka1.doPhase(publicKey2, true);
    SecretKey sharedKey1 = ka1.generateSecret("DES");
    System.out.println("1 is using " + new String(sharedKey1.getEncoded()) + " as its DES session key");
    // Use the KeyAgreement object of 2 to generate its shared key
    ka2.doPhase(publicKey1, true);
    SecretKey sharedKey2 = ka2.generateSecret("DES");
    System.out.println("2 is using " + new String(sharedKey2.getEncoded()) + "as its DES session key");
}

From source file:edu.internet2.middleware.openid.message.encoding.EncodingUtils.java

/**
 * Encode a DH public key./*from  www  .  j a va2  s  .c  o  m*/
 * 
 * @param publicKey DH public key to encode
 * @return encoded public key
 */
public static String encodePublicKey(DHPublicKey publicKey) {
    return new String(Base64.encodeBase64(publicKey.getY().toByteArray()));
}

From source file:org.cesecore.keys.util.KeyTools.java

/**
 * Print parameters of public part of a key.
 * //from w ww. j  a va  2 s  .c  o  m
 * @param publK
 *            the key
 * @param ps
 *            stream to print to.
 */
public static void printPublicKeyInfo(final PublicKey publK, final PrintStream ps) {
    if (publK instanceof RSAPublicKey) {
        ps.println("RSA key:");
        final RSAPublicKey rsa = (RSAPublicKey) publK;
        ps.println("  modulus: " + rsa.getModulus().toString(16));
        ps.println("  public exponent: " + rsa.getPublicExponent().toString(16));
        return;
    }
    if (publK instanceof ECPublicKey) {
        ps.println("Elliptic curve key:");
        final ECPublicKey ec = (ECPublicKey) publK;
        ps.println("  the affine x-coordinate: " + ec.getW().getAffineX().toString(16));
        ps.println("  the affine y-coordinate: " + ec.getW().getAffineY().toString(16));
        return;
    }
    if (publK instanceof DHPublicKey) {
        ps.println("DH key:");
        final DHPublicKey dh = (DHPublicKey) publK;
        ps.println("  the public value y: " + dh.getY().toString(16));
        return;
    }
    if (publK instanceof DSAPublicKey) {
        ps.println("DSA key:");
        final DSAPublicKey dsa = (DSAPublicKey) publK;
        ps.println("  the public value y: " + dsa.getY().toString(16));
        return;
    }
}

From source file:org.red5.server.net.rtmp.RTMPHandshake.java

/**
 * Returns the public key for a given key pair.
 * // w w w . j ava2  s .c om
 * @param keyPair
 * @return public key
 */
protected static byte[] getPublicKey(KeyPair keyPair) {
    DHPublicKey incomingPublicKey = (DHPublicKey) keyPair.getPublic();
    BigInteger dhY = incomingPublicKey.getY();
    log.debug("Public key: {}", dhY);
    byte[] result = dhY.toByteArray();
    //log.debug("Public key as bytes - length [{}]: {}", result.length, Hex.encodeHexString(result));
    byte[] temp = new byte[KEY_LENGTH];
    if (result.length < KEY_LENGTH) {
        System.arraycopy(result, 0, temp, KEY_LENGTH - result.length, result.length);
        result = temp;
        log.debug("Padded public key length to 128");
    } else if (result.length > KEY_LENGTH) {
        System.arraycopy(result, result.length - KEY_LENGTH, temp, 0, KEY_LENGTH);
        result = temp;
        log.debug("Truncated public key length to 128");
    }
    return result;
}