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
(PowerBuilder) JWE using A256GCMKWThis example demonstrates creating a JCE with AES GCM key wrap.
integer li_rc integer li_Success string ls_Plaintext oleobject loo_Jwe oleobject loo_JweProtHdr oleobject loo_Prng string ls_AesWrappingKey string ls_StrJwe oleobject loo_Jwe2 string ls_OriginalPlaintext // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. ls_Plaintext = "My text to enrypt" loo_Jwe = create oleobject // Use "Chilkat_9_5_0.Jwe" for versions of Chilkat < 10.0.0 li_rc = loo_Jwe.ConnectToNewObject("Chilkat.Jwe") if li_rc < 0 then destroy loo_Jwe MessageBox("Error","Connecting to COM object failed") return end if // First build the JWE Protected Header: // { // "alg": "A256GCMKW", // "kid": "18ec08e1-bfa9-4d95-b205-2b4dd1d4321d", // "tag": "kfPduVQ3T3H6vnewt--ksw", // "iv": "KkYT0GX_2jHlfqN_", // "enc": "A128CBC-HS256" // } loo_JweProtHdr = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_JweProtHdr.ConnectToNewObject("Chilkat.JsonObject") loo_JweProtHdr.AppendString("alg","A256GCMKW") // kid is optional loo_JweProtHdr.AppendString("kid","18ec08e1-bfa9-4d95-b205-2b4dd1d4321d") // tag is optional loo_JweProtHdr.AppendString("tag","kfPduVQ3T3H6vnewt--ksw") loo_JweProtHdr.AppendString("enc","A256GCM") // the iv should be 16 random chars. loo_Prng = create oleobject // Use "Chilkat_9_5_0.Prng" for versions of Chilkat < 10.0.0 li_rc = loo_Prng.ConnectToNewObject("Chilkat.Prng") loo_JweProtHdr.AppendString("iv",loo_Prng.RandomString(16,1,1,1)) loo_Jwe.SetProtectedHeader(loo_JweProtHdr) Write-Debug "JWE Protected Header: " + loo_JweProtHdr.Emit() Write-Debug "--" // Given that we have 256-bit AES, our key should be 32 bytes. // The ascii string here is 32 bytes, therefore the 2nd arg is "ascii" to use these // ascii chars directly as the key. ls_AesWrappingKey = "2baf4f730f5e4542b428593ef9cceb0e" loo_Jwe.SetWrappingKey(0,ls_AesWrappingKey,"ascii") // Encrypt and return the JWE: ls_StrJwe = loo_Jwe.Encrypt(ls_Plaintext,"utf-8") if loo_Jwe.LastMethodSuccess <> 1 then Write-Debug loo_Jwe.LastErrorText destroy loo_Jwe destroy loo_JweProtHdr destroy loo_Prng return end if // Show the JWE we just created: Write-Debug ls_StrJwe // Decrypt the JWE that was just produced. // 1) Load the JWE. // 2) Set the AES wrapping key. // 3) Decrypt. loo_Jwe2 = create oleobject // Use "Chilkat_9_5_0.Jwe" for versions of Chilkat < 10.0.0 li_rc = loo_Jwe2.ConnectToNewObject("Chilkat.Jwe") li_Success = loo_Jwe2.LoadJwe(ls_StrJwe) if li_Success <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_Jwe destroy loo_JweProtHdr destroy loo_Prng destroy loo_Jwe2 return end if // Set the AES wrap key. Important to use "ascii" loo_Jwe2.SetWrappingKey(0,ls_AesWrappingKey,"ascii") // Decrypt. ls_OriginalPlaintext = loo_Jwe2.Decrypt(0,"utf-8") if loo_Jwe2.LastMethodSuccess <> 1 then Write-Debug loo_Jwe2.LastErrorText destroy loo_Jwe destroy loo_JweProtHdr destroy loo_Prng destroy loo_Jwe2 return end if Write-Debug "original text: " Write-Debug ls_OriginalPlaintext destroy loo_Jwe destroy loo_JweProtHdr destroy loo_Prng destroy loo_Jwe2 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.