SQL Server Stored Procedure Examples

ChilkatHOMEASPVisual BasicVB.NETC#Visual C++CMFCDelphiFoxProJavaPerlPHPPythonRubySQL ServerVBScript

SQL Server
Stored Procedure Examples

Quick Start
Encryption
File Access
IMAP
POP3
SMTP
Email Object
FTP
HTML-to-XML
HTTP
MHT
MIME
RSA Encryption
Socket
Spider
String
Tar
Upload
XML
XMP
Zip

Byte Array
RSS
Atom
Self-Extractor

Generate Psuedo-Random Data using ARC4 as a PRNG

This example demonstrates how to use the ARC4 stream encryption algorithm as a pseudo-random number generator (PRNG). This example generates the random data as hex encoded strings. The EncryptStringENC method can be replaced with EncryptBytes to generate random bytes. Note: This example uses new features available in the pre-release, or any official new version released after 17-October-2007.

Download Chilkat Crypt ActiveX

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

    DECLARE @success int

    EXEC sp_OAMethod @crypt, 'UnlockComponent', @success OUT, 'Anything for 30-day trial'
    IF @success <> 1
      BEGIN
        PRINT 'Crypt component unlock failed'
        RETURN
      END

    EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'arc4'
    EXEC sp_OASetProperty @crypt, 'KeyLength', 128

    EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, '000102030405060708090A0B0C0D0E0F', 'hex'

    EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex'

    --  We will repeatedly feed these 8-bytes of data to
    --  the ARC4 stream encryptor to generate our pseudo-random
    --  sequence.
    DECLARE @strData nvarchar(4000)

    SELECT @strData = '012345678'

    --  Set FirstChunk to 1 to initialize the ARC4 PRNG with the key.
    EXEC sp_OASetProperty @crypt, 'FirstChunk', 1
    EXEC sp_OASetProperty @crypt, 'LastChunk', 0

    DECLARE @encryptedText nvarchar(4000)

    EXEC sp_OAMethod @crypt, 'EncryptStringENC', @encryptedText OUT, @strData

    PRINT @encryptedText

    --  Set FirstChunk to 0 to continue encrypting
    --  without re-initializing the ARC4 PRNG
    EXEC sp_OASetProperty @crypt, 'FirstChunk', 0

    DECLARE @i int

    SELECT @i = 1
    WHILE @i <= 16
      BEGIN
        --  Repeatedly encrypting the same 8 bytes of data
        --  produces then pseudo-random sequence.
        EXEC sp_OAMethod @crypt, 'EncryptStringENC', @encryptedText OUT, @strData

        PRINT @encryptedText
        SELECT @i = @i + 1
      END
END
GO

 

Need a specific example? Send a request to support@chilkatsoft.com

© 2000-2007 Chilkat Software, Inc. All Rights Reserved.