Sample code for 30+ languages & platforms
VB.NET

Encrypt a JWE with a Recipient Public Key

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPublicKey, which sets the public key used to encrypt for a recipient. The example uses RSA-OAEP-256 key management with AES-256-GCM content encryption.

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. In public-key JWE, the content-encryption key is wrapped for the recipient using their public key, so only the holder of the matching private key can decrypt.

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

Dim jwe As New Chilkat.Jwe

'  Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
Dim header As New Chilkat.JsonObject
header.UpdateString("alg","RSA-OAEP-256")
header.UpdateString("enc","A256GCM")
success = jwe.SetProtectedHeader(header)
If (success = False) Then
    Debug.WriteLine(jwe.LastErrorText)
    Exit Sub
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 public key.
Dim pubKey As New Chilkat.PublicKey
success = pubKey.LoadFromFile("qa_data/recipient_pubkey.pem")
If (success = False) Then
    Debug.WriteLine(pubKey.LastErrorText)
    Exit Sub
End If


'  Set the public key used to encrypt for recipient 0.
success = jwe.SetPublicKey(0,pubKey)
If (success = False) Then
    Debug.WriteLine(jwe.LastErrorText)
    Exit Sub
End If


Dim jweCompact As String = jwe.Encrypt("This is the secret content.","utf-8")
If (jwe.LastMethodSuccess = False) Then
    Debug.WriteLine(jwe.LastErrorText)
    Exit Sub
End If

Debug.WriteLine(jweCompact)