Search in sources :

Example 26 with DHPublicKey

use of javax.crypto.interfaces.DHPublicKey in project Bytecoder by mirkosertic.

the class DHCrypt method checkConstraints.

// Check constraints of the specified DH public key.
void checkConstraints(AlgorithmConstraints constraints, BigInteger peerPublicValue) throws SSLHandshakeException {
    try {
        KeyFactory kf = JsseJce.getKeyFactory("DiffieHellman");
        DHPublicKeySpec spec = new DHPublicKeySpec(peerPublicValue, modulus, base);
        DHPublicKey publicKey = (DHPublicKey) kf.generatePublic(spec);
        // check constraints of DHPublicKey
        if (!constraints.permits(EnumSet.of(CryptoPrimitive.KEY_AGREEMENT), publicKey)) {
            throw new SSLHandshakeException("DHPublicKey does not comply to algorithm constraints");
        }
    } catch (GeneralSecurityException gse) {
        throw (SSLHandshakeException) new SSLHandshakeException("Could not generate DHPublicKey").initCause(gse);
    }
}
Also used : DHPublicKey(javax.crypto.interfaces.DHPublicKey) SSLHandshakeException(javax.net.ssl.SSLHandshakeException)

Example 27 with DHPublicKey

use of javax.crypto.interfaces.DHPublicKey in project protools by SeanDragon.

the class ToolDH method initKey.

/**
 * 初始化甲方密钥
 *
 * @return Map 甲方密钥Map
 *
 * @throws Exception
 */
public static Map<String, Object> initKey() throws NoSuchAlgorithmException {
    // 实例化密钥对生成器
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    // 初始化密钥对生成器
    keyPairGenerator.initialize(KEY_SIZE);
    // 生成密钥对
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    // 甲方公钥
    DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();
    // 甲方私钥
    DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();
    // 将密钥对存储在Map中
    Map<String, Object> keyMap = Maps.newHashMapWithExpectedSize(2);
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) KeyPair(java.security.KeyPair) DHPublicKey(javax.crypto.interfaces.DHPublicKey) KeyPairGenerator(java.security.KeyPairGenerator)

Example 28 with DHPublicKey

use of javax.crypto.interfaces.DHPublicKey in project Gradle-demo by Arisono.

the class DHUtil method initKey.

/**
 * 乙方根据甲方公钥初始化并返回密钥对
 */
public static Map<String, Object> initKey(byte[] key) throws Exception {
    // 将甲方公钥从字节数组转换为publicKey
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key);
    // 实例化密钥工厂
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    // 产生甲方公钥pubKey
    DHPublicKey dhPublicKey = (DHPublicKey) keyFactory.generatePublic(keySpec);
    // 剖析甲方公钥,得到其参数
    DHParameterSpec dhParameterSpec = dhPublicKey.getParams();
    // 实例化密钥对生成器
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
    // 用甲方公钥初始化密钥对生成器
    keyPairGenerator.initialize(dhParameterSpec);
    // 产生密钥对
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    // 得到乙方公钥
    DHPublicKey publicKey = (DHPublicKey) keyPair.getPublic();
    // 得到乙方私钥
    DHPrivateKey privateKey = (DHPrivateKey) keyPair.getPrivate();
    // 将公钥和私钥封装到Map中,方便以后使用
    Map<String, Object> keyMap = new HashMap<String, Object>();
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) KeyPair(java.security.KeyPair) DHPublicKey(javax.crypto.interfaces.DHPublicKey) HashMap(java.util.HashMap) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Aggregations

DHPublicKey (javax.crypto.interfaces.DHPublicKey)28 KeyPair (java.security.KeyPair)16 IOException (java.io.IOException)14 BigInteger (java.math.BigInteger)11 KeyPairGenerator (java.security.KeyPairGenerator)11 KeyFactory (java.security.KeyFactory)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 PublicKey (java.security.PublicKey)9 DHParameterSpec (javax.crypto.spec.DHParameterSpec)8 DHPublicKeySpec (javax.crypto.spec.DHPublicKeySpec)8 CertificateException (java.security.cert.CertificateException)6 DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)6 Cipher (javax.crypto.Cipher)5 KeyAgreement (javax.crypto.KeyAgreement)5 OtrCryptoEngineImpl (net.java.otr4j.crypto.OtrCryptoEngineImpl)5 X509Certificate (java.security.cert.X509Certificate)4 RSAPublicKey (java.security.interfaces.RSAPublicKey)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 X509ExtendedKeyManager (javax.net.ssl.X509ExtendedKeyManager)4 X509KeyManager (javax.net.ssl.X509KeyManager)4