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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations