Example usage for javax.crypto.spec DHPublicKeySpec DHPublicKeySpec

List of usage examples for javax.crypto.spec DHPublicKeySpec DHPublicKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec DHPublicKeySpec DHPublicKeySpec.

Prototype

public DHPublicKeySpec(BigInteger y, BigInteger p, BigInteger g) 

Source Link

Document

Constructor that takes a public value y, a prime modulus p, and a base generator g.

Usage

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

/**
 * Decode a DH public key.//from www  . j  a v a 2s.c o  m
 * 
 * @param encodedKey public key to decode
 * @param parameters DH parameters used in decoding
 * @return decoded public key
 * @throws NoSuchAlgorithmException if DH algorithm is unavailable
 * @throws InvalidKeySpecException if unable to build a valid DH key spec
 */
public static DHPublicKey decodePublicKey(String encodedKey, DHParameterSpec parameters)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    byte[] keyBytes = Base64.decodeBase64(encodedKey.getBytes());
    DHPublicKeySpec keySpec = new DHPublicKeySpec(new BigInteger(keyBytes), parameters.getP(),
            parameters.getG());
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    return (DHPublicKey) keyFactory.generatePublic(keySpec);
}

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

/**
 * Determines the validation scheme for given input.
 * //w  w w .j  a v  a  2 s . c o m
 * @param otherPublicKeyBytes
 * @param agreement
 * @return shared secret bytes if client used a supported validation scheme
 */
protected static byte[] getSharedSecret(byte[] otherPublicKeyBytes, KeyAgreement agreement) {
    BigInteger otherPublicKeyInt = new BigInteger(1, otherPublicKeyBytes);
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("DH");
        KeySpec otherPublicKeySpec = new DHPublicKeySpec(otherPublicKeyInt, RTMPHandshake.DH_MODULUS,
                RTMPHandshake.DH_BASE);
        PublicKey otherPublicKey = keyFactory.generatePublic(otherPublicKeySpec);
        agreement.doPhase(otherPublicKey, true);
    } catch (Exception e) {
        log.error("Exception getting the shared secret", e);
    }
    byte[] sharedSecret = agreement.generateSecret();
    //log.debug("Shared secret [{}]: {}", sharedSecret.length, Hex.encodeHexString(sharedSecret));
    return sharedSecret;
}