Search in sources :

Example 11 with DHPrivateKey

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

the class KeyAgreementTest method testDoPhase.

/**
     * Test for <code>doPhase(Key key, boolean lastPhase)</code> method
     * Assertion: throws InvalidKeyException if key is not appropriate
     */
public void testDoPhase() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    DHParameterSpec dhPs = ((DHPrivateKey) privKey).getParams();
    SecureRandom randomNull = null;
    SecureRandom random = new SecureRandom();
    for (int i = 0; i < kAgs.length; i++) {
        try {
            kAgs[i].doPhase(publKey, true);
            fail("IllegalStateException expected");
        } catch (IllegalStateException e) {
        //expected
        }
        kAgs[i].init(privKey);
        try {
            kAgs[i].doPhase(privKey, false);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        try {
            kAgs[i].doPhase(privKey, true);
            fail("InvalidKeyException must be throw");
        } catch (InvalidKeyException e) {
        }
        kAgs[i].init(privKey, dhPs);
        kAgs[i].doPhase(publKey, true);
        kAgs[i].init(privKey, dhPs, random);
        kAgs[i].doPhase(publKey, true);
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) SecureRandom(java.security.SecureRandom) DHParameterSpec(javax.crypto.spec.DHParameterSpec) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException)

Example 12 with DHPrivateKey

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

the class DHPrivateKeyTest method test_getParams.

public void test_getParams() throws Exception {
    KeyPairGenerator kg = KeyPairGenerator.getInstance("DH");
    kg.initialize(192);
    KeyPair kp1 = kg.genKeyPair();
    KeyPair kp2 = kg.genKeyPair();
    DHPrivateKey pk1 = (DHPrivateKey) kp1.getPrivate();
    DHPrivateKey pk2 = (DHPrivateKey) kp2.getPrivate();
    assertTrue(pk1.getX().getClass().getCanonicalName().equals("java.math.BigInteger"));
    assertTrue(pk1.getParams().getClass().getCanonicalName().equals("javax.crypto.spec.DHParameterSpec"));
    assertFalse(pk1.equals(pk2));
    assertTrue(pk1.getX().equals(pk1.getX()));
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) KeyPair(java.security.KeyPair) KeyPairGenerator(java.security.KeyPairGenerator)

Example 13 with DHPrivateKey

use of javax.crypto.interfaces.DHPrivateKey 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 14 with DHPrivateKey

use of javax.crypto.interfaces.DHPrivateKey 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)

Example 15 with DHPrivateKey

use of javax.crypto.interfaces.DHPrivateKey in project wycheproof by google.

the class DhTest method testDefaultKeyPairGenerator.

/**
 * Tests the default Diffie-Hellman key pair generation.
 *
 * <p>This test uses NIST SP 800-57 part1, revision 4
 * http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-57pt1r4.pdf . Table 2 on page
 * 53 recommends 2048 bits as the minimal key length for Diffie-Hellman for new keys that expire
 * before the year 2030.
 *
 * <p>Note that JCE documentation is outdated. According to
 * https://docs.oracle.com/javase/7/docs/api/java/security/KeyPairGenerator.html an implementation
 * of the Java platform is only required to support 1024 bit keys.
 */
@NoPresubmitTest(providers = { ProviderType.OPENJDK, ProviderType.BOUNCY_CASTLE }, bugs = { "b/33190860", "b/33190677" })
@SlowTest(providers = { ProviderType.BOUNCY_CASTLE, ProviderType.SPONGY_CASTLE })
@Test
public void testDefaultKeyPairGenerator() throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
    KeyPair keyPair;
    try {
        keyPair = keyGen.generateKeyPair();
    } catch (Exception ex) {
        // When a provider decides not to implement a default key size then this is still better than
        // implementing a default that is out of date. Hence the test should not fail in this case.
        System.out.println("Cannot generate a DH key without initialize: " + ex.getMessage());
        return;
    }
    DHPrivateKey priv = (DHPrivateKey) keyPair.getPrivate();
    int keySize = priv.getParams().getP().bitLength();
    assertTrue("Default key size for DH is too small. Key size = " + keySize, keySize >= 2048);
    testKeyPair(keyPair, keySize);
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) KeyPair(java.security.KeyPair) KeyPairGenerator(java.security.KeyPairGenerator) GeneralSecurityException(java.security.GeneralSecurityException) SlowTest(com.google.security.wycheproof.WycheproofRunner.SlowTest) SlowTest(com.google.security.wycheproof.WycheproofRunner.SlowTest) Test(org.junit.Test) NoPresubmitTest(com.google.security.wycheproof.WycheproofRunner.NoPresubmitTest) NoPresubmitTest(com.google.security.wycheproof.WycheproofRunner.NoPresubmitTest)

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