Java
Chilkat2 Java
Decrypt a JWE with a Private Key
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPrivateKey, which sets the private key used to decrypt a recipient of a loaded JWE.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The private key unwraps the content-encryption key that was wrapped with the corresponding public key during encryption.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Jwe;
import com.chilkatsoft.chilkat2.PrivateKey;
public class ChilkatExample {
public static void main(String[] args) {
Jwe jwe = new Jwe();
// Load the JWE compact serialization to be decrypted.
String jweCompact = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>";
try {
jwe.loadJwe(jweCompact);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Load the recipient's private key.
PrivateKey privKey = new PrivateKey();
String content;
try {
privKey.loadPemFile("qa_data/recipient_privkey.pem");
// Set the private key used to decrypt recipient 0.
jwe.setPrivateKey(0, privKey);
content = jwe.decrypt(0, "utf-8");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println(content);
}
}