Search in sources :

Example 1 with DHPrivateKey

use of javax.crypto.interfaces.DHPrivateKey in project XobotOS by xamarin.

the class JCEDHKeyAgreement method engineInit.

protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException {
    if (!(key instanceof DHPrivateKey)) {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
    }
    DHPrivateKey privKey = (DHPrivateKey) key;
    this.p = privKey.getParams().getP();
    this.g = privKey.getParams().getG();
    this.x = this.result = privKey.getX();
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with DHPrivateKey

use of javax.crypto.interfaces.DHPrivateKey in project XobotOS by xamarin.

the class JCEDHKeyAgreement method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (!(key instanceof DHPrivateKey)) {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
    }
    DHPrivateKey privKey = (DHPrivateKey) key;
    if (params != null) {
        if (!(params instanceof DHParameterSpec)) {
            throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
        }
        DHParameterSpec p = (DHParameterSpec) params;
        this.p = p.getP();
        this.g = p.getG();
    } else {
        this.p = privKey.getParams().getP();
        this.g = privKey.getParams().getG();
    }
    this.x = this.result = privKey.getX();
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DHParameterSpec(javax.crypto.spec.DHParameterSpec) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with DHPrivateKey

use of javax.crypto.interfaces.DHPrivateKey in project robovm by robovm.

the class KeyAgreementSpi method engineInit.

protected void engineInit(Key key, SecureRandom random) throws InvalidKeyException {
    if (!(key instanceof DHPrivateKey)) {
        throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey");
    }
    DHPrivateKey privKey = (DHPrivateKey) key;
    this.p = privKey.getParams().getP();
    this.g = privKey.getParams().getG();
    this.x = this.result = privKey.getX();
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidKeyException(java.security.InvalidKeyException)

Example 4 with DHPrivateKey

use of javax.crypto.interfaces.DHPrivateKey in project robovm by robovm.

the class KeyAgreementTest method testInit04.

/**
     * Test for the methods:
     * <code>init(Key key, AlgorithmParameterSpec params)</code>
     * <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
     * <code>generateSecret()</code>
     * Assertions: initializes KeyAgreement and returns byte array
     */
public void testInit04() throws Exception, InvalidAlgorithmParameterException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
    AlgorithmParameterSpec aps = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
    byte[] bbRes1;
    byte[] bbRes2;
    byte[] bbRes3;
    SecureRandom randomNull = null;
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < kAgs.length; i++) {
        kAgs[i].init(privKey, dhPs);
        kAgs[i].doPhase(publKey, true);
        bbRes1 = kAgs[i].generateSecret();
        kAgs[i].init(privKey, dhPs, random);
        kAgs[i].doPhase(publKey, true);
        bbRes2 = kAgs[i].generateSecret();
        assertEquals("Incorrect byte array length", bbRes1.length, bbRes2.length);
        for (int j = 0; j < bbRes1.length; j++) {
            assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes2[j]);
        }
        kAgs[i].init(privKey, dhPs, randomNull);
        kAgs[i].doPhase(publKey, true);
        bbRes3 = kAgs[i].generateSecret();
        assertEquals("Incorrect byte array length", bbRes1.length, bbRes3.length);
        for (int j = 0; j < bbRes1.length; j++) {
            assertEquals("Incorrect byte (index: ".concat(Integer.toString(i)).concat(")"), bbRes1[j], bbRes3[j]);
        }
        try {
            kAgs[i].init(publKey, dhPs, random);
            fail("InvalidKeyException expected");
        } catch (InvalidKeyException e) {
        //expected
        }
        try {
            kAgs[i].init(privKey, aps, random);
            fail("InvalidAlgorithmParameterException expected");
        } catch (InvalidAlgorithmParameterException e) {
        //expected
        }
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 5 with DHPrivateKey

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

the class ToolDH method initKey.

/**
 * 初始化乙方密钥
 *
 * @param key
 *         甲方公钥
 *
 * @return Map 乙方密钥Map
 *
 * @throws Exception
 */
public static Map<String, Object> initKey(byte[] key) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException {
    // 解析甲方公钥
    // 转换公钥材料
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
    // 实例化密钥工厂
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    // 产生公钥
    PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
    // 由甲方公钥构建乙方密钥
    DHParameterSpec dhParamSpec = ((DHPublicKey) pubKey).getParams();
    // 实例化密钥对生成器
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(keyFactory.getAlgorithm());
    // 初始化密钥对生成器
    keyPairGenerator.initialize(dhParamSpec);
    // 产生密钥对
    KeyPair keyPair = keyPairGenerator.genKeyPair();
    // 乙方公钥
    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) PublicKey(java.security.PublicKey) DHPublicKey(javax.crypto.interfaces.DHPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Aggregations

DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)15 InvalidKeyException (java.security.InvalidKeyException)8 KeyPair (java.security.KeyPair)8 KeyPairGenerator (java.security.KeyPairGenerator)6 DHPublicKey (javax.crypto.interfaces.DHPublicKey)6 DHParameterSpec (javax.crypto.spec.DHParameterSpec)6 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)4 BigInteger (java.math.BigInteger)3 KeyFactory (java.security.KeyFactory)3 SecureRandom (java.security.SecureRandom)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 HashMap (java.util.HashMap)2 KeyAgreement (javax.crypto.KeyAgreement)2 NoPresubmitTest (com.google.security.wycheproof.WycheproofRunner.NoPresubmitTest)1 SlowTest (com.google.security.wycheproof.WycheproofRunner.SlowTest)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 PublicKey (java.security.PublicKey)1 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)1