DocumentEncrypterImpl implements DocumentEncrypter { @Override public String encryptDocument(Key deSedeEncryptKey, String document, RuleExecutionContainer ruleExecutionContainer) { Document xmlDocument = null; String xmlString = null; boolean encryptDoc = false; for (RuleExecutionResponse response : ruleExecutionContainer .getExecutionResponseList()) { if (response.getDocumentObligationPolicy().equals(ObligationPolicyDocument.ENCRYPT)) { encryptDoc = true; break; } } if (encryptDoc) { try { xmlDocument = documentXmlConverter.loadDocument(document); Key aesSymmetricKey = EncryptTool.generateDataEncryptionKey(); String algorithmURI = XMLCipher.TRIPLEDES_KeyWrap; XMLCipher keyCipher = XMLCipher.getInstance(algorithmURI); keyCipher.init(XMLCipher.WRAP_MODE, deSedeEncryptKey); EncryptedKey encryptedKey = keyCipher.encryptKey(xmlDocument, aesSymmetricKey); Element rootElement = xmlDocument.getDocumentElement(); encryptElement(xmlDocument, aesSymmetricKey, encryptedKey, rootElement); xmlString = documentXmlConverter .convertXmlDocToString(xmlDocument); } catch (Exception e) { logger.error(e.getMessage(), e); throw new DS4PException(e.toString(), e); } } return xmlString; } DocumentEncrypterImpl();  DocumentEncrypterImpl(DocumentXmlConverter documentXmlConverter); @Override String encryptDocument(Key deSedeEncryptKey, String document,
			RuleExecutionContainer ruleExecutionContainer);  }
@Test public void testEncryptDocument() throws SAXException, IOException { logger.debug("NOT ENCRYPTED--> " + c32); String encrypted = null; try { encrypted = documentEncrypter.encryptDocument( EncryptTool.generateKeyEncryptionKey(), c32, ruleExecutionContainer); } catch (Exception e) { logger.error(e.getMessage(), e); } logger.debug("ENCRYPTED--> " + encrypted); assertNotEquals(c32, encrypted); assertTrue(XmlComparator.compareXMLs(testEncrypted, encrypted, Arrays.asList("CipherData")).similar()); }
@Test public void testEncryptDocument_EmptyRuleExecutionContainer() { logger.debug("NOT ENCRYPTED--> " + c32); String encrypted = null; try { List<RuleExecutionResponse> list = new LinkedList<RuleExecutionResponse>(); RuleExecutionContainer container = new RuleExecutionContainer(); container.setExecutionResponseList(list); encrypted = documentEncrypter.encryptDocument( EncryptTool.generateKeyEncryptionKey(), c32, container); } catch (Exception e) { logger.error(e.getMessage(), e); } logger.debug("ENCRYPTED--> " + encrypted); assertNotEquals(c32, encrypted); assertNull(encrypted); }
@Test public void testEncryptDocument_NoEncryptObligationPolicy() { logger.debug("NOT ENCRYPTED--> " + c32); String encrypted = null; try { List<RuleExecutionResponse> list = new LinkedList<RuleExecutionResponse>(); RuleExecutionContainer container = new RuleExecutionContainer(); RuleExecutionResponse resp = new RuleExecutionResponse(); resp.setDocumentRefrainPolicy(RefrainPolicy.NOVIP); list.add(resp); container.setExecutionResponseList(list); encrypted = documentEncrypter.encryptDocument( EncryptTool.generateKeyEncryptionKey(), c32, container); } catch (Exception e) { logger.error(e.getMessage(), e); } logger.debug("ENCRYPTED--> " + encrypted); assertNotEquals(c32, encrypted); assertNull(encrypted); }
@Test(expected = DS4PException.class) public void testEncryptDocument_Throws_DS4PException() throws XMLEncryptionException, Exception { documentEncrypter.encryptDocument( EncryptTool.generateDataEncryptionKey(), "", ruleExecutionContainer); }