use of java.security.AlgorithmParameterGenerator in project jdk8u_jdk by JetBrains.
the class KAParticipant method runTest.
public static boolean runTest(String algo, int numParties, String secretAlgo) {
KAParticipant[] parties = new KAParticipant[numParties];
Key[] keyArchives = new Key[numParties];
try {
// generate AlogirhtmParameterSpec
AlgorithmParameterGenerator apg = AlgorithmParameterGenerator.getInstance("DH", "SunJCE");
AlgorithmParameterSpec aps = new DHGenParameterSpec(512, 64);
apg.init(aps);
DHParameterSpec spec = apg.generateParameters().getParameterSpec(DHParameterSpec.class);
//initilize all KeyAgreement participants
for (int i = 0; i < numParties; i++) {
parties[i] = new KAParticipant(PA_NAMES[i], algo);
parties[i].initialize(spec);
keyArchives[i] = parties[i].getPublicKey();
}
// Do all phases in the KeyAgreement for all participants
Key[] keyBuffer = new Key[numParties];
boolean lastPhase = false;
for (int j = 0; j < numParties - 1; j++) {
if (j == numParties - 2) {
lastPhase = true;
}
for (int k = 0; k < numParties; k++) {
if (k == numParties - 1) {
keyBuffer[k] = parties[k].doPhase(keyArchives[0], lastPhase);
} else {
keyBuffer[k] = parties[k].doPhase(keyArchives[k + 1], lastPhase);
}
}
System.arraycopy(keyBuffer, 0, keyArchives, 0, numParties);
}
//Comparison: The secret keys generated by all involved parties should be the same
SecretKey[] sKeys = new SecretKey[numParties];
for (int n = 0; n < numParties; n++) {
sKeys[n] = parties[n].generateSecret(secretAlgo);
}
for (int q = 0; q < numParties - 1; q++) {
if (!Arrays.equals(sKeys[q].getEncoded(), sKeys[q + 1].getEncoded())) {
return false;
}
}
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
use of java.security.AlgorithmParameterGenerator 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 java.security.AlgorithmParameterGenerator in project robovm by robovm.
the class myAlgPG method testAlgorithmParameterGenerator02.
/**
* Test for <code>getInstance(String algorithm)</code> method
* Assertion: returns AlgorithmParameterGenerator instance
* when algorithm is DSA
*/
public void testAlgorithmParameterGenerator02() throws NoSuchAlgorithmException {
if (!DSASupported) {
fail(validAlgName + " algorithm is not supported");
return;
}
AlgorithmParameterGenerator apg;
for (int i = 0; i < algs.length; i++) {
apg = AlgorithmParameterGenerator.getInstance(algs[i]);
assertEquals("Incorrect algorithm", apg.getAlgorithm(), algs[i]);
}
}
use of java.security.AlgorithmParameterGenerator in project robovm by robovm.
the class AlgorithmParameterGenerator2Test method testGetInstance01.
/**
* Test for <code>getInstance(String algorithm)</code> method
* Assertions:
* throws NullPointerException must be thrown is null
* throws NoSuchAlgorithmException must be thrown if algorithm is not available
* returns AlgorithmParameterGenerator object
*/
public void testGetInstance01() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
try {
AlgorithmParameterGenerator.getInstance(null);
fail("NullPointerException or NoSuchAlgorithmException should be thrown");
} catch (NullPointerException e) {
} catch (NoSuchAlgorithmException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
AlgorithmParameterGenerator.getInstance(invalidValues[i]);
fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
} catch (NoSuchAlgorithmException e) {
}
}
AlgorithmParameterGenerator apG;
for (int i = 0; i < validValues.length; i++) {
apG = AlgorithmParameterGenerator.getInstance(validValues[i]);
assertEquals("Incorrect algorithm", apG.getAlgorithm(), validValues[i]);
assertEquals("Incorrect provider", apG.getProvider(), mProv);
checkResult(apG);
}
}
use of java.security.AlgorithmParameterGenerator in project robovm by robovm.
the class OldAlgorithmParameterGeneratorTest method test_initI.
public void test_initI() throws Exception {
// Test for method void
// java.security.AlgorithmParameterGenerator.init(int)
// checks that no exception is thrown
int[] valid = { 512, 576, 640, 960, 1024 };
AlgorithmParameterGenerator gen = AlgorithmParameterGenerator.getInstance("DSA");
for (int i = 0; i < valid.length; i++) {
try {
gen.init(valid[i]);
} catch (Exception e) {
fail("Exception should not be thrown for valid parameter" + valid[i]);
}
}
}
Aggregations