Search in sources :

Example 1 with EncryptionMethod

use of org.apache.xml.security.encryption.EncryptionMethod in project santuario-java by apache.

the class XMLCipherTest method testSameDocumentCipherReference.

/*
     * Test a Cipher Reference
     */
@org.junit.Test
public void testSameDocumentCipherReference() throws Exception {
    if (haveISOPadding) {
        DocumentBuilder db = XMLUtils.createDocumentBuilder(false);
        Document d = db.newDocument();
        Element docElement = d.createElement("EncryptedDoc");
        d.appendChild(docElement);
        // Create the XMLCipher object
        cipher = XMLCipher.getInstance();
        EncryptedData ed = cipher.createEncryptedData(CipherData.REFERENCE_TYPE, "#CipherTextId");
        EncryptionMethod em = cipher.createEncryptionMethod(XMLCipher.AES_128);
        ed.setEncryptionMethod(em);
        org.apache.xml.security.encryption.Transforms xencTransforms = cipher.createTransforms(d);
        ed.getCipherData().getCipherReference().setTransforms(xencTransforms);
        org.apache.xml.security.transforms.Transforms dsTransforms = xencTransforms.getDSTransforms();
        // An XPath transform
        XPathContainer xpc = new XPathContainer(d);
        xpc.setXPath("self::text()[parent::CipherText[@Id=\"CipherTextId\"]]");
        dsTransforms.addTransform(org.apache.xml.security.transforms.Transforms.TRANSFORM_XPATH, xpc.getElementPlusReturns());
        // Add a Base64 Transforms
        dsTransforms.addTransform(org.apache.xml.security.transforms.Transforms.TRANSFORM_BASE64_DECODE);
        Element ee = cipher.martial(d, ed);
        docElement.appendChild(ee);
        // Add the cipher text
        Element encryptedElement = d.createElement("CipherText");
        encryptedElement.setAttributeNS(null, "Id", "CipherTextId");
        encryptedElement.setIdAttributeNS(null, "Id", true);
        encryptedElement.appendChild(d.createTextNode(tstBase64EncodedString));
        docElement.appendChild(encryptedElement);
        // dump(d);
        // Now the decrypt, with a brand new cipher
        XMLCipher cipherDecrypt = XMLCipher.getInstance();
        Key key = new SecretKeySpec("abcdefghijklmnop".getBytes(StandardCharsets.US_ASCII), "AES");
        cipherDecrypt.init(XMLCipher.DECRYPT_MODE, key);
        byte[] decryptBytes = cipherDecrypt.decryptToByteArray(ee);
        assertEquals("A test encrypted secret", new String(decryptBytes, StandardCharsets.US_ASCII));
    } else {
        LOG.warn("Test testSameDocumentCipherReference skipped as " + "necessary algorithms not available");
    }
}
Also used : Element(org.w3c.dom.Element) XMLCipher(org.apache.xml.security.encryption.XMLCipher) EncryptionMethod(org.apache.xml.security.encryption.EncryptionMethod) Document(org.w3c.dom.Document) XPathContainer(org.apache.xml.security.transforms.params.XPathContainer) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SecretKeySpec(javax.crypto.spec.SecretKeySpec) EncryptedData(org.apache.xml.security.encryption.EncryptedData) PublicKey(java.security.PublicKey) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey)

Aggregations

Key (java.security.Key)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 SecretKey (javax.crypto.SecretKey)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 EncryptedData (org.apache.xml.security.encryption.EncryptedData)1 EncryptedKey (org.apache.xml.security.encryption.EncryptedKey)1 EncryptionMethod (org.apache.xml.security.encryption.EncryptionMethod)1 XMLCipher (org.apache.xml.security.encryption.XMLCipher)1 XPathContainer (org.apache.xml.security.transforms.params.XPathContainer)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1