[英]Is it Possible to Encrypt a JWT token in Php And Decrypt in Javascript?
因為JWE是一種標准格式,所以從平台(PHP,Java,JS ...)發行JWE並與另一個平台一起讀取它沒有問題。
唯一的問題是
我不了解nov / jose-php,但是代碼查看和我進行的測試表明我支持一些知名的算法(用於密鑰加密的RSA1_5
/ RSA-OAEP
/ dir
和用於內容加密的A128CBC-HS256
/ A256CBC-HS512
) 。
如果在項目上使用RSA-OAEP
和A128CBC-HS256
或A256CBC-HS512
,那么您應該能夠在一側進行加密,而在另一側進行解密。
如果要在PHP上使用其他算法,例如AxxxKW
或AxxxGCM
,則應查看spomky-labs / jose PHP庫,該庫支持多種算法,壓縮等。
您還應該查看jwt.io ,其中列出了多個平台上的許多Jose實現。
專用/公用RSA密鑰轉換器
使用spomky-labs/jose
:
<?php
use Jose\Factory\JWKFactory;
$key = JWKFactory::createFromKeyFile('/path/to/your/key.pem');
// The variable $key is a JWKInterface instance that can be easily converted into a JSON object: json_encode($key)
在純PHP中:
<?php
$res = openssl_pkey_get_private($data);
if (false === $res) {
$res = openssl_pkey_get_public($data);
}
// Verify here that $res is not false.
$details = openssl_pkey_get_details($res);
//Verify here that the array $details has a key 'rsa' (could be 'ec')
$components = [
'n' => 'n',
'e' => 'e',
'd' => 'd',
'p' => 'p',
'q' => 'q',
'dp' => 'dmp1',
'dq' => 'dmq1',
'qi' => 'iqmp',
];
$key = ['kty' => 'RSA'];
foreach ($details['rsa'] as $key => $value) {
if (in_array($key, $components)) {
$key[array_search($key, $components)] = Base64Url::encode($value); // Base64 Url Safe encoding. See https://github.com/Spomky-Labs/base64url
}
}
// The variable $key is an array with the expected information
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.