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 robovm by robovm.
the class ECDHKeyAgreementTest method testGenerateSecret_withoutBuffer.
void testGenerateSecret_withoutBuffer(Provider provider) throws Exception {
KeyAgreement keyAgreement = getKeyAgreement(provider);
keyAgreement.init(KAT_PRIVATE_KEY2);
keyAgreement.doPhase(KAT_PUBLIC_KEY1, true);
byte[] secret = keyAgreement.generateSecret();
assertTrue(Arrays.equals(KAT_SECRET, secret));
}
use of javax.crypto.KeyAgreement in project robovm by robovm.
the class ECDHKeyAgreementTest method testGenerateSecret_withNullOutputBuffer.
void testGenerateSecret_withNullOutputBuffer(Provider provider) throws Exception {
KeyAgreement keyAgreement = getKeyAgreement(provider);
keyAgreement.init(KAT_PRIVATE_KEY1);
keyAgreement.doPhase(KAT_PUBLIC_KEY2, true);
try {
keyAgreement.generateSecret(null, 0);
fail();
} catch (NullPointerException expected) {
}
}
use of javax.crypto.KeyAgreement in project robovm by robovm.
the class ECDHKeyAgreementTest method testDoPhase_withPhaseWhichIsNotLast.
void testDoPhase_withPhaseWhichIsNotLast(Provider provider) throws Exception {
KeyAgreement keyAgreement = getKeyAgreement(provider);
keyAgreement.init(KAT_PRIVATE_KEY1);
try {
keyAgreement.doPhase(KAT_PUBLIC_KEY2, false);
fail();
} catch (IllegalStateException expected) {
}
}
Aggregations