Sample code for 30+ languages & platforms
Xojo Plugin

Load RSA Public Key from JWK Format (JSON Web Key)

See more RSA Examples

Demonstrates how to load an RSA public key from JWK (JSON Web Key) format.

Note: This example requires Chilkat v9.5.0.66 or later.

Chilkat Xojo Plugin Downloads

Xojo Plugin
Dim success As Boolean
success = False

// Note: This example requires Chilkat v9.5.0.66 or later.

// First build a JWK sample to load..
Dim json As New Chilkat.JsonObject
success = json.UpdateString("kty","RSA")
success = json.UpdateString("n","33TqqLR3eeUmDtHS89qF3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA04DDnEFGAf-kDQiNSe2ZtqC7bnIc8-KSG_qOGQIVaay4Ucr6ovDkykO5Hxn7OU7sJp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwFwnxCsU5-UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUEf0YN3_Q0auBkdbDR_ES2PbgKTJdkjc_rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1WSw")
success = json.UpdateString("e","AQAB")

// The JSON contains the following:
// { 
//   "kty": "RSA",
//   "n": "33TqqLR3eeUmDtHS89qF3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA04DDnEFGAf-kDQiNSe2ZtqC7bnIc8-KSG_qOGQIVaay4Ucr6ovDkykO5Hxn7OU7sJp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwFwnxCsU5-UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUEf0YN3_Q0auBkdbDR_ES2PbgKTJdkjc_rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1WSw",
//   "e": "AQAB",
// } 

// Note: The JSON can contain other members, such as "use", "kid", or anything else.  These will be ignored.
json.EmitCompact = False

// Show the JWK string to be loaded:
Dim jwkStr As String
jwkStr = json.Emit()

Dim pubKey As New Chilkat.PublicKey
// The LoadFromString method will automatically detect the format.
success = pubKey.LoadFromString(jwkStr)
If (success <> True) Then
    System.DebugLog(pubKey.LastErrorText)
    Return
End If

// OK.. the JWK is loaded.  It can be used in whatever way desired...

// The key can be retrieved in any other format, such as XML or PEM..
System.DebugLog(pubKey.GetXml())

// XML output:
// <RSAPublicKey>
//     <Modulus>33TqqLR3eeUmDtHS89qF3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA04DDnEFGAf+kDQiNSe2ZtqC7bnIc8+KSG/qOGQIVaay4Ucr6ovDkykO5Hxn7OU7sJp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwFwnxCsU5+UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUEf0YN3/Q0auBkdbDR/ES2PbgKTJdkjc/rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1WSw==</Modulus>
//     <Exponent>AQAB</Exponent>
// </RSAPublicKey>

// Choose PCKS1 or PCKS8 PEM format..
Dim bPreferPkcs1 As Boolean
bPreferPkcs1 = False
System.DebugLog(pubKey.GetPem(bPreferPkcs1))

// PEM output
// -----BEGIN PUBLIC KEY-----
// MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA33TqqLR3eeUmDtHS89qF
// 3p4MP7Wfqt2Zjj3lZjLjjCGDvwr9cJNlNDiuKboODgUiT4ZdPWbOiMAfDcDzlOxA
// 04DDnEFGAf+kDQiNSe2ZtqC7bnIc8+KSG/qOGQIVaay4Ucr6ovDkykO5Hxn7OU7s
// Jp9TP9H0JH8zMQA6YzijYH9LsupTerrY3U6zyihVEDXXOv08vBHk50BMFJbE9iwF
// wnxCsU5+UZUZYw87Uu0n4LPFS9BT8tUIvAfnRXIEWCha3KbFWmdZQZlyrFw0buUE
// f0YN3/Q0auBkdbDR/ES2PbgKTJdkjc/rEeM0TxvOUf7HuUNOhrtAVEN1D5uuxE1W
// SwIDAQAB
// -----END PUBLIC KEY-----