Sample code for 30+ languages & platforms
B4X

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 B4X Downloads

B4X
Dim success As Boolean = False

Dim jwe As ChilkatJwe
jwe.Initialize

'  Load the JWE compact serialization to be decrypted.
Dim jweCompact As String = "eyJhbGciOiJBMjU2S1ciLCJlbmMiOiJBMjU2R0NNIn0.<...>.<iv>.<ciphertext>.<tag>"
success = jwe.LoadJwe(jweCompact)
If success = False Then
    Log(jwe.LastErrorText)
    Return
End If


'  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.
Dim privKey As ChilkatPrivateKey
privKey.Initialize("privKey")
success = privKey.LoadPemFile("qa_data/recipient_privkey.pem")
If success = False Then
    Log(privKey.LastErrorText)
    Return
End If


'  Set the private key used to decrypt recipient 0.
success = jwe.SetPrivateKey(0, privKey)
If success = False Then
    Log(jwe.LastErrorText)
    Return
End If


Dim content As String = jwe.Decrypt(0, "utf-8")
If jwe.LastMethodSuccess = False Then
    Log(jwe.LastErrorText)
    Return
End If

Log(content)