Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.0.0+

Everyware API RSA Encrypt JSON

See more RSA Examples

Demonstrates how to RSA encrypt JSON using everyware.com's RSA public key.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  This example assumes the Chilkat API to have been previously unlocked.
'  See Global Unlock Sample for sample code.

'  First build the JSON to be encrypted.
Dim json As ChilkatJsonObject
json.Initialize
json.UpdateString("provider_key", "USER GUID HERE")
json.UpdateString("menu_item", "payment")
Dim dt As ChilkatDateTime
dt.Initialize
dt.SetFromCurrentSystemTime
json.UpdateString("date_time", dt.GetAsUnixTimeStr(False))

'  This build JSON like the following:

'  {
'      "provider_key": "USER GUID HERE",
'      "menu_item": "payment",
'      "date_time": "1588163411"
'  }

'  When we sign, we'll want to sign the most compact JSON possible
json.EmitCompact = True

'  Everyware's RSA public key is at:  https://docs.everyware.com/docs/everyware-public-rsa-key
Dim sb As ChilkatStringBuilder
sb.Initialize
Dim bCrlf As Boolean = True
sb.AppendLine("-----BEGIN PUBLIC KEY-----", bCrlf)
sb.AppendLine("MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxNbflxUSWQ3XJ1N9dAoh", bCrlf)
sb.AppendLine("k+uaiFsg3wkPi9LGS/mH8DtHBgZxKyz+oQBDtnDd9FDEo0ql7MMgCMsTAv27W5vk", bCrlf)
sb.AppendLine("Pu0rm6zhcTeYquWEuVCS7VtVsyTATr0Z9WhqNeZlIRurovJAXl2jRDX6QeY5dayC", bCrlf)
sb.AppendLine("ubwyG4lBWE4fCakGY6zlh+oaElK0rvblqjYoEg3dn4KPRCYGof8OFxLptHThD4cE", bCrlf)
sb.AppendLine("T30j+utVafhO0HRyJ4iR3Pigb4GXdWBtJEEEWddZJizMkjFQkyUAoYLOT8EJ2TW3", bCrlf)
sb.AppendLine("Tz8SvAuHBUEFcPWTSTMAG/bSp5wrYBTXaeEhx+wrYa60OruHuzgmhzKyQVuYlCNJ", bCrlf)
sb.AppendLine("HdbnassuIRjjSNo25o4AdSlWwpGfBZjAiyEInR+KGpHdhKTxSekJxiwiXUS0UfSG", bCrlf)
sb.AppendLine("prOpd5PzWaAR7DvjLsdmR9XffxvJCVxC735gLK7hDJKjCajDPHVDr8FSL8xMlrq0", bCrlf)
sb.AppendLine("nKxtsHeRl1yzoGrRr12+9MiQnHtpqROTNXcXdwe3v+Vh8V5k8v8oIrcgh1+/N7Bd", bCrlf)
sb.AppendLine("NiRsy1gFHBdu/he/KcDRT/9/acQFMPLQueGfZxUvU5As6pEONjtKX2MUg2fMF6Rc", bCrlf)
sb.AppendLine("sQVVrLzg0g7EcuHGfuPeKfD/716MvS8NU7rX+2soijCSQv/e18PJPMVDlcMXjnup", bCrlf)
sb.AppendLine("PPx1tStemesavFlj1okhS6UCAwEAAQ==", bCrlf)
sb.AppendLine("-----END PUBLIC KEY-----", bCrlf)

Dim pubkey As ChilkatPublicKey
pubkey.Initialize
success = pubkey.LoadFromString(sb.GetAsString)
If success = False Then
    Log(pubkey.LastErrorText)
    Return
End If


Dim rsa As ChilkatRsa
rsa.Initialize
success = rsa.UsePublicKey(pubkey)
If success = False Then
    Log(rsa.LastErrorText)
    Return
End If


'  We probably need a base64Url encoded encrypted key.
'  Using straight-up base64 would potenially include chars that are not URL safe (i.e. have special meanings in URLs)
rsa.EncodingMode = "base64url"

Dim encryptedJson As String = rsa.EncryptStringENC(json.Emit, False)

'  Build the URL
'  Such as:  https://portal.everyware.com/Account/LoginMenu?data={Base64Url_encrypted_JSON}
Dim sbUrl As ChilkatStringBuilder
sbUrl.Initialize
sbUrl.Append("https://portal.everyware.com/Account/LoginMenu?data=")
sbUrl.Append(encryptedJson)

Log(sbUrl.GetAsString)