Search in sources :

Example 1 with EncryptionProperty

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");
    }
}
Also used : EncryptionProperty(org.apache.xml.security.encryption.EncryptionProperty) SecretKey(javax.crypto.SecretKey) Element(org.w3c.dom.Element) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) EncryptionProperties(org.apache.xml.security.encryption.EncryptionProperties) EncryptedData(org.apache.xml.security.encryption.EncryptedData) Document(org.w3c.dom.Document) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

SecretKey (javax.crypto.SecretKey)1 SecretKeyFactory (javax.crypto.SecretKeyFactory)1 DESedeKeySpec (javax.crypto.spec.DESedeKeySpec)1 EncryptedData (org.apache.xml.security.encryption.EncryptedData)1 EncryptionProperties (org.apache.xml.security.encryption.EncryptionProperties)1 EncryptionProperty (org.apache.xml.security.encryption.EncryptionProperty)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1