diff --git a/src/main/java/org/apache/xml/security/utils/Encryption11ElementProxy.java b/src/main/java/org/apache/xml/security/utils/Encryption11ElementProxy.java index ba9f6e394..41fb2d50c 100644 --- a/src/main/java/org/apache/xml/security/utils/Encryption11ElementProxy.java +++ b/src/main/java/org/apache/xml/security/utils/Encryption11ElementProxy.java @@ -43,7 +43,7 @@ public Encryption11ElementProxy(Document doc) { setElement(XMLUtils.createElementInEncryption11Space(doc, this.getBaseLocalName())); String prefix = ElementProxy.getDefaultPrefix(this.getBaseNamespace()); if (prefix != null && prefix.length() > 0) { - getElement().setAttribute("xmlns:" + prefix, this.getBaseNamespace()); + getElement().setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix, this.getBaseNamespace()); } } diff --git a/src/main/java/org/apache/xml/security/utils/EncryptionElementProxy.java b/src/main/java/org/apache/xml/security/utils/EncryptionElementProxy.java index c23b33d75..b1b2b907c 100644 --- a/src/main/java/org/apache/xml/security/utils/EncryptionElementProxy.java +++ b/src/main/java/org/apache/xml/security/utils/EncryptionElementProxy.java @@ -44,7 +44,7 @@ public EncryptionElementProxy(Document doc) { setElement(XMLUtils.createElementInEncryptionSpace(doc, this.getBaseLocalName())); String prefix = ElementProxy.getDefaultPrefix(this.getBaseNamespace()); if (prefix != null && !prefix.isEmpty()) { - getElement().setAttribute("xmlns:" + prefix, this.getBaseNamespace()); + getElement().setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix, this.getBaseNamespace()); } } diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11Test.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11Test.java index d47d0551d..08de39ca3 100644 --- a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11Test.java +++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLEncryption11Test.java @@ -525,6 +525,12 @@ void testAgreementKeyEncryptDecryptDocument(String w3cTag, Files.write(Path.of("target","test-enc-"+w3cTag+".xml"), toString(doc.getFirstChild()).getBytes()); // XMLUtils.outputDOM(doc.getFirstChild(), System.out); + // re-read the document and compare both are the same after canonicalization + Document doc2 = XMLUtils.read( + new ByteArrayInputStream(toString(doc.getFirstChild()).getBytes(java.nio.charset.StandardCharsets.UTF_8)), + false); + assertEquals(toString(doc.getFirstChild()), toString(doc2.getFirstChild())); + // Perform decryption Document dd = decryptElement(doc, ecKey, (X509Certificate)cert); // XMLUtils.outputDOM(dd.getFirstChild(), System.out);