Sample code for 30+ languages & platforms
SQL Server

ARC4 PRNG (Pseudo Random Number Generator)

See more Encryption Examples

Uses the ARC4 stream encryption algorithm as a pseudo random number generator.

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

    -- Set the encryption algorithm to ARC4:	
    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'arc4'

    -- We want the encrypted output to be a hex-encoded string.
    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex'

    DECLARE @key nvarchar(4000)
    SELECT @key = '000102030405060708090A0B0C0D0E0F'
    DECLARE @data nvarchar(4000)
    SELECT @data = '12345678'

    -- Key length is 128 bits in this example.
    EXEC sp_OASetProperty @crypt, 'KeyLength', 128
    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, @key, 'hex'

    -- Generate 16 "random" 8-byte blocks, encoded as hex strings.
    -- This example will generate the identical output each time
    -- it is run.
    DECLARE @cipherHex nvarchar(4000)

    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= 15
      BEGIN
        EXEC sp_OAMethod @crypt, 'EncryptStringENC', @cipherHex OUT, @data

        PRINT @data

        PRINT @cipherHex
        SELECT @data = @cipherHex
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @crypt


END
GO