Search in sources :

Example 56 with SecureRandom

use of java.security.SecureRandom in project camel by apache.

the class PGPDataFormatTest method createEncryptedNonCompressedData.

void createEncryptedNonCompressedData(ByteArrayOutputStream bos, String keyringPath) throws Exception, IOException, PGPException, UnsupportedEncodingException {
    PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.CAST5).setSecureRandom(new SecureRandom()).setProvider(getProvider()));
    encGen.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(readPublicKey(keyringPath)));
    OutputStream encOut = encGen.open(bos, new byte[512]);
    PGPLiteralDataGenerator litData = new PGPLiteralDataGenerator();
    OutputStream litOut = litData.open(encOut, PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[512]);
    try {
        litOut.write("Test Message Without Compression".getBytes("UTF-8"));
        litOut.flush();
    } finally {
        IOHelper.close(litOut);
        IOHelper.close(encOut, bos);
    }
}
Also used : BCPGOutputStream(org.bouncycastle.bcpg.BCPGOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) SecureRandom(java.security.SecureRandom) PGPLiteralDataGenerator(org.bouncycastle.openpgp.PGPLiteralDataGenerator) PGPEncryptedDataGenerator(org.bouncycastle.openpgp.PGPEncryptedDataGenerator) Date(java.util.Date) JcePGPDataEncryptorBuilder(org.bouncycastle.openpgp.operator.jcajce.JcePGPDataEncryptorBuilder) JcePublicKeyKeyEncryptionMethodGenerator(org.bouncycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator)

Example 57 with SecureRandom

use of java.security.SecureRandom in project camel by apache.

the class CryptoDataFormatTest method createRouteBuilders.

protected RouteBuilder[] createRouteBuilders() throws Exception {
    return new RouteBuilder[] { new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: basic
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey());
            from("direct:basic-encryption").marshal(cryptoFormat).to("mock:encrypted").unmarshal(cryptoFormat).to("mock:unencrypted");
        // END SNIPPET: basic
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: init-vector
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            byte[] initializationVector = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES/CBC/PKCS5Padding", generator.generateKey());
            cryptoFormat.setInitializationVector(initializationVector);
            from("direct:init-vector").marshal(cryptoFormat).to("mock:encrypted").unmarshal(cryptoFormat).to("mock:unencrypted");
        // END SNIPPET: init-vector
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: inline-init-vector
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            byte[] initializationVector = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
            SecretKey key = generator.generateKey();
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES/CBC/PKCS5Padding", key);
            cryptoFormat.setInitializationVector(initializationVector);
            cryptoFormat.setShouldInlineInitializationVector(true);
            CryptoDataFormat decryptFormat = new CryptoDataFormat("DES/CBC/PKCS5Padding", key);
            decryptFormat.setShouldInlineInitializationVector(true);
            from("direct:inline").marshal(cryptoFormat).to("mock:encrypted").unmarshal(decryptFormat).to("mock:unencrypted");
        // END SNIPPET: inline-init-vector
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: hmac
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey());
            cryptoFormat.setShouldAppendHMAC(true);
            from("direct:hmac").marshal(cryptoFormat).to("mock:encrypted").unmarshal(cryptoFormat).to("mock:unencrypted");
        // END SNIPPET: hmac
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: hmac-algorithm
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey());
            cryptoFormat.setShouldAppendHMAC(true);
            cryptoFormat.setMacAlgorithm("HmacMD5");
            from("direct:hmac-algorithm").marshal(cryptoFormat).to("mock:encrypted").unmarshal(cryptoFormat).to("mock:unencrypted");
        // END SNIPPET: hmac-algorithm
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: hmac-sha256-algorithm
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", generator.generateKey());
            cryptoFormat.setShouldAppendHMAC(true);
            cryptoFormat.setMacAlgorithm("HmacSHA256");
            from("direct:hmac-sha-256-algorithm").marshal(cryptoFormat).to("mock:encrypted").unmarshal(cryptoFormat).to("mock:unencrypted");
        // END SNIPPET: hmac-sha256-algorithm
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: key-in-header
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("DES", null);
            /**
                 * Note: the header containing the key should be cleared after
                 * marshalling to stop it from leaking by accident and
                 * potentially being compromised. The processor version below is
                 * arguably better as the key is left in the header when you use
                 * the DSL leaks the fact that camel encryption was used.
                 */
            from("direct:key-in-header-encrypt").marshal(cryptoFormat).removeHeader(CryptoDataFormat.KEY).to("mock:encrypted");
            from("direct:key-in-header-decrypt").unmarshal(cryptoFormat).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    exchange.getIn().getHeaders().remove(CryptoDataFormat.KEY);
                    exchange.getOut().copyFrom(exchange.getIn());
                }
            }).to("mock:unencrypted");
        // END SNIPPET: key-in-header
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: 3DES-ECB
            KeyGenerator generator = KeyGenerator.getInstance("DESede");
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("DESede/ECB/PKCS5Padding", generator.generateKey());
            from("direct:3des-ecb-encryption").marshal(cryptoFormat).to("mock:encrypted").unmarshal(cryptoFormat).to("mock:unencrypted");
        // END SNIPPET: 3DES-ECB
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: 3DES-CBC
            KeyGenerator generator = KeyGenerator.getInstance("DES");
            byte[] iv = new byte[8];
            SecureRandom random = new SecureRandom();
            random.nextBytes(iv);
            Key key = generator.generateKey();
            CryptoDataFormat encCryptoFormat = new CryptoDataFormat("DES/CBC/PKCS5Padding", key);
            encCryptoFormat.setInitializationVector(iv);
            encCryptoFormat.setShouldInlineInitializationVector(true);
            CryptoDataFormat decCryptoFormat = new CryptoDataFormat("DES/CBC/PKCS5Padding", key);
            decCryptoFormat.setShouldInlineInitializationVector(true);
            from("direct:3des-cbc-encryption").marshal(encCryptoFormat).to("mock:encrypted").unmarshal(decCryptoFormat).to("mock:unencrypted");
        // END SNIPPET: 3DES-CBC
        }
    }, new RouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: AES-128-ECB
            KeyGenerator generator = KeyGenerator.getInstance("AES");
            CryptoDataFormat cryptoFormat = new CryptoDataFormat("AES/ECB/PKCS5Padding", generator.generateKey());
            from("direct:aes-128-ecb-encryption").marshal(cryptoFormat).to("mock:encrypted").unmarshal(cryptoFormat).to("mock:unencrypted");
        // END SNIPPET: AES-128-ECB
        }
    } };
}
Also used : Exchange(org.apache.camel.Exchange) SecretKey(javax.crypto.SecretKey) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key) SecretKey(javax.crypto.SecretKey)

Example 58 with SecureRandom

use of java.security.SecureRandom in project camel by apache.

the class SigningProcessor method initSignatureService.

protected Signature initSignatureService(Exchange exchange) throws Exception {
    PrivateKey pk = getPrivateKeyFromKeystoreOrExchange(exchange);
    SecureRandom random = config.getSecureRandom();
    Signature service = createSignatureService();
    if (random != null) {
        service.initSign(pk, random);
    } else {
        service.initSign(pk);
    }
    return service;
}
Also used : PrivateKey(java.security.PrivateKey) Signature(java.security.Signature) SecureRandom(java.security.SecureRandom)

Example 59 with SecureRandom

use of java.security.SecureRandom in project redisson by redisson.

the class RedissonConcurrentMapTest method test_Multi_RemoveValue_MultiInstance.

@Test
public void test_Multi_RemoveValue_MultiInstance() throws InterruptedException {
    final String name = "test_Multi_RemoveValue_MultiInstance";
    ConcurrentMap<Integer, Integer> map = BaseTest.createInstance().getMap(name);
    for (int i = 0; i < 10; i++) {
        map.put(i, 1);
    }
    final SecureRandom secureRandom = new SecureRandom();
    testMultiInstanceConcurrency(100, r -> {
        ConcurrentMap<String, String> map1 = r.getMap(name);
        map1.remove(secureRandom.nextInt(10), 1);
    });
    assertMapSize(0, name);
}
Also used : SecureRandom(java.security.SecureRandom) Test(org.junit.Test)

Example 60 with SecureRandom

use of java.security.SecureRandom in project redisson by redisson.

the class RedissonConcurrentMapTest method test_Multi_Replace_MultiInstance.

@Test
public void test_Multi_Replace_MultiInstance() throws InterruptedException {
    final String name = "test_Multi_Replace_MultiInstance";
    ConcurrentMap<Integer, Integer> map = BaseTest.createInstance().getMap(name);
    for (int i = 0; i < 5; i++) {
        map.put(i, 1);
    }
    final SecureRandom secureRandom = new SecureRandom();
    testSingleInstanceConcurrency(100, r -> {
        ConcurrentMap<Integer, Integer> map1 = r.getMap(name);
        Assert.assertNotNull(map1.replace(secureRandom.nextInt(5), 2));
    });
    ConcurrentMap<Integer, Integer> testMap = BaseTest.createInstance().getMap(name);
    for (Integer value : testMap.values()) {
        Assert.assertEquals(2, (int) value);
    }
    assertMapSize(5, name);
}
Also used : SecureRandom(java.security.SecureRandom) Test(org.junit.Test)

Aggregations

SecureRandom (java.security.SecureRandom)720 SSLContext (javax.net.ssl.SSLContext)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)97 IOException (java.io.IOException)87 Test (org.junit.Test)76 SecretKey (javax.crypto.SecretKey)62 X509Certificate (java.security.cert.X509Certificate)61 KeyGenerator (javax.crypto.KeyGenerator)57 TrustManager (javax.net.ssl.TrustManager)56 X509TrustManager (javax.net.ssl.X509TrustManager)47 Cipher (javax.crypto.Cipher)46 KeyPairGenerator (java.security.KeyPairGenerator)44 BigInteger (java.math.BigInteger)42 CertificateException (java.security.cert.CertificateException)40 InvalidKeyException (java.security.InvalidKeyException)35 KeyPair (java.security.KeyPair)34 KeyStore (java.security.KeyStore)34 SecretKeySpec (javax.crypto.spec.SecretKeySpec)30 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)28 KeyManagementException (java.security.KeyManagementException)28