@@ -33,7 +33,7 @@ public static function encryptPayload($payload, $config, $params = null) {
3333
3434 // Perform encryption (if needed)
3535 foreach ($ config ->getEncryptionPaths () as $ jsonPathIn => $ jsonPathOut ) {
36- self ::encryptPayloadPath ($ payloadJsonObject , $ jsonPathIn , $ jsonPathOut , $ config , $ params );
36+ $ payloadJsonObject = self ::encryptPayloadPath ($ payloadJsonObject , $ jsonPathIn , $ jsonPathOut , $ config , $ params );
3737 }
3838
3939 // Return the updated payload
@@ -64,7 +64,7 @@ public static function decryptPayload($payload, $config, $params = null) {
6464
6565 // Perform decryption (if needed)
6666 foreach ($ config ->getDecryptionPaths () as $ jsonPathIn => $ jsonPathOut ) {
67- self ::decryptPayloadPath ($ payloadJsonObject , $ jsonPathIn , $ jsonPathOut , $ config , $ params );
67+ $ payloadJsonObject = self ::decryptPayloadPath ($ payloadJsonObject , $ jsonPathIn , $ jsonPathOut , $ config , $ params );
6868 }
6969
7070 // Return the updated payload
@@ -91,7 +91,7 @@ private static function encryptPayloadPath($payloadJsonObject, $jsonPathIn, $jso
9191 $ inJsonObject = self ::readJsonElement ($ payloadJsonObject , $ jsonPathIn );
9292 if (is_null ($ inJsonObject )) {
9393 // Nothing to encrypt
94- return ;
94+ return $ payloadJsonObject ;
9595 }
9696
9797 if (empty ($ params )) {
@@ -108,10 +108,7 @@ private static function encryptPayloadPath($payloadJsonObject, $jsonPathIn, $jso
108108 if ('$ ' !== $ jsonPathIn ) {
109109 JsonPath::delete ($ payloadJsonObject , $ jsonPathIn );
110110 } else {
111- // Delete keys one by one
112- foreach ($ inJsonObject as $ key => $ value ) {
113- unset($ inJsonObject ->$ key );
114- }
111+ $ payloadJsonObject = json_decode ('{} ' );
115112 }
116113
117114 // Add encrypted data and encryption fields at the given JSON path
@@ -132,6 +129,7 @@ private static function encryptPayloadPath($payloadJsonObject, $jsonPathIn, $jso
132129 if (!empty ($ config ->getOaepPaddingDigestAlgorithmFieldName ())) {
133130 $ outJsonObject ->{$ config ->getOaepPaddingDigestAlgorithmFieldName ()} = $ params ->getOaepPaddingDigestAlgorithmValue ();
134131 }
132+ return $ payloadJsonObject ;
135133 }
136134
137135 /**
@@ -147,14 +145,14 @@ private static function decryptPayloadPath($payloadJsonObject, $jsonPathIn, $jso
147145 $ inJsonObject = self ::readJsonObject ($ payloadJsonObject , $ jsonPathIn );
148146 if (is_null ($ inJsonObject )) {
149147 // Nothing to decrypt
150- return ;
148+ return $ payloadJsonObject ;
151149 }
152150
153151 // Read and remove encrypted data and encryption fields at the given JSON path
154152 $ encryptedValueJsonElement = self ::readAndDeleteJsonKey ($ inJsonObject , $ config ->getEncryptedValueFieldName ());
155153 if (empty ($ encryptedValueJsonElement )) {
156154 // Nothing to decrypt
157- return ;
155+ return $ payloadJsonObject ;
158156 }
159157
160158 if (!$ config ->useHttpPayloads () && empty ($ params )) {
@@ -176,16 +174,17 @@ private static function decryptPayloadPath($payloadJsonObject, $jsonPathIn, $jso
176174 $ encryptedValueBytes = EncodingUtils::decodeValue ($ encryptedValueJsonElement , $ config ->getFieldValueEncoding ());
177175 $ decryptedValueBytes = self ::decryptBytes ($ params ->getSecretKeyBytes (), $ params ->getIvBytes (), $ encryptedValueBytes );
178176
179- // Add decrypted data at the given JSON path
177+ // Add decrypted data at the given JSON path
180178 $ decryptedValue = self ::sanitizeJson ($ decryptedValueBytes );
181179 $ outJsonObject = self ::checkOrCreateOutObject ($ payloadJsonObject , $ jsonPathOut );
182- self ::addDecryptedDataToPayload ($ payloadJsonObject , $ jsonPathOut , $ outJsonObject , $ decryptedValue );
180+ $ payloadJsonObject = self ::addDecryptedDataToPayload ($ payloadJsonObject , $ jsonPathOut , $ outJsonObject , $ decryptedValue );
183181
184182 // Remove the input if now empty
185183 $ inJsonElement = self ::readJsonElement ($ payloadJsonObject , $ jsonPathIn );
186184 if (empty ((array )$ inJsonElement ) && '$ ' !== $ jsonPathIn ) {
187185 JsonPath::delete ($ payloadJsonObject , $ jsonPathIn );
188186 }
187+ return $ payloadJsonObject ;
189188 }
190189
191190 /**
@@ -200,19 +199,25 @@ private static function addDecryptedDataToPayload($payloadJsonObject, $jsonPathO
200199 // 'json_decode' returns null for strings
201200 $ decryptedValueJsonElement = $ decryptedValue ;
202201 }
202+
203+ if ('$ ' === $ jsonPathOut && is_array ($ decryptedValueJsonElement )) {
204+ return $ decryptedValueJsonElement ;
205+ }
206+
203207 if (!is_object ($ decryptedValueJsonElement )) {
204208 // Array or primitive: overwrite
205209 $ parentPath = JsonPath::getParentPath ($ jsonPathOut );
206210 $ elementKey = JsonPath::getElementKey ($ jsonPathOut );
207211 $ parentObject = JsonPath::find ($ payloadJsonObject , $ parentPath );
208212 $ parentObject ->$ elementKey = $ decryptedValueJsonElement ;
209- return ;
213+ return $ payloadJsonObject ;
210214 }
211215
212216 // Object: merge
213217 foreach ($ decryptedValueJsonElement as $ key => $ value ) {
214218 $ outJsonObject ->$ key = $ value ;
215219 }
220+ return $ payloadJsonObject ;
216221 }
217222
218223 /**
0 commit comments