use of javax.crypto.KeyGenerator in project storm by nathanmarz.
the class BlowfishTupleSerializer method main.
/**
* Produce a blowfish key to be used in "Storm jar" command
*/
public static void main(String[] args) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
String keyString = new String(Hex.encodeHex(raw));
System.out.println("storm -c " + SECRET_KEY + "=" + keyString + " -c " + Config.TOPOLOGY_TUPLE_SERIALIZER + "=" + BlowfishTupleSerializer.class.getName() + " ...");
} catch (Exception ex) {
LOG.error(ex.getMessage());
ex.printStackTrace();
}
}
use of javax.crypto.KeyGenerator in project robovm by robovm.
the class AlgorithmParameterSymmetricHelper method test.
@Override
public void test(AlgorithmParameters parameters) {
KeyGenerator generator = null;
try {
generator = KeyGenerator.getInstance(algorithmName);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
}
generator.init(keySize);
Key key = generator.generateKey();
Cipher cipher = null;
try {
String transformation = algorithmName;
if (blockmode != null) {
transformation += "/" + blockmode;
}
cipher = Cipher.getInstance(transformation);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
} catch (NoSuchPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.ENCRYPT_MODE, key, parameters);
} catch (InvalidKeyException e) {
Assert.fail(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
Assert.fail(e.getMessage());
}
byte[] bs = null;
try {
bs = cipher.doFinal(plainData.getBytes());
} catch (IllegalBlockSizeException e) {
Assert.fail(e.getMessage());
} catch (BadPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.DECRYPT_MODE, key, parameters);
} catch (InvalidKeyException e) {
Assert.fail(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
Assert.fail(e.getMessage());
}
byte[] decrypted = null;
try {
decrypted = cipher.doFinal(bs);
} catch (IllegalBlockSizeException e) {
Assert.fail(e.getMessage());
} catch (BadPaddingException e) {
Assert.fail(e.getMessage());
}
Assert.assertTrue(Arrays.equals(plainData.getBytes(), decrypted));
}
use of javax.crypto.KeyGenerator in project robovm by robovm.
the class ExemptionMechanismTest method test_initLjava_security_KeyLjava_security_AlgorithmParameters.
public void test_initLjava_security_KeyLjava_security_AlgorithmParameters() throws Exception {
Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
ExemptionMechanism em = new ExemptionMechanism(new MyExemptionMechanismSpi(), mProv, defaultAlg) {
};
Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]);
em.init(key, AlgorithmParameters.getInstance("DES"));
try {
em.init(key, (AlgorithmParameters) null);
fail("InvalidAlgorithmParameterException expected");
} catch (InvalidAlgorithmParameterException e) {
//expected
}
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56, new SecureRandom());
key = kg.generateKey();
try {
em.init(null, AlgorithmParameters.getInstance("DES"));
fail("InvalidKeyException expected");
} catch (InvalidKeyException e) {
//expected
}
try {
em.init(key, AlgorithmParameters.getInstance("DES"));
fail("ExemptionMechanismException expected");
} catch (ExemptionMechanismException e) {
//expected
}
}
use of javax.crypto.KeyGenerator in project robovm by robovm.
the class ExemptionMechanismTest method test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec.
public void test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec() throws Exception {
Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
ExemptionMechanism em = new ExemptionMechanism(new MyExemptionMechanismSpi(), mProv, defaultAlg) {
};
Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]);
em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
try {
em.init(key, (AlgorithmParameterSpec) null);
fail("InvalidAlgorithmParameterException expected");
} catch (InvalidAlgorithmParameterException e) {
//expected
}
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56, new SecureRandom());
key = kg.generateKey();
try {
em.init(null, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
fail("InvalidKeyException expected");
} catch (InvalidKeyException e) {
//expected
}
try {
em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
fail("ExemptionMechanismException expected");
} catch (ExemptionMechanismException e) {
//expected
}
}
use of javax.crypto.KeyGenerator in project robovm by robovm.
the class myKeyGenerator method testGetInstanceString02.
/*
* Test for <code> getInstance(String algorithm) </code> method
* Assertions: returns KeyGenerator object
*/
public void testGetInstanceString02() throws NoSuchAlgorithmException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
KeyGenerator keyG;
for (int i = 0; i < validValues.length; i++) {
keyG = KeyGenerator.getInstance(validValues[i]);
assertEquals("Incorrect algorithm", keyG.getAlgorithm(), validValues[i]);
}
}
Aggregations