Sample code for 30+ languages & platforms
SQL Server

Encrypt StringBuilder Content to a JWE

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.EncryptSb, which encrypts content supplied in a StringBuilder and writes the resulting JWE into another StringBuilder.

Background. JWE (JSON Web Encryption) protects content with a content-encryption algorithm (the header enc value) while the content-encryption key is managed by the key-management algorithm (the header alg value). This example uses AES key wrapping with a symmetric wrapping key.

Chilkat SQL Server Downloads

SQL Server
-- 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

    --  Set the JWE protected header, specifying the key-management algorithm (alg) and the content-
    --  encryption algorithm (enc).
    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

    --  Encrypt content supplied in a StringBuilder, writing the resulting JWE into another StringBuilder.
    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 @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 @sbContent
    EXEC @hr = sp_OADestroy @sbJwe


END
GO