use of javax.crypto.KeyAgreement in project robovm by robovm.
the class KeyAgreementTest method test_getAlgorithm.
public void test_getAlgorithm() throws NoSuchAlgorithmException {
Mock_KeyAgreement mka = new Mock_KeyAgreement(null, null, null);
assertNull(mka.getAlgorithm());
KeyAgreement keyA;
for (int i = 0; i < validValues.length; i++) {
keyA = KeyAgreement.getInstance(validValues[i]);
assertEquals("Incorrect algorithm", keyA.getAlgorithm(), validValues[i]);
}
}
use of javax.crypto.KeyAgreement in project robovm by robovm.
the class KeyAgreementTest method testInit01.
/**
* Test for the methods <code>init(Key key)</code>
* <code>init(Key key, SecureRandom random)</code>
* <code>init(Key key, AlgorithmParameterSpec params)</code>
* <code>init(Key key, AlgorithmParameterSpec params, SecureRandom random)</code>
* Assertion: throws InvalidKeyException when key is inappropriate
*/
public void testInit01() throws Exception {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
createKeys();
KeyAgreement[] kAgs = createKAs();
SecureRandom random = null;
AlgorithmParameterSpec aps = null;
DHParameterSpec dhPs = new DHParameterSpec(new BigInteger("56"), new BigInteger("56"));
for (int i = 0; i < kAgs.length; i++) {
try {
kAgs[i].init(publKey);
fail("InvalidKeyException must be throw");
} catch (InvalidKeyException e) {
}
try {
kAgs[i].init(publKey, new SecureRandom());
fail("InvalidKeyException must be throw");
} catch (InvalidKeyException e) {
}
try {
kAgs[i].init(publKey, random);
fail("InvalidKeyException must be throw");
} catch (InvalidKeyException e) {
}
try {
kAgs[i].init(publKey, dhPs);
fail("InvalidKeyException must be throw");
} catch (InvalidKeyException e) {
}
try {
kAgs[i].init(publKey, aps);
fail("InvalidKeyException must be throw");
} catch (InvalidKeyException e) {
}
try {
kAgs[i].init(publKey, dhPs, new SecureRandom());
fail("InvalidKeyException must be throw");
} catch (InvalidKeyException e) {
}
}
}
use of javax.crypto.KeyAgreement 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.KeyAgreement in project Gradle-demo by Arisono.
the class DHUtil method getSecretKey.
/**
* 根据对方的公钥和自己的私钥生成本地密钥
*/
public static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception {
//实例化密钥工厂
KeyFactory keyFactory = KeyFactory.getInstance("DH");
//将公钥从字节数组转换为publicKey
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey);
PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
//将私钥从字节数组转换为privateKey
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(privateKey);
PrivateKey priKey = keyFactory.generatePrivate(priKeySpec);
//准备根据以上公钥和私钥生成本地密钥SecretKey
//先实例化KeyAgreement
KeyAgreement keyAgreement = KeyAgreement.getInstance("DH");
//用自己的私钥初始化keyAgreement
keyAgreement.init(priKey);
//结合对方的公钥进行运算
keyAgreement.doPhase(pubKey, true);
//开始生成本地密钥secretKey 密钥算法为对称密码算法
//DES、3DES、AES
SecretKey secretKey = keyAgreement.generateSecret("DES");
return secretKey.getEncoded();
}
use of javax.crypto.KeyAgreement in project jdk8u_jdk by JetBrains.
the class ECDHCrypt method getAgreedSecret.
// called by ClientHandshaker with either the server's static or
// ephemeral public key
SecretKey getAgreedSecret(PublicKey peerPublicKey) throws SSLHandshakeException {
try {
KeyAgreement ka = JsseJce.getKeyAgreement("ECDH");
ka.init(privateKey);
ka.doPhase(peerPublicKey, true);
return ka.generateSecret("TlsPremasterSecret");
} catch (GeneralSecurityException e) {
throw (SSLHandshakeException) new SSLHandshakeException("Could not generate secret").initCause(e);
}
}
Aggregations