Sample code for 30+ languages & platforms
Classic ASP

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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

set jwe = Server.CreateObject("Chilkat.Jwe")

'  Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
set header = Server.CreateObject("Chilkat.JsonObject")
success = header.UpdateString("alg","RSA-OAEP-256")
success = header.UpdateString("enc","A256GCM")
success = jwe.SetProtectedHeader(header)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( jwe.LastErrorText) & "</pre>"
    Response.End
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.
set pubKey = Server.CreateObject("Chilkat.PublicKey")
success = pubKey.LoadFromFile("qa_data/recipient_pubkey.pem")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pubKey.LastErrorText) & "</pre>"
    Response.End
End If

'  Set the public key used to encrypt for recipient 0.
success = jwe.SetPublicKey(0,pubKey)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( jwe.LastErrorText) & "</pre>"
    Response.End
End If

jweCompact = jwe.Encrypt("This is the secret content.","utf-8")
If (jwe.LastMethodSuccess = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( jwe.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( jweCompact) & "</pre>"

%>
</body>
</html>