private static JToken EncryptPayloadPath(JToken json, string jsonPathIn, string jsonPathOut, JweConfig config) { JToken token = json.SelectToken(jsonPathIn); if (JsonUtils.IsNullOrEmptyJson(token)) { // Nothing to encrypt return(json); } // Encode and encrypt string inJsonString = JsonUtils.SanitizeJson(token.ToString(Formatting.None)); JweHeader header = new JweHeader(ALGORITHM, ENCRYPTION, config.EncryptionKeyFingerprint, CONTENT_TYPE); string encrypted = JweObject.Encrypt(config, inJsonString, header); // Delete data in the clear if ("$".Equals(jsonPathIn)) { // Create a new object json = JObject.Parse("{}"); } else { token.Parent.Remove(); } JsonUtils.CheckOrCreateOutObject(json, jsonPathOut); var outJsonToken = json.SelectToken(jsonPathOut) as JObject; JsonUtils.AddOrReplaceJsonKey(outJsonToken, config.EncryptedValueFieldName, encrypted); return(outJsonToken); }
public static JweObject Parse(String encryptedPayload) { string[] fields = encryptedPayload.Trim().Split('.'); JweObject jweObject = new JweObject(); jweObject.RawHeader = fields[0]; jweObject.Header = JweHeader.Parse(jweObject.RawHeader); jweObject.EncryptedKey = fields[1]; jweObject.Iv = fields[2]; jweObject.CipherText = fields[3]; jweObject.AuthTag = fields[4]; return(jweObject); }
private static JToken DecryptPayloadPath(JToken payload, string jsonPathIn, string jsonPathOut, JweConfig config) { JToken token = payload.SelectToken(jsonPathIn); if (JsonUtils.IsNullOrEmptyJson(token)) { // Nothing to decrypt return(payload); } // Read and remove encrypted data and encryption fields at the given JSON path string encryptedValue = ReadAndDeleteJsonKey(payload, token, config.EncryptedValueFieldName); if (string.IsNullOrEmpty(encryptedValue)) { // Nothing to decrypt return(payload); } JweObject jweObject = JweObject.Parse(encryptedValue); string decryptedValue = jweObject.Decrypt(config); if ("$".Equals(jsonPathOut)) { return(JObject.Parse(decryptedValue)); } JsonUtils.CheckOrCreateOutObject(payload, jsonPathOut); JsonUtils.AddDecryptedDataToPayload(payload, decryptedValue, jsonPathOut); // Remove the input token = payload.SelectToken(jsonPathIn); if (null != token && token.Parent != null) { token.Parent.Remove(); } return(payload); }