Last week we started to work with a new API of a provider. For authentication they request a JWE in the header of an https request.
We have no experience with JWE, then we started to look information about it, to devolope it with PHP.
After many tested and thanks to DinoChiesa online JWT Decoder (https://dinochiesa.github.io/jwt/), we developed a function to make it, but there're something was wrong yet. At the end Dino Chiesa helped us to fix issues and the function below now is working.
<?php
require_once("auth/key.php");
include 'vendor/autoload.php';
use phpseclib3\Crypt\PublicKeyLoader;
function jwe (){
global $payloadAuth; //put here payload that you have to use
/**************************************************/
/********************HEADER************************/
/**************************************************/
// base64 encodes the header json
$arr = array('enc' => 'A256GCM', 'alg' => 'RSA-OAEP');
$arr2 = json_encode($arr);
$encoded_header=base64url_encode($arr2);
/**************************************************/
/********************JWE KEY***********************/
/**************************************************/
$CEK=openssl_random_pseudo_bytes(32);
$encoded_EncCEK = base64url_encode(rsaEncryptionOaepSha256($publicKey, $CEK));
/**************************************************/
/********************VECTOR************************/
/**************************************************/
$iv = openssl_random_pseudo_bytes(12);
$encoded_iv = base64url_encode($iv);
/**************************************************/
/********************CYPHERTEXT********************/
$cipher = "aes-256-gcm";
$option=1;
$aad=$encoded_header;
$ciphertext=openssl_encrypt($payloadAuth, $cipher, $CEK, $option, $iv, $tag, $aad);
$encoded_ciphertext=base64url_encode($ciphertext);
/**************************************************/
/********************TAG***************************/
/**************************************************/
$encoded_tag=base64url_encode($tag);
/**************************************************/
/********************FIN***************************/
/**************************************************/
return $encoded_header . '.' . $encoded_EncCEK . '.' . $encoded_iv . '.' . $encoded_ciphertext . '.' . $encoded_tag;
}
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
function rsaEncryptionOaepSha256($publicKey, $plaintext) {
global $publicKey;
$rsa = PublicKeyLoader::load($publicKey)->withHash('sha1')->withMGFHash('sha1');
return $rsa->encrypt($plaintext);
}
?>
Could someone explain how would change the code if "enc"="A256CBC-HS512" and "alg"="RSA-OAEP-256"?
Thanks and regards, Luis
I expect that people who work with PHP can find year a completee solution for JWE authentication.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.