Search in sources :

Example 11 with KeyGenerator

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();
    }
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 12 with KeyGenerator

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));
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 13 with KeyGenerator

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
    }
}
Also used : SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) MyExemptionMechanismSpi(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi) KeyGenerator(javax.crypto.KeyGenerator) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) ExemptionMechanism(javax.crypto.ExemptionMechanism) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Key(java.security.Key) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Provider(java.security.Provider)

Example 14 with KeyGenerator

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
    }
}
Also used : SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) MyExemptionMechanismSpi(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi) ExemptionMechanism(javax.crypto.ExemptionMechanism) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Provider(java.security.Provider) BigInteger(java.math.BigInteger) KeyGenerator(javax.crypto.KeyGenerator) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Key(java.security.Key)

Example 15 with KeyGenerator

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]);
    }
}
Also used : KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

KeyGenerator (javax.crypto.KeyGenerator)166 SecretKey (javax.crypto.SecretKey)117 SecureRandom (java.security.SecureRandom)53 Cipher (javax.crypto.Cipher)43 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)32 Key (java.security.Key)31 Test (org.junit.Test)25 InvalidKeyException (java.security.InvalidKeyException)19 IvParameterSpec (javax.crypto.spec.IvParameterSpec)19 IOException (java.io.IOException)18 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 BadPaddingException (javax.crypto.BadPaddingException)13 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)13 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)13 File (java.io.File)12 FileOutputStream (java.io.FileOutputStream)12 RouteBuilder (org.apache.camel.builder.RouteBuilder)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 Provider (java.security.Provider)11 SecretKeySpec (javax.crypto.spec.SecretKeySpec)10