My problem is I am unable to decrypt the encrypted data using pho package like web-token/jwt-framework.
Here is the code for encryption that I did it given encrypted string. But no success while decrypting it returns false.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use JoseComponentCoreAlgorithmManager;
use JoseComponentCoreJWK;
use JoseComponentCoreJWKSet;
use JoseComponentEncryptionAlgorithmKeyEncryptionRSAOAEP256;
use JoseComponentEncryptionAlgorithmContentEncryptionA256GCM;
use JoseComponentEncryptionSerializerCompactSerializer;
use JoseComponentEncryptionJWEBuilder;
$algorithmManager = new AlgorithmManager([new RSAOAEP256(), new A256GCM()]);
$jweBuilder = new JWEBuilder($algorithmManager);
$JWEserializer = new CompactSerializer();
$privateKey = file_get_contents('privateKey.key');
$decodedPrivateKey = openssl_pkey_get_private($privateKey);
$keyDetails = openssl_pkey_get_details($decodedPrivateKey);
$n = rtrim(strtr(base64_encode($keyDetails['rsa']['n']), '+/', '-_'), '=');
$e = rtrim(strtr(base64_encode($keyDetails['rsa']['e']), '+/', '-_'), '=');
$d = rtrim(strtr(base64_encode($keyDetails['rsa']['d']), '+/', '-_'), '=');
$recipientKey = new JWK([
'kty' => 'RSA',
'n' => $n,
'e' => $e,
'd' => $d
]);
$recipientHeader = [
'enc' => 'A256GCM',
'alg' => 'RSA-OAEP-256'
];
$payload = [
"Data" => [
"wstoken" => "b455d8373bfde040749065a4728f2d2b",
"wsfunction" => "mobile_webservices_get_mynotifications",
"moodlewsrestformat" => "json"
],
"Risk" => []
];
$jsonPayload = json_encode($payload);
// Sign the data using the private key
openssl_sign($jsonPayload, $signature, $privateKey, OPENSSL_ALGO_SHA256);
$signedPayloadRS256 = base64_encode($signature);
$jwe = $jweBuilder->create()
->withPayload($signedPayloadRS256)
->addRecipient($recipientKey, $recipientHeader)
//->withSharedProtectedHeader([])
->build();
echo $jweToken = $JWEserializer->serialize($jwe);
?>
Can someone please verify that my encryption process or method is proper?
New contributor
what do you think about is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.