Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Everyware API RSA Encrypt JSONSee more RSA ExamplesDemonstrates how to RSA encrypt JSON using everyware.com's RSA public key. For more information, see https://docs.everyware.com/docs/access-iframe#sample-json--encrypted-payload
#include <C_CkJsonObjectW.h> #include <C_CkDateTimeW.h> #include <C_CkStringBuilderW.h> #include <C_CkPublicKeyW.h> #include <C_CkRsaW.h> void ChilkatSample(void) { BOOL success; HCkJsonObjectW json; HCkDateTimeW dt; HCkStringBuilderW sb; BOOL bCrlf; HCkPublicKeyW pubkey; HCkRsaW rsa; const wchar_t *encryptedJson; HCkStringBuilderW sbUrl; // 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. json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"provider_key",L"USER GUID HERE"); CkJsonObjectW_UpdateString(json,L"menu_item",L"payment"); dt = CkDateTimeW_Create(); CkDateTimeW_SetFromCurrentSystemTime(dt); CkJsonObjectW_UpdateString(json,L"date_time",CkDateTimeW_getAsUnixTimeStr(dt,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 CkJsonObjectW_putEmitCompact(json,TRUE); // Everyware's RSA public key is at: https://docs.everyware.com/docs/everyware-public-rsa-key sb = CkStringBuilderW_Create(); bCrlf = TRUE; CkStringBuilderW_AppendLine(sb,L"-----BEGIN PUBLIC KEY-----",bCrlf); CkStringBuilderW_AppendLine(sb,L"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxNbflxUSWQ3XJ1N9dAoh",bCrlf); CkStringBuilderW_AppendLine(sb,L"k+uaiFsg3wkPi9LGS/mH8DtHBgZxKyz+oQBDtnDd9FDEo0ql7MMgCMsTAv27W5vk",bCrlf); CkStringBuilderW_AppendLine(sb,L"Pu0rm6zhcTeYquWEuVCS7VtVsyTATr0Z9WhqNeZlIRurovJAXl2jRDX6QeY5dayC",bCrlf); CkStringBuilderW_AppendLine(sb,L"ubwyG4lBWE4fCakGY6zlh+oaElK0rvblqjYoEg3dn4KPRCYGof8OFxLptHThD4cE",bCrlf); CkStringBuilderW_AppendLine(sb,L"T30j+utVafhO0HRyJ4iR3Pigb4GXdWBtJEEEWddZJizMkjFQkyUAoYLOT8EJ2TW3",bCrlf); CkStringBuilderW_AppendLine(sb,L"Tz8SvAuHBUEFcPWTSTMAG/bSp5wrYBTXaeEhx+wrYa60OruHuzgmhzKyQVuYlCNJ",bCrlf); CkStringBuilderW_AppendLine(sb,L"HdbnassuIRjjSNo25o4AdSlWwpGfBZjAiyEInR+KGpHdhKTxSekJxiwiXUS0UfSG",bCrlf); CkStringBuilderW_AppendLine(sb,L"prOpd5PzWaAR7DvjLsdmR9XffxvJCVxC735gLK7hDJKjCajDPHVDr8FSL8xMlrq0",bCrlf); CkStringBuilderW_AppendLine(sb,L"nKxtsHeRl1yzoGrRr12+9MiQnHtpqROTNXcXdwe3v+Vh8V5k8v8oIrcgh1+/N7Bd",bCrlf); CkStringBuilderW_AppendLine(sb,L"NiRsy1gFHBdu/he/KcDRT/9/acQFMPLQueGfZxUvU5As6pEONjtKX2MUg2fMF6Rc",bCrlf); CkStringBuilderW_AppendLine(sb,L"sQVVrLzg0g7EcuHGfuPeKfD/716MvS8NU7rX+2soijCSQv/e18PJPMVDlcMXjnup",bCrlf); CkStringBuilderW_AppendLine(sb,L"PPx1tStemesavFlj1okhS6UCAwEAAQ==",bCrlf); CkStringBuilderW_AppendLine(sb,L"-----END PUBLIC KEY-----",bCrlf); pubkey = CkPublicKeyW_Create(); success = CkPublicKeyW_LoadFromString(pubkey,CkStringBuilderW_getAsString(sb)); if (success == FALSE) { wprintf(L"%s\n",CkPublicKeyW_lastErrorText(pubkey)); CkJsonObjectW_Dispose(json); CkDateTimeW_Dispose(dt); CkStringBuilderW_Dispose(sb); CkPublicKeyW_Dispose(pubkey); return; } rsa = CkRsaW_Create(); success = CkRsaW_ImportPublicKeyObj(rsa,pubkey); if (success == FALSE) { wprintf(L"%s\n",CkRsaW_lastErrorText(rsa)); CkJsonObjectW_Dispose(json); CkDateTimeW_Dispose(dt); CkStringBuilderW_Dispose(sb); CkPublicKeyW_Dispose(pubkey); CkRsaW_Dispose(rsa); 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) CkRsaW_putEncodingMode(rsa,L"base64url"); encryptedJson = CkRsaW_encryptStringENC(rsa,CkJsonObjectW_emit(json),FALSE); // Build the URL // Such as: https://portal.everyware.com/Account/LoginMenu?data={Base64Url_encrypted_JSON} sbUrl = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbUrl,L"https://portal.everyware.com/Account/LoginMenu?data="); CkStringBuilderW_Append(sbUrl,encryptedJson); wprintf(L"%s\n",CkStringBuilderW_getAsString(sbUrl)); CkJsonObjectW_Dispose(json); CkDateTimeW_Dispose(dt); CkStringBuilderW_Dispose(sb); CkPublicKeyW_Dispose(pubkey); CkRsaW_Dispose(rsa); CkStringBuilderW_Dispose(sbUrl); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.