Hi Colm,

Thank you for the reply.

As per decryptUsingStAX method in EncryptionUtils class I tried to code
something similar but it returns a document object rather void:

public static Document decryptUsingStAX(InputStream inputStream,
                        List<QName> namesToEncrypt, Key privateKey) throws 
Exception {
                // Set up the Configuration
                XMLSecurityProperties properties = new XMLSecurityProperties();
                List<XMLSecurityConstants.Action> actions = new
ArrayList<XMLSecurityConstants.Action>();
                actions.add(XMLSecurityConstants.ENCRYPT);
                properties.setActions(actions);

                properties.setDecryptionKey(privateKey);

                InboundXMLSec inboundXMLSec = 
XMLSec.getInboundWSSec(properties);

                XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
                final XMLStreamReader xmlStreamReader = xmlInputFactory
                                .createXMLStreamReader(inputStream);

                TestSecurityEventListener eventListener = new 
TestSecurityEventListener();
                XMLStreamReader securityStreamReader = 
inboundXMLSec.processInMessage(
                                xmlStreamReader, null, eventListener);

                while (securityStreamReader.hasNext()) {
                        securityStreamReader.next();
                }

                xmlStreamReader.close();
                inputStream.close();

                // Check that what we were expecting to be encrypted was 
actually
                // encrypted
                List<EncryptedElementSecurityEvent> encryptedElementEvents = 
eventListener
                                
.getSecurityEvents(SecurityEventConstants.EncryptedElement);

                for (int i = 0; i < encryptedElementEvents.size(); i++) {
                        System.out.println(i + ": " + 
encryptedElementEvents.get(i));
                }

                for (QName nameToEncrypt : namesToEncrypt) {
                        boolean found = false;
                        for (EncryptedElementSecurityEvent encryptedElement :
encryptedElementEvents) {
                                if (encryptedElement.isEncrypted()
                                                && nameToEncrypt
                                                                
.equals(getEncryptedQName(encryptedElement
                                                                                
.getElementPath()))) {
                                        found = true;
                                        System.out.println("nameToEncrypt " + 
nameToEncrypt);
                                        break;
                                }
                                if (!found) {
                                        throw new Exception("Element not 
found");
                                }
                        }
                }

                Document document = StAX2DOM.readDoc(
                                XMLUtils.createDocumentBuilder(false), 
securityStreamReader);
                return document;
        }

Now, in my main method I have instructions like:
              // Decrypt
                Key privateKey = keyStore
                                .getKey("myservicekey", "skpass".toCharArray());
                ByteArrayInputStream bais = new 
ByteArrayInputStream(baos.toByteArray());

                Document result = EncryptionUtils.decryptUsingStAX(bais,
                                namesToEncrypt, privateKey);
                // check decrypt done successfully
                String resultString = getStringFromDocument(result);
                System.out.println("result:\n" + resultString.toString());

The method getStringFromDocument is a helper method 

public String getStringFromDocument(Document doc) {
                try {
                        DOMSource domSource = new DOMSource(doc);
                        StringWriter writer = new StringWriter();
                        StreamResult result = new StreamResult(writer);
                        TransformerFactory tf = 
TransformerFactory.newInstance();
                        Transformer transformer = tf.newTransformer();
                        transformer.transform(domSource, result);
                        return writer.toString();
                } catch (TransformerException ex) {
                        ex.printStackTrace();
                        return null;
                }
        }

The output is:
result:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>


Apologies if the question looks stupid but I am really struggling to figure
out the way to achieve the task, I would appreciate your assistance.
Thanks very much in advance,
Regards
Mujahed




--
View this message in context: 
http://apache-xml-project.6118.n7.nabble.com/newbie-decryption-stream-using-stax-tp41280p41286.html
Sent from the Apache XML - Security - Dev mailing list archive at Nabble.com.

Reply via email to