Sample code for 30+ languages & platforms
Unicode C++

Everyware API RSA Encrypt JSON

See more RSA Examples

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

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkJsonObjectW.h>
#include <CkDateTimeW.h>
#include <CkStringBuilderW.h>
#include <CkPublicKeyW.h>
#include <CkRsaW.h>

void ChilkatSample(void)
    {
    bool success = 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.
    CkJsonObjectW json;
    json.UpdateString(L"provider_key",L"USER GUID HERE");
    json.UpdateString(L"menu_item",L"payment");
    CkDateTimeW dt;
    dt.SetFromCurrentSystemTime();
    json.UpdateString(L"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.put_EmitCompact(true);

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

    CkPublicKeyW pubkey;
    success = pubkey.LoadFromString(sb.getAsString());
    if (success == false) {
        wprintf(L"%s\n",pubkey.lastErrorText());
        return;
    }

    CkRsaW rsa;
    success = rsa.UsePublicKey(pubkey);
    if (success == false) {
        wprintf(L"%s\n",rsa.lastErrorText());
        return;
    }

    // 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.put_EncodingMode(L"base64url");

    const wchar_t *encryptedJson = rsa.encryptStringENC(json.emit(),false);

    // Build the URL
    // Such as:  https://portal.everyware.com/Account/LoginMenu?data={Base64Url_encrypted_JSON}
    CkStringBuilderW sbUrl;
    sbUrl.Append(L"https://portal.everyware.com/Account/LoginMenu?data=");
    sbUrl.Append(encryptedJson);

    wprintf(L"%s\n",sbUrl.getAsString());
    }