@@ -41,7 +41,7 @@ public function __construct(
4141 * @param string $contentEncryptionAlg the algorithm to use for the content encryption
4242 * @return string
4343 */
44- public function createSerializedJwe (
44+ public function createSerializedJweWithKey (
4545 array $ payloadArray , array $ encryptionJwk ,
4646 string $ keyEncryptionAlg = JwkService::PEM_ENC_KEY_ALGORITHM ,
4747 string $ contentEncryptionAlg = self ::CONTENT_ENCRYPTION_ALGORITHM ,
@@ -94,7 +94,7 @@ public function createSerializedJwe(
9494 * @return string
9595 * @throws \Exception
9696 */
97- public function decryptSerializedJwe (string $ serializedJwe , array $ jwkArray ): string {
97+ public function decryptSerializedJweWithKey (string $ serializedJwe , array $ jwkArray ): string {
9898 $ algorithmManager = new AlgorithmManager ([
9999 new A256KW (),
100100 new A256CBCHS512 (),
@@ -162,6 +162,24 @@ public function decryptSerializedJwe(string $serializedJwe, array $jwkArray): st
162162 return $ payload ;
163163 }
164164
165+ public function decryptSerializedJwe (string $ serializedJwe ): string {
166+ $ myPemEncryptionKey = $ this ->jwkService ->getMyEncryptionKey (true );
167+ $ sslEncryptionKey = openssl_pkey_get_private ($ myPemEncryptionKey );
168+ $ sslEncryptionKeyDetails = openssl_pkey_get_details ($ sslEncryptionKey );
169+ $ encPrivJwk = $ this ->jwkService ->getJwkFromSslKey ($ sslEncryptionKeyDetails , isEncryptionKey: true , includePrivateKey: true );
170+
171+ return $ this ->decryptSerializedJweWithKey ($ serializedJwe , $ encPrivJwk );
172+ }
173+
174+ public function createSerializedJwe (array $ payloadArray ): string {
175+ $ myPemEncryptionKey = $ this ->jwkService ->getMyEncryptionKey (true );
176+ $ sslEncryptionKey = openssl_pkey_get_private ($ myPemEncryptionKey );
177+ $ sslEncryptionKeyDetails = openssl_pkey_get_details ($ sslEncryptionKey );
178+ $ encPublicJwk = $ this ->jwkService ->getJwkFromSslKey ($ sslEncryptionKeyDetails , isEncryptionKey: true );
179+
180+ return $ this ->createSerializedJweWithKey ($ payloadArray , $ encPublicJwk );
181+ }
182+
165183 public function debug (): array {
166184 $ myPemEncryptionKey = $ this ->jwkService ->getMyEncryptionKey (true );
167185 $ sslEncryptionKey = openssl_pkey_get_private ($ myPemEncryptionKey );
@@ -185,12 +203,17 @@ public function debug(): array {
185203 $serializedJweToken = $this->createSerializedJwe($payloadArray, $exampleJwkArray);
186204 $decryptedJweString = $this->decryptSerializedJwe($serializedJweToken, $exampleJwkArray);
187205 */
188- $ serializedJweToken = $ this ->createSerializedJwe ($ payloadArray , $ encPublicJwk );
189- $ decryptedJweString = $ this ->decryptSerializedJwe ($ serializedJweToken , $ encPrivJwk );
206+ $ serializedJweToken = $ this ->createSerializedJweWithKey ($ payloadArray , $ encPublicJwk );
207+ $ jwtParts = explode ('. ' , $ serializedJweToken , 3 );
208+ $ jwtHeader = json_decode (base64_decode ($ jwtParts [0 ]), true );
209+ $ decryptedJweString = $ this ->decryptSerializedJweWithKey ($ serializedJweToken , $ encPrivJwk );
190210
191211 return [
212+ 'public_key ' => $ encPublicJwk ,
213+ 'private_key ' => $ encPrivJwk ,
192214 'input_payloadArray ' => $ payloadArray ,
193215 'input_serializedJweToken ' => $ serializedJweToken ,
216+ 'jwe_header ' => $ jwtHeader ,
194217 'output_payloadArray ' => json_decode ($ decryptedJweString , true ),
195218 ];
196219 }
0 commit comments