SQL Server
SQL Server
Set a Per-Recipient JWE Header
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetRecipientHeader, which sets the per-recipient unprotected header for a recipient index.
Background. Per-recipient headers carry recipient-specific values such as a key id (
kid), and are used with the JSON serialization forms that support multiple recipients.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
DECLARE @jwe int
EXEC @hr = sp_OACreate 'Chilkat.Jwe', @jwe OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Protected header: AES key-wrap with AES-256-GCM content encryption.
DECLARE @header int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @header OUT
EXEC sp_OAMethod @header, 'UpdateString', @success OUT, 'alg', 'A256KW'
EXEC sp_OAMethod @header, 'UpdateString', @success OUT, 'enc', 'A256GCM'
EXEC sp_OAMethod @jwe, 'SetProtectedHeader', @success OUT, @header
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jwe, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jwe
EXEC @hr = sp_OADestroy @header
RETURN
END
-- The 256-bit key-wrapping key (base64) for recipient index 0. In production, obtain the key from a
-- secure source rather than hard-coding it.
DECLARE @base64Key nvarchar(4000)
SELECT @base64Key = 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU='
EXEC sp_OAMethod @jwe, 'SetWrappingKey', @success OUT, 0, @base64Key, 'base64'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jwe, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jwe
EXEC @hr = sp_OADestroy @header
RETURN
END
-- Set the per-recipient unprotected header for recipient 0, for example a key id used to identify
-- which key a recipient should use.
DECLARE @recipHeader int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @recipHeader OUT
EXEC sp_OAMethod @recipHeader, 'UpdateString', @success OUT, 'kid', 'recipient-key-1'
EXEC sp_OAMethod @jwe, 'SetRecipientHeader', @success OUT, 0, @recipHeader
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jwe, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jwe
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @recipHeader
RETURN
END
DECLARE @sbContent int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbContent OUT
EXEC sp_OAMethod @sbContent, 'Append', @success OUT, 'This is the secret content.'
DECLARE @sbJwe int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJwe OUT
EXEC sp_OAMethod @jwe, 'EncryptSb', @success OUT, @sbContent, 'utf-8', @sbJwe
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jwe, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jwe
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @recipHeader
EXEC @hr = sp_OADestroy @sbContent
EXEC @hr = sp_OADestroy @sbJwe
RETURN
END
EXEC sp_OAMethod @sbJwe, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @jwe
EXEC @hr = sp_OADestroy @header
EXEC @hr = sp_OADestroy @recipHeader
EXEC @hr = sp_OADestroy @sbContent
EXEC @hr = sp_OADestroy @sbJwe
END
GO