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
(Delphi DLL) 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
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, Rsa, PublicKey, JsonObject, CkDateTime; ... procedure TForm1.Button1Click(Sender: TObject); var success: Boolean; json: HCkJsonObject; dt: HCkDateTime; sb: HCkStringBuilder; bCrlf: Boolean; pubkey: HCkPublicKey; rsa: HCkRsa; encryptedJson: PWideChar; sbUrl: HCkStringBuilder; begin // 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 := CkJsonObject_Create(); CkJsonObject_UpdateString(json,'provider_key','USER GUID HERE'); CkJsonObject_UpdateString(json,'menu_item','payment'); dt := CkDateTime_Create(); CkDateTime_SetFromCurrentSystemTime(dt); CkJsonObject_UpdateString(json,'date_time',CkDateTime__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 CkJsonObject_putEmitCompact(json,True); // Everyware's RSA public key is at: https://docs.everyware.com/docs/everyware-public-rsa-key sb := CkStringBuilder_Create(); bCrlf := True; CkStringBuilder_AppendLine(sb,'-----BEGIN PUBLIC KEY-----',bCrlf); CkStringBuilder_AppendLine(sb,'MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxNbflxUSWQ3XJ1N9dAoh',bCrlf); CkStringBuilder_AppendLine(sb,'k+uaiFsg3wkPi9LGS/mH8DtHBgZxKyz+oQBDtnDd9FDEo0ql7MMgCMsTAv27W5vk',bCrlf); CkStringBuilder_AppendLine(sb,'Pu0rm6zhcTeYquWEuVCS7VtVsyTATr0Z9WhqNeZlIRurovJAXl2jRDX6QeY5dayC',bCrlf); CkStringBuilder_AppendLine(sb,'ubwyG4lBWE4fCakGY6zlh+oaElK0rvblqjYoEg3dn4KPRCYGof8OFxLptHThD4cE',bCrlf); CkStringBuilder_AppendLine(sb,'T30j+utVafhO0HRyJ4iR3Pigb4GXdWBtJEEEWddZJizMkjFQkyUAoYLOT8EJ2TW3',bCrlf); CkStringBuilder_AppendLine(sb,'Tz8SvAuHBUEFcPWTSTMAG/bSp5wrYBTXaeEhx+wrYa60OruHuzgmhzKyQVuYlCNJ',bCrlf); CkStringBuilder_AppendLine(sb,'HdbnassuIRjjSNo25o4AdSlWwpGfBZjAiyEInR+KGpHdhKTxSekJxiwiXUS0UfSG',bCrlf); CkStringBuilder_AppendLine(sb,'prOpd5PzWaAR7DvjLsdmR9XffxvJCVxC735gLK7hDJKjCajDPHVDr8FSL8xMlrq0',bCrlf); CkStringBuilder_AppendLine(sb,'nKxtsHeRl1yzoGrRr12+9MiQnHtpqROTNXcXdwe3v+Vh8V5k8v8oIrcgh1+/N7Bd',bCrlf); CkStringBuilder_AppendLine(sb,'NiRsy1gFHBdu/he/KcDRT/9/acQFMPLQueGfZxUvU5As6pEONjtKX2MUg2fMF6Rc',bCrlf); CkStringBuilder_AppendLine(sb,'sQVVrLzg0g7EcuHGfuPeKfD/716MvS8NU7rX+2soijCSQv/e18PJPMVDlcMXjnup',bCrlf); CkStringBuilder_AppendLine(sb,'PPx1tStemesavFlj1okhS6UCAwEAAQ==',bCrlf); CkStringBuilder_AppendLine(sb,'-----END PUBLIC KEY-----',bCrlf); pubkey := CkPublicKey_Create(); success := CkPublicKey_LoadFromString(pubkey,CkStringBuilder__getAsString(sb)); if (success = False) then begin Memo1.Lines.Add(CkPublicKey__lastErrorText(pubkey)); Exit; end; rsa := CkRsa_Create(); success := CkRsa_ImportPublicKeyObj(rsa,pubkey); if (success = False) then begin Memo1.Lines.Add(CkRsa__lastErrorText(rsa)); Exit; end; // 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) CkRsa_putEncodingMode(rsa,'base64url'); encryptedJson := CkRsa__encryptStringENC(rsa,CkJsonObject__emit(json),False); // Build the URL // Such as: https://portal.everyware.com/Account/LoginMenu?data={Base64Url_encrypted_JSON} sbUrl := CkStringBuilder_Create(); CkStringBuilder_Append(sbUrl,'https://portal.everyware.com/Account/LoginMenu?data='); CkStringBuilder_Append(sbUrl,encryptedJson); Memo1.Lines.Add(CkStringBuilder__getAsString(sbUrl)); CkJsonObject_Dispose(json); CkDateTime_Dispose(dt); CkStringBuilder_Dispose(sb); CkPublicKey_Dispose(pubkey); CkRsa_Dispose(rsa); CkStringBuilder_Dispose(sbUrl); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.