use of org.apache.xml.security.encryption.EncryptionProperty in project santuario-java by apache.
the class XMLCipherTest method testEncryptionProperties.
@org.junit.Test
public void testEncryptionProperties() throws Exception {
// source
Document d = document();
// target
Document ed = null;
// target
Document dd = null;
Element e = d.getDocumentElement();
Element ee = null;
String source = null;
String target = null;
if (haveISOPadding) {
source = toString(d);
// prepare for encryption
byte[] passPhrase = "24 Bytes per DESede key!".getBytes();
DESedeKeySpec keySpec = new DESedeKeySpec(passPhrase);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
SecretKey key = keyFactory.generateSecret(keySpec);
// encrypt
cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
cipher.init(XMLCipher.ENCRYPT_MODE, key);
// Add EncryptionProperties
Element elem = d.createElement("CustomInformation");
elem.setTextContent("Some text content");
EncryptionProperties eps = cipher.createEncryptionProperties();
EncryptionProperty ep = cipher.createEncryptionProperty();
ep.addEncryptionInformation(elem);
ep.setId("_124124");
ep.setTarget("http://localhost/");
ep.setAttribute("xml:lang", "en");
eps.addEncryptionProperty(ep);
EncryptedData encData = cipher.getEncryptedData();
encData.setEncryptionProperties(eps);
ed = cipher.doFinal(d, e);
// XMLUtils.outputDOM(ed, System.out);
// decrypt
cipher = XMLCipher.getInstance(XMLCipher.TRIPLEDES);
cipher.init(XMLCipher.DECRYPT_MODE, key);
ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
dd = cipher.doFinal(ed, ee);
target = toString(dd);
assertEquals(source, target);
} else {
LOG.warn("Test testTripleDesDocumentCipher skipped as " + "necessary algorithms not available");
}
}
Aggregations