use of javax.crypto.spec.DHParameterSpec in project robovm by robovm.
the class KeyAgreementThread method test.
@Override
public void test() throws Exception {
AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH");
apg.init(1024, new SecureRandom());
AlgorithmParameters ap = apg.generateParameters();
DHParameterSpec ps = ap.getParameterSpec(DHParameterSpec.class);
KeyAgreementGen kag1 = new KeyAgreementGen(ps);
KeyAgreementGen kag2 = new KeyAgreementGen(ps);
byte[] bArray1 = kag1.getPublicKeyBytes();
byte[] bArray2 = kag2.getPublicKeyBytes();
byte[] sk1 = kag1.getSecretKey(algName, bArray2);
byte[] sk2 = kag2.getSecretKey(algName, bArray1);
if (Arrays.areEqual(sk1, sk2) == false) {
throw new Exception("Generated keys are not the same");
}
}
use of javax.crypto.spec.DHParameterSpec 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.spec.DHParameterSpec 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.spec.DHParameterSpec 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.spec.DHParameterSpec in project robovm by robovm.
the class KeyPairGeneratorTest method getDHParams.
/**
* DH parameters pre-generated so that the test doesn't take too long.
* These parameters were generated with:
*
* openssl gendh 512 | openssl dhparams -C
*/
private static AlgorithmParameterSpec getDHParams() {
BigInteger p = new BigInteger("E7AB1768BD75CD24700960FFA32D3F1557344E587101237532CC641646ED7A7C104743377F6D46251698B665CE2A6CBAB6714C2569A7D2CA22C0CF03FA40AC93", 16);
BigInteger g = new BigInteger("02", 16);
return new DHParameterSpec(p, g, 512);
}
Aggregations