Sample code for 30+ languages & platforms
SQL Server

Transition from RandomizeKey to GenRandomBytesENC

Provides instructions for replacing deprecated RandomizeKey method calls with GenRandomBytesENC.

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', 256
    -- ...

    -- ------------------------------------------------------------------------
    -- The RandomizeKey method is deprecated:

    -- Generates and sets a random 32-byte (256-bit) secret key.
    EXEC sp_OAMethod @crypt, 'RandomizeKey', NULL

    -- ------------------------------------------------------------------------
    -- Do the equivalent using GenRandomBytesENC followed by SetEncodedKey

    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'base64'
    DECLARE @randomKeyBase64 nvarchar(4000)
    EXEC sp_OAMethod @crypt, 'GenRandomBytesENC', @randomKeyBase64 OUT, 32
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @randomKeyBase64, 'base64'

    EXEC @hr = sp_OADestroy @crypt


END
GO