Sample code for 30+ languages & platforms
Java 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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.PublicKey;
import com.chilkatsoft.chilkat2.Rsa;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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.
        JsonObject json = new JsonObject();
        json.updateString("provider_key", "USER GUID HERE");
        json.updateString("menu_item", "payment");
        DateTime dt = new DateTime();
        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.setEmitCompact(true);

        // Everyware's RSA public key is at:  https://docs.everyware.com/docs/everyware-public-rsa-key
        StringBuilder sb = new StringBuilder();
        boolean bCrlf = 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);

        PublicKey pubkey = new PublicKey();
        try {
            pubkey.loadFromString(sb.getAsString());
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Rsa rsa = new Rsa();
        try {
            rsa.usePublicKey(pubkey);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            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.setEncodingMode("base64url");

        String encryptedJson = rsa.encryptStringENC(json.emit(), false);

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

        System.out.println(sbUrl.getAsString());
    }
}