Java 解密JWE时出错
对JWE解密来说有点新鲜。我有一个执行JWE的服务器,它根据服务器和客户机之间共享的密钥将JWE发送给客户机 我正在使用一个Jose4j进行解密,并且得到了这个错误 java.lang.NullPointerException:尚未设置JWE的明文负载。 我使用的示例代码如本链接“接收器部分”中所示 我对服务器没有任何了解,我只是在编写客户端。我很困惑paylaod本身是否不会出现,或者该框架是否在试图解密时出错 任何调试该问题的指针都将受到赞赏 问候,,Java 解密JWE时出错,java,encryption,jwt,jose4j,jwe,Java,Encryption,Jwt,Jose4j,Jwe,对JWE解密来说有点新鲜。我有一个执行JWE的服务器,它根据服务器和客户机之间共享的密钥将JWE发送给客户机 我正在使用一个Jose4j进行解密,并且得到了这个错误 java.lang.NullPointerException:尚未设置JWE的明文负载。 我使用的示例代码如本链接“接收器部分”中所示 我对服务器没有任何了解,我只是在编写客户端。我很困惑paylaod本身是否不会出现,或者该框架是否在试图解密时出错 任何调试该问题的指针都将受到赞赏 问候,, Aravind只有在没有有效负载集的
Aravind只有在没有有效负载集的情况下,才会从
getCompactSerialization()
方法引发特定异常-getCompactSerialization()
是发送/加密端创建JWE的最后一步。如果你正在解密,你不应该叫它。也许你在某处接到一个意外的电话?否则,您使用的代码以及示例原始JWE值可能有助于解决疑难问题(和密钥,如果这只是一个测试,您可以共享它们) JWE需要两级解密才能获得纯文本负载
所以首先是JWE到JWS。
然后在验证签名后从JWS到JWT。下面的代码将实现这一点
// That other party, the receiver, can then use JsonWebEncryption to decrypt the message.
JsonWebEncryption receiverJwe = new JsonWebEncryption();
// Set the compact serialization on new Json Web Encryption object
//This is the received payload JWE payload
receiverJwe.setCompactSerialization(result.toString());
// Symmetric encryption, like we are doing here, requires that both parties have the same key.
// The key will have had to have been securely exchanged out-of-band somehow.
receiverJwe.setKey(secretKeySpec);
// Set the "alg" header, which indicates the key management mode for this JWE.
// In this example we are using the direct key management mode, which means
// the given key will be used directly as the content encryption key.
//receiverJwe.setAlgorithmHeaderValue(KeyManagementAlgorithmIdentifiers.DIRECT);
//receiverJwe.setEncryptionMethodHeaderParameter(ContentEncryptionAlgorithmIdentifiers.AES_128_CBC_HMAC_SHA_256);
// Get the message that was encrypted in the JWE. This step performs the actual decryption steps.
String jwsPayload = receiverJwe.getPlaintextString();
// And do whatever you need to do with the clear text message.
System.out.println("plaintext: " + jwsPayload);
// Create a new JsonWebSignature object
JsonWebSignature jws = new JsonWebSignature();
jws.setCompactSerialization(jwsPayload);
jws.setKey(secretKeySpec);
boolean signatureVerified = jws.verifySignature();
// Do something useful with the result of signature verification
System.out.println("JWS Signature is valid: " + signatureVerified);
// Get the payload, or signed content, from the JWS
String payload = jws.getPayload();
// Do something useful with the content
System.out.println("JWS payload: " + payload);
添加了有效负载、密钥和我得到的新错误。再次感谢你的帮助,不用担心。。谢谢。。它很有魅力。。添加上面的代码以便其他人可以使用。。