Sample code for 30+ languages & platforms
SQL Server

Example: Crypt2.DecryptStringENC method

Demonstrates how to call the DecryptStringENC method.

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
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @crypt int
    EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'aes'
    EXEC sp_OASetProperty @crypt, 'CipherMode', 'cbc'
    EXEC sp_OASetProperty @crypt, 'KeyLength', 128
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, '000102030405060708090A0B0C0D0E0F', 'hex'
    EXEC sp_OAMethod @crypt, 'SetEncodedIV', NULL, '000102030405060708090A0B0C0D0E0F', 'hex'
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'

    -- Return the base64 encoded encrypted bytes
    DECLARE @encodedEncrypted nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @encodedEncrypted OUT, 'Hello World!'

    PRINT 'Encrypted: ' + @encodedEncrypted

    -- Output:
    -- Encrypted: qiq+IFhcjTkEIkZyf31V/g==

    -- Decrypt
    DECLARE @originalText nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'DecryptStringENC', @originalText OUT, @encodedEncrypted


    PRINT 'Decrypted: ' + @originalText

    -- Output:
    -- Decrypted: Hello World!

    EXEC @hr = sp_OADestroy @crypt


END
GO