@Override public SerializableDatabase load(InputStream inputStream) throws IOException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); doc = dBuilder.parse(inputStream); // we need to decrypt all protected fields // TODO we assume they are all strings, which is wrong NodeList protectedContent = (NodeList) DomHelper.xpath.evaluate("//*[@Protected='True']", doc, XPathConstants.NODESET); for (int i = 0; i < protectedContent.getLength(); i++){ Element element = ((Element) protectedContent.item(i)); String base64 = DomHelper.getElementContent(".", element); // Android compatibility byte[] encrypted = Base64.decodeBase64(base64.getBytes()); String decrypted = new String(encryption.decrypt(encrypted), "UTF-8"); DomHelper.setElementContent(".", element, decrypted); element.removeAttribute("Protected"); } return this; } catch (ParserConfigurationException e) { throw new IllegalStateException("Instantiating Document Builder", e); } catch (SAXException e) { throw new IllegalStateException("Parsing exception", e); } catch (XPathExpressionException e) { throw new IllegalStateException("XPath Exception", e); } }
if (encryptContent) { String unencrypted = event.asCharacters().getData(); String encrypted = Helpers.encodeBase64Content(encryptor.encrypt(unencrypted.getBytes()), false); event = eventFactory.createCharacters(encrypted);
decrypted = ""; byte[] encrypted = encryption.encrypt(decrypted.getBytes());
@Override public void beforeMarshal(Object source) { try { if (source instanceof StringField) { StringField field = (StringField) source; if (toEncrypt.contains(field.getKey())) { byte[] encrypted = encryption.encrypt(field.getValue().getValue().getBytes()); String b64 = new String(Base64.encodeBase64(encrypted), "UTF-8"); field.getValue().setValue(b64); field.getValue().setProtected(true); } } } catch (UnsupportedEncodingException e) { throw new IllegalStateException(); } } });
public XMLEvent transform (XMLEvent event) { switch (event.getEventType()) { case START_ELEMENT: { Attribute attribute = event.asStartElement().getAttributeByName(new QName("Protected")); if (attribute != null) { decryptContent = Helpers.toBoolean(attribute.getValue()); } break; } case END_ELEMENT: { decryptContent = false; break; } case CHARACTERS: { if (decryptContent) { String text = event.asCharacters().getData(); text = new String(streamEncryptor.decrypt(Helpers.decodeBase64Content(text.getBytes(), false))); event = xmlEventFactory.createCharacters(text); } break; } } return event; } }
@Override public void afterUnmarshal(Object target, Object parent) { try { if (target instanceof StringField.Value) { StringField.Value value = (StringField.Value) target; if (value.getProtected() !=null && value.getProtected()) { byte[] encrypted = Base64.decodeBase64(value.getValue().getBytes()); String decrypted = new String(encryption.decrypt(encrypted), "UTF-8"); value.setValue(decrypted); value.setProtected(false); } } if (target instanceof JaxbGroupBinding && (parent instanceof JaxbGroupBinding)) { ((JaxbGroupBinding) target).parent = ((JaxbGroupBinding) parent); } if (target instanceof JaxbEntryBinding && (parent instanceof JaxbGroupBinding)) { ((JaxbEntryBinding) target).parent = ((JaxbGroupBinding) parent); } } catch (UnsupportedEncodingException e) { throw new IllegalStateException(); } } });