@Ignore @Test public void encryptJsonUsingKey() throws IOException { JsonNode plaintext = mapper.readTree("{\"RequestInfo\":{\"api_id\":\"1\",\"ver\":\"1\",\"ts\":null," + "\"action\":\"create\",\"did\":\"\",\"key\":\"\",\"msg_id\":\"\",\"requester_id\":\"\"," + "\"auth_token\":null},\"User\":{\"userName\":\"ajay\",\"name\":\"ajay\",\"gender\":\"male\"," + "\"mobileNumber\":\"12312312\",\"active\":true,\"type\":\"CITIZEN\",\"password\":\"password\"}}"); JsonNode ciphertext = encryptionServiceImpl.encryptJson(plaintext, "User", "pb"); log.info(ciphertext.toString()); }
public JsonNode encryptJson(Object plaintextJson, String key, String tenantId) throws IOException { JsonNode plaintextNode = createJsonNode(plaintextJson); JsonNode encryptNode = plaintextNode.deepCopy(); List<Attribute> attributesToEncrypt = encryptionPolicyConfiguration.getAttributesForKey(key); Map<String, List<Attribute>> typeAttributeMap = encryptionPolicyConfiguration.getTypeAttributeMap(attributesToEncrypt); for (String type : typeAttributeMap.keySet()) { List<Attribute> attributes = typeAttributeMap.get(type); List<String> paths = attributes.stream().map(Attribute::getJsonPath).collect(Collectors.toList()); JsonNode jsonNode = JacksonUtils.filterJsonNodeForPaths(plaintextNode, paths); if(! jsonNode.isEmpty(objectMapper.getSerializerProvider())) { JsonNode returnedEncryptedNode = objectMapper.valueToTree(encryptionServiceRestConnection.callEncrypt(tenantId, type, jsonNode)); encryptNode = JacksonUtils.merge(returnedEncryptedNode, encryptNode); } } return encryptNode; }
EncryptionServiceImpl implements EncryptionService { public JsonNode encryptJson(Object plaintextJson, String key, String tenantId) throws IOException { JsonNode plaintextNode = createJsonNode(plaintextJson); JsonNode encryptNode = plaintextNode.deepCopy(); List<Attribute> attributesToEncrypt = encryptionPolicyConfiguration.getAttributesForKey(key); Map<String, List<Attribute>> typeAttributeMap = encryptionPolicyConfiguration.getTypeAttributeMap(attributesToEncrypt); for (String type : typeAttributeMap.keySet()) { List<Attribute> attributes = typeAttributeMap.get(type); List<String> paths = attributes.stream().map(Attribute::getJsonPath).collect(Collectors.toList()); JsonNode jsonNode = JacksonUtils.filterJsonNodeForPaths(plaintextNode, paths); if(! jsonNode.isEmpty(objectMapper.getSerializerProvider())) { JsonNode returnedEncryptedNode = objectMapper.valueToTree(encryptionServiceRestConnection.callEncrypt(tenantId, type, jsonNode)); encryptNode = JacksonUtils.merge(returnedEncryptedNode, encryptNode); } } return encryptNode; } }
EncryptionServiceImpl implements EncryptionService { public JsonNode encryptJson(Object plaintextJson, String key, String tenantId) throws IOException { JsonNode plaintextNode = createJsonNode(plaintextJson); JsonNode encryptNode = plaintextNode.deepCopy(); List<Attribute> attributesToEncrypt = encryptionPolicyConfiguration.getAttributesForKey(key); Map<String, List<Attribute>> typeAttributeMap = encryptionPolicyConfiguration.getTypeAttributeMap(attributesToEncrypt); for (String type : typeAttributeMap.keySet()) { List<Attribute> attributes = typeAttributeMap.get(type); List<String> paths = attributes.stream().map(Attribute::getJsonPath).collect(Collectors.toList()); JsonNode jsonNode = JacksonUtils.filterJsonNodeForPaths(plaintextNode, paths); if(! jsonNode.isEmpty(objectMapper.getSerializerProvider())) { JsonNode returnedEncryptedNode = objectMapper.valueToTree(encryptionServiceRestConnection.callEncrypt(tenantId, type, jsonNode)); encryptNode = JacksonUtils.merge(returnedEncryptedNode, encryptNode); } } return encryptNode; }  }
EncryptionServiceImpl implements EncryptionService { public JsonNode encryptJson(Object plaintextJson, String key, String tenantId) throws IOException { JsonNode plaintextNode = createJsonNode(plaintextJson); JsonNode encryptNode = plaintextNode.deepCopy(); List<Attribute> attributesToEncrypt = encryptionPolicyConfiguration.getAttributesForKey(key); Map<String, List<Attribute>> typeAttributeMap = encryptionPolicyConfiguration.getTypeAttributeMap(attributesToEncrypt); for (String type : typeAttributeMap.keySet()) { List<Attribute> attributes = typeAttributeMap.get(type); List<String> paths = attributes.stream().map(Attribute::getJsonPath).collect(Collectors.toList()); JsonNode jsonNode = JacksonUtils.filterJsonNodeForPaths(plaintextNode, paths); if(! jsonNode.isEmpty(objectMapper.getSerializerProvider())) { JsonNode returnedEncryptedNode = objectMapper.valueToTree(encryptionServiceRestConnection.callEncrypt(tenantId, type, jsonNode)); encryptNode = JacksonUtils.merge(returnedEncryptedNode, encryptNode); } } return encryptNode; }  JsonNode encryptJson(Object plaintextJson, String key, String tenantId); P encryptJson(Object plaintextJson, String key, String tenantId, Class<E> valueType); JsonNode decryptJson(Object ciphertextJson, Map<Attribute, AccessType> attributeAccessTypeMap, User user); JsonNode decryptJson(Object ciphertextJson, String key, User user); P decryptJson(Object ciphertextJson, String key, User user, Class<E> valueType); String encryptValue(Object plaintext, String tenantId, String type); List<String> encryptValue(List<Object> plaintext, String tenantId, String type); }
EncryptionServiceImpl implements EncryptionService { public JsonNode encryptJson(Object plaintextJson, String key, String tenantId) throws IOException { JsonNode plaintextNode = createJsonNode(plaintextJson); JsonNode encryptNode = plaintextNode.deepCopy(); List<Attribute> attributesToEncrypt = encryptionPolicyConfiguration.getAttributesForKey(key); Map<String, List<Attribute>> typeAttributeMap = encryptionPolicyConfiguration.getTypeAttributeMap(attributesToEncrypt); for (String type : typeAttributeMap.keySet()) { List<Attribute> attributes = typeAttributeMap.get(type); List<String> paths = attributes.stream().map(Attribute::getJsonPath).collect(Collectors.toList()); JsonNode jsonNode = JacksonUtils.filterJsonNodeForPaths(plaintextNode, paths); if(! jsonNode.isEmpty(objectMapper.getSerializerProvider())) { JsonNode returnedEncryptedNode = objectMapper.valueToTree(encryptionServiceRestConnection.callEncrypt(tenantId, type, jsonNode)); encryptNode = JacksonUtils.merge(returnedEncryptedNode, encryptNode); } } return encryptNode; }  JsonNode encryptJson(Object plaintextJson, String key, String tenantId); P encryptJson(Object plaintextJson, String key, String tenantId, Class<E> valueType); JsonNode decryptJson(Object ciphertextJson, Map<Attribute, AccessType> attributeAccessTypeMap, User user); JsonNode decryptJson(Object ciphertextJson, String key, User user); P decryptJson(Object ciphertextJson, String key, User user, Class<E> valueType); String encryptValue(Object plaintext, String tenantId, String type); List<String> encryptValue(List<Object> plaintext, String tenantId, String type);  }