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);
}
}
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()));
}
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;
}
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;
}
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);
}
Aggregations