Search in sources :

Example 36 with KeyGenerator

use of javax.crypto.KeyGenerator in project otter by alibaba.

the class AESUtils method generateSecretKey.

/**
     * 生成AES密钥
     * 
     * @return Secret key
     * @throws AESException AES exception
     */
public byte[] generateSecretKey() throws AESException {
    try {
        // Get the KeyGenerator
        KeyGenerator kgen = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM);
        // 192 and 256 bits may not be available
        kgen.init(KEY_SIZE);
        // Generate the secret key specs.
        SecretKey skey = kgen.generateKey();
        secretKey = skey.getEncoded();
        return secretKey;
    } catch (NoSuchAlgorithmException e) {
        throw new AESException(e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyGenerator(javax.crypto.KeyGenerator)

Example 37 with KeyGenerator

use of javax.crypto.KeyGenerator in project camel by apache.

the class EncryptionAlgorithmTest method testAES128GCM.

@Test
public void testAES128GCM() throws Exception {
    // Set up the Key
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);
    SecretKey key = keygen.generateKey();
    final XMLSecurityDataFormat xmlEncDataFormat = new XMLSecurityDataFormat();
    xmlEncDataFormat.setPassPhrase(key.getEncoded());
    xmlEncDataFormat.setSecureTagContents(true);
    xmlEncDataFormat.setSecureTag("//cheesesites/italy/cheese");
    xmlEncDataFormat.setXmlCipherAlgorithm(XMLCipher.AES_128_GCM);
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct:start").marshal(xmlEncDataFormat).to("mock:encrypted").log("Body: + ${body}").unmarshal(xmlEncDataFormat).to("mock:decrypted");
        }
    });
    xmlsecTestHelper.testDecryption(context);
}
Also used : SecretKey(javax.crypto.SecretKey) RouteBuilder(org.apache.camel.builder.RouteBuilder) KeyGenerator(javax.crypto.KeyGenerator) Test(org.junit.Test)

Example 38 with KeyGenerator

use of javax.crypto.KeyGenerator in project camel by apache.

the class EncryptionAlgorithmTest method testAES128.

@Test
public void testAES128() throws Exception {
    // Set up the Key
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(128);
    SecretKey key = keygen.generateKey();
    final XMLSecurityDataFormat xmlEncDataFormat = new XMLSecurityDataFormat();
    xmlEncDataFormat.setPassPhrase(key.getEncoded());
    xmlEncDataFormat.setSecureTagContents(true);
    xmlEncDataFormat.setSecureTag("//cheesesites/italy/cheese");
    xmlEncDataFormat.setXmlCipherAlgorithm(XMLCipher.AES_128);
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct:start").marshal(xmlEncDataFormat).to("mock:encrypted").log("Body: + ${body}").unmarshal(xmlEncDataFormat).to("mock:decrypted");
        }
    });
    xmlsecTestHelper.testDecryption(context);
}
Also used : SecretKey(javax.crypto.SecretKey) RouteBuilder(org.apache.camel.builder.RouteBuilder) KeyGenerator(javax.crypto.KeyGenerator) Test(org.junit.Test)

Example 39 with KeyGenerator

use of javax.crypto.KeyGenerator in project camel by apache.

the class EncryptionAlgorithmTest method testCAMELLIA256.

@Test
public void testCAMELLIA256() throws Exception {
    if (!TestHelper.UNRESTRICTED_POLICIES_INSTALLED) {
        return;
    }
    // Set up the Key
    KeyGenerator keygen = KeyGenerator.getInstance("CAMELLIA");
    keygen.init(256);
    SecretKey key = keygen.generateKey();
    final XMLSecurityDataFormat xmlEncDataFormat = new XMLSecurityDataFormat();
    xmlEncDataFormat.setPassPhrase(key.getEncoded());
    xmlEncDataFormat.setSecureTagContents(true);
    xmlEncDataFormat.setSecureTag("//cheesesites/italy/cheese");
    xmlEncDataFormat.setXmlCipherAlgorithm(XMLCipher.CAMELLIA_256);
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct:start").marshal(xmlEncDataFormat).to("mock:encrypted").log("Body: + ${body}").unmarshal(xmlEncDataFormat).to("mock:decrypted");
        }
    });
    xmlsecTestHelper.testDecryption(context);
}
Also used : SecretKey(javax.crypto.SecretKey) RouteBuilder(org.apache.camel.builder.RouteBuilder) KeyGenerator(javax.crypto.KeyGenerator) Test(org.junit.Test)

Example 40 with KeyGenerator

use of javax.crypto.KeyGenerator in project camel by apache.

the class EncryptionAlgorithmTest method testAES256.

@Test
public void testAES256() throws Exception {
    if (!TestHelper.UNRESTRICTED_POLICIES_INSTALLED) {
        return;
    }
    // Set up the Key
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    keygen.init(256);
    SecretKey key = keygen.generateKey();
    final XMLSecurityDataFormat xmlEncDataFormat = new XMLSecurityDataFormat();
    xmlEncDataFormat.setPassPhrase(key.getEncoded());
    xmlEncDataFormat.setSecureTagContents(true);
    xmlEncDataFormat.setSecureTag("//cheesesites/italy/cheese");
    xmlEncDataFormat.setXmlCipherAlgorithm(XMLCipher.AES_256);
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct:start").marshal(xmlEncDataFormat).to("mock:encrypted").log("Body: + ${body}").unmarshal(xmlEncDataFormat).to("mock:decrypted");
        }
    });
    xmlsecTestHelper.testDecryption(context);
}
Also used : SecretKey(javax.crypto.SecretKey) RouteBuilder(org.apache.camel.builder.RouteBuilder) KeyGenerator(javax.crypto.KeyGenerator) Test(org.junit.Test)

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