In Drupal 9 JWE decryption

In Drupal 9 Is it available any module for decrypt data which is encrypted by Json Web Encryption with “RSA256” Algoritham.

If no than please suggest any PHP library for same.

Hello Zamin!

Firstly, it’s important to note that decryption and encryption functionalities do not belong to Drupal; you can utilize PHP’s default functions to decode the string and then proceed with JWT handling as illustrated below:

$publicKey = openssl_pkey_get_public("file://publickey.pem");
$privateKey = openssl_pkey_get_private("file://privatekey.pem");
$myText = "The quick brown fox jumps over the lazy dog";

echo "Before encryption: " . $myText . "<br><br>";

openssl_public_encrypt($myText, $encrypted, $publicKey);
echo "Encrypted data:<br>" . $encrypted . "<br><br>";

openssl_private_decrypt($encrypted, $decrypted, $privateKey);
echo $decrypted;

In this code snippet, the $publicKey and $privateKey variables are assigned using the openssl_pkey_get_public and openssl_pkey_get_private functions, respectively, based on the paths to your public and private key files (publickey.pem and privatekey.pem). The $myText variable holds the text to be encrypted.

The script then outputs the original text, encrypts it using the public key, displays the encrypted data, and finally decrypts it using the private key, printing the decrypted result.

Reference Link: https://www.webdevsplanet.com/post/rsa-encryption-in-php

Thanks,
Gurinderpal

1 Like

Thank you @gurinderpal sir for reply.

But here I have an JWE token and JWE token has a specific structure with parts like the Initialization Vector (IV), Authentication Tag, and others. This structure isn’t directly compatible with “openssl_private_decrypt()”.

Can you please refer any other working solution ?

Thank you !!