• home
  • forum
  • my
  • kt
  • download
  • MS SQL RD4 Encryption

    Author: 2007-07-03 13:20:06 From:

    This is a simple RD4 encryption stored procedure.  It beats using a cast as varbinary!

    This is a simple RD4 encryption stored procedure. It's nice because it uses the same procedure to encrypt and decrypt and it beats using a cast as varbinary!



    /*--------------------------------------------------------------------------
    AUTHOR: Suman Chakrabarti
    DATE: 4/29/2003
    DESC: This is a simple RD4 Encryption algorithm
    --------------------------------------------------------------------------*/
    CREATE PROCEDURE uspEncryptDecrypt
    (
        @sKey  varchar(100),
        @sValue varchar(100),
        @sReturnVal varchar(100) output
    )
    AS

    SET QUOTED_IDENTIFIER ON

    SET ANSI_NULLS ON

    DECLARE @iKeyLen int
    DECLARE @counter int
    DECLARE @iLoopCount int

    create table #sKey ([id] int not null identity primary key, col int)
    create table #sBox ([id] int not null identity primary key, col int)

    SET @iKeyLen = Len(@sKey)
    SET @counter = 0
    -- PRINT @iKeyLen

    -- setup the Keybox ------------------------------------------------------
    WHILE @counter < @iKeyLen
    BEGIN
        INSERT INTO #sKey (col) VALUES (ASCII(SUBSTRING(@sKey, (@counter % @iKeyLen)+1, 1)))
        SET @counter = @counter + 1
    END

    SET @iLoopCount = Floor(255 / (@iKeyLen-1))

    WHILE @counter < @iLoopCount
    BEGIN
        INSERT INTO #sKey (col) SELECT col FROM #sKey WHERE [id] < @iKeyLen+1 ORDER BY [id]
        SET @counter = @counter + 1
    END
    DELETE FROM #sKey WHERE [id] > 255

    -- setup the Sandbox --------------------------------------------------------
    SET @counter = 0
    WHILE @counter < 256
    BEGIN
        INSERT INTO #sBox (col) VALUES (@counter)
        SET @counter = @counter + 1
    END

    -- Begin the encryption routine ---------------------------------------------
    DECLARE @iVal int, @tmpSwap int
    DECLARE @iValueLen int
    DECLARE @i int, @j int, @k int, @iFinal1 int, @iFinal2 int

    SET @counter = 0
    SET @iVal = 0
    WHILE @counter < 255
    BEGIN
        SELECT @iVal = (@iVal + k.col + b.col) % 256 FROM #sKey k, #sBox b WHERE k.[id] = @counter + 1
        SELECT @tmpSwap = col FROM #sBox WHERE [id] = @counter+1
        UPDATE #sBox SET col = @iVal WHERE [id] = @counter+1
        UPDATE #sBox SET col = @tmpSwap WHERE [id] = @iVal
        SET @counter = @counter + 1
    END

    SET @i = 0
    SET @j = 0
    SET @i = (@i + 1) % 256

    SELECT @j = (@j + col) % 256 FROM #sBox where [id] = @i
    SELECT @iVal = col FROM #sBox WHERE [id] = @j
    SELECT @tmpSwap = col FROM #sBox WHERE [id] = @i
    UPDATE #sBox SET col = @iVal WHERE [id] = @i
    UPDATE #sBox SET col = @tmpSwap WHERE [id] = @j

    SELECT @iFinal1 = col FROM #sBox WHERE [id] = @i
    SELECT @iFinal2 = col FROM #sBox WHERE [id] = @j

    --select CHAR(col) from #sBox
    --SELECT @k = col % 256 FROM #sBox WHERE [id] = (@iFinal1 + @iFinal2)
    -- print 'k: ' + CAST(@k as varchar(20))
    SET @k = 1

    -- [EnDe]Crypt the value ---------------------------------------------------------------
    DECLARE @cipher varchar(500)
    DECLARE @cipherBy int

    SET @counter = 1
    SET @iValueLen = Len(@sValue)
    SET @cipher = ''

    WHILE @counter < @iValueLen+1
    BEGIN
        SET @cipherby = ASCII(SUBSTRING(@sValue, @counter, 1)) ^ @k
        SET @cipher = @cipher + CHAR(@cipherby)
        SET @counter = @counter + 1
    END

    SELECT @sReturnVal = @cipher

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      NET (110)

    New

    Hot