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
(PureBasic) 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
IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkRsa.pb" IncludeFile "CkDateTime.pb" IncludeFile "CkPublicKey.pb" Procedure ChilkatExample() ; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. success.i ; First build the JSON to be encrypted. json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(json,"provider_key","USER GUID HERE") CkJsonObject::ckUpdateString(json,"menu_item","payment") dt.i = CkDateTime::ckCreate() If dt.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkDateTime::ckSetFromCurrentSystemTime(dt) CkJsonObject::ckUpdateString(json,"date_time",CkDateTime::ckGetAsUnixTimeStr(dt,0)) ; 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::setCkEmitCompact(json, 1) ; Everyware's RSA public key is at: https://docs.everyware.com/docs/everyware-public-rsa-key sb.i = CkStringBuilder::ckCreate() If sb.i = 0 Debug "Failed to create object." ProcedureReturn EndIf bCrlf.i = 1 CkStringBuilder::ckAppendLine(sb,"-----BEGIN PUBLIC KEY-----",bCrlf) CkStringBuilder::ckAppendLine(sb,"MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxNbflxUSWQ3XJ1N9dAoh",bCrlf) CkStringBuilder::ckAppendLine(sb,"k+uaiFsg3wkPi9LGS/mH8DtHBgZxKyz+oQBDtnDd9FDEo0ql7MMgCMsTAv27W5vk",bCrlf) CkStringBuilder::ckAppendLine(sb,"Pu0rm6zhcTeYquWEuVCS7VtVsyTATr0Z9WhqNeZlIRurovJAXl2jRDX6QeY5dayC",bCrlf) CkStringBuilder::ckAppendLine(sb,"ubwyG4lBWE4fCakGY6zlh+oaElK0rvblqjYoEg3dn4KPRCYGof8OFxLptHThD4cE",bCrlf) CkStringBuilder::ckAppendLine(sb,"T30j+utVafhO0HRyJ4iR3Pigb4GXdWBtJEEEWddZJizMkjFQkyUAoYLOT8EJ2TW3",bCrlf) CkStringBuilder::ckAppendLine(sb,"Tz8SvAuHBUEFcPWTSTMAG/bSp5wrYBTXaeEhx+wrYa60OruHuzgmhzKyQVuYlCNJ",bCrlf) CkStringBuilder::ckAppendLine(sb,"HdbnassuIRjjSNo25o4AdSlWwpGfBZjAiyEInR+KGpHdhKTxSekJxiwiXUS0UfSG",bCrlf) CkStringBuilder::ckAppendLine(sb,"prOpd5PzWaAR7DvjLsdmR9XffxvJCVxC735gLK7hDJKjCajDPHVDr8FSL8xMlrq0",bCrlf) CkStringBuilder::ckAppendLine(sb,"nKxtsHeRl1yzoGrRr12+9MiQnHtpqROTNXcXdwe3v+Vh8V5k8v8oIrcgh1+/N7Bd",bCrlf) CkStringBuilder::ckAppendLine(sb,"NiRsy1gFHBdu/he/KcDRT/9/acQFMPLQueGfZxUvU5As6pEONjtKX2MUg2fMF6Rc",bCrlf) CkStringBuilder::ckAppendLine(sb,"sQVVrLzg0g7EcuHGfuPeKfD/716MvS8NU7rX+2soijCSQv/e18PJPMVDlcMXjnup",bCrlf) CkStringBuilder::ckAppendLine(sb,"PPx1tStemesavFlj1okhS6UCAwEAAQ==",bCrlf) CkStringBuilder::ckAppendLine(sb,"-----END PUBLIC KEY-----",bCrlf) pubkey.i = CkPublicKey::ckCreate() If pubkey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkPublicKey::ckLoadFromString(pubkey,CkStringBuilder::ckGetAsString(sb)) If success = 0 Debug CkPublicKey::ckLastErrorText(pubkey) CkJsonObject::ckDispose(json) CkDateTime::ckDispose(dt) CkStringBuilder::ckDispose(sb) CkPublicKey::ckDispose(pubkey) ProcedureReturn EndIf rsa.i = CkRsa::ckCreate() If rsa.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkRsa::ckImportPublicKeyObj(rsa,pubkey) If success = 0 Debug CkRsa::ckLastErrorText(rsa) CkJsonObject::ckDispose(json) CkDateTime::ckDispose(dt) CkStringBuilder::ckDispose(sb) CkPublicKey::ckDispose(pubkey) CkRsa::ckDispose(rsa) ProcedureReturn EndIf ; 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::setCkEncodingMode(rsa, "base64url") encryptedJson.s = CkRsa::ckEncryptStringENC(rsa,CkJsonObject::ckEmit(json),0) ; Build the URL ; Such as: https://portal.everyware.com/Account/LoginMenu?data={Base64Url_encrypted_JSON} sbUrl.i = CkStringBuilder::ckCreate() If sbUrl.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkStringBuilder::ckAppend(sbUrl,"https://portal.everyware.com/Account/LoginMenu?data=") CkStringBuilder::ckAppend(sbUrl,encryptedJson) Debug CkStringBuilder::ckGetAsString(sbUrl) CkJsonObject::ckDispose(json) CkDateTime::ckDispose(dt) CkStringBuilder::ckDispose(sb) CkPublicKey::ckDispose(pubkey) CkRsa::ckDispose(rsa) CkStringBuilder::ckDispose(sbUrl) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.