Sample code for 30+ languages & platforms
SQL Server

Example: Crypt2.EncryptEncoded method

Demonstrates how to call the EncryptEncoded 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
    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'

    -- Encrypt the bytes 0x00, 0x01, 0x02, ... 0x0A
    -- and return the encrypted bytes using the lowercase hex encoding.
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex_lower'
    DECLARE @encrypted nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'EncryptEncoded', @encrypted OUT, '000102030405060708090a'

    PRINT @encrypted

    -- Output:
    -- 9da2ae71a5378487114b430e5e230378

    DECLARE @decrypted nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'DecryptEncoded', @decrypted OUT, @encrypted

    PRINT @decrypted

    -- Output:
    -- 000102030405060708090a

    EXEC @hr = sp_OADestroy @crypt


END
GO