Quick Search for:  in language:    
RC4,Implements,standard,algorithms,using,memo
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
SQL Stats

 Code: 45,316. lines
 Jobs: 126. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for SQL.
sp_db_paging_pr imary_key
By Marcus LeBlanc on 1/11


sp_db_paging
By Marcus LeBlanc on 1/11


Beginners pack
By hardik shah on 1/11


Inline Views
By Thivya Prabakaran on 1/6


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

RC4 stored proc functions

Print
Email
 
VB icon
Submitted on: 11/8/2002 4:14:17 AM
By: Stuart Dee  
Level: Intermediate
User Rating: Unrated
Compatibility:SQL Server 2000

Users have accessed this code 2383 times.
 
 
     Implements standard RC4 algorithms using memory tables through sql functions. No active x needed. Speed can be improved by pre generating and storing key state table.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

--**************************************
--     
-- Name: RC4 stored proc functions
-- Description:Implements standard RC4 a
--     lgorithms using memory tables through sq
--     l functions. No active x needed. Speed c
--     an be improved by pre generating and sto
--     ring key state table.
-- By: Stuart Dee
--
-- Inputs:key and data
--
-- Returns:encrypted/decrypted data stri
--     ng
--
--This code is copyrighted and has-- limited warranties.Please see http://
--     www.Planet-Source-Code.com/vb/scripts/Sh
--     owCode.asp?txtCodeId=587&lngWId;=5--for details.--**************************************
--     

CREATE FUNCTION rc4_fnEncryption
 (@p_cKey varchar(255),@p_cData varchar(255)) 
RETURNS varchar(255)
AS 
    BEGIN 
    DECLARE @stateTable TABLE (PKstate int PRIMARY key,statevalue int)
    DECLARE @loopCounter int,
    	 @dataLen int,
    	@Iindex int,
    	@jIndex int,
    	@stateValue int,
    	@stateJvalue int,
    	@KIndex int,
    	@KValue int,
    	@cipherValue int,
    	@cipherChar char(1),
    	@cipherResults varchar(255)
    SELECT @loopCounter=1,
    	 @dataLen=LEN(@p_cData)+1,
    	@Iindex=0,
    	@JIndex=0,
    	@cipherResults=''
    -- Initialise state Table
    INSERT INTO @stateTable
    (pkState,
     statevalue)
    SELECT *
    FROM dbo.rc4_fnInitialise(@p_cKey)
    WHILE @loopCounter<@dataLen
        BEGIN
        	SET @iIndex=(@iIndex+1) % 256
        	SELECT @statevalue=statevalue
        	FROM @stateTable
        	WHERE PKstate=@iIndex
        	SET @jIndex=(@jIndex+@stateValue) % 256
        	SELECT @stateJvalue=stateValue
        	FROM @stateTable
        	WHERE PKstate=@JIndex
        	UPDATE @stateTable
        	SET stateValue=@stateJvalue
        	WHERE PKstate=@IIndex
        	UPDATE @stateTable
        	SET stateValue=@stateValue
        	WHERE PKstate=@JIndex
        	SET @kIndex=(@stateValue+@stateJvalue) % 256
        	SELECT @kValue=stateValue
        	FROM @stateTable
        	WHERE PKstate=@kIndex
        	SET @cipherChar=SUBSTRING(@p_cData,@loopCounter,1)
        	SET @cipherValue=ASCII(@cipherChar) ^@kIndex
        	SET @cipherResults=@cipherResults+CHAR(@cipherValue)
        	SET @loopCounter=@loopCounter+1
    END

RETURN @cipherResults END
GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS OFF GO CREATE FUNCTION rc4_fnInitialise (@p_cKey varchar(255)) RETURNS @stateTable TABLE (PKstate int PRIMARY KEY identity(1,1),statevalue int) AS BEGIN DECLARE @LoopCounter int, @keyAscii int, @charStart int, @keyLength int, @stateIndex int, @stateValue int, @keyValue int, @swapValue Int, @swapIdxValue int DECLARE @keyTable TABLE (PKEncrypt int IDENTITY(1,1) PRIMARY key , keyvalue int) --DECLARE @stateTable TABLE (PKState int -- IDENTITY(1,1) primary key, stateValue i -- nt) SELECT @loopCounter=1, @keyLength=LEN(@p_cKey) -- populate key & state tables WHILE @loopCounter<257 BEGIN SET @charStart=(@loopCounter %@keyLength)+1 SET @keyAscii=ASCII(SUBSTRING(@p_cKey,@charStart,1)) INSERT INTO @keyTable (keyValue) VALUES (@keyAscii) INSERT INTO @stateTable (stateValue) VALUES (@loopCounter) SET @loopCounter=@loopCounter+1 END
SELECT @loopCounter=1, @stateIndex=0 WHILE @loopCounter<257 BEGIN SELECT @stateValue=stateValue FROM @stateTable WHERE pkState=@loopCounter SELECT @keyValue=keyvalue FROM @keyTable WHERE pkEncrypt=@loopCounter SET @stateIndex=(@stateIndex+@stateValue+@keyValue) % 256 SET @swapValue=@stateValue SELECT @swapIdxValue=stateValue FROM @stateTable WHERE pkState=@stateIndex UPDATE @stateTable SET stateValue=@swapIdxValue WHERE pkstate=@loopCounter UPDATE @stateTable SET stateValue=@swapValue WHERE pkstate=@stateIndex SET @loopCounter=@loopCounter+1 END
RETURN END
GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
12/28/2002 5:24:44 PM:
The code and the code presentation is 
very clean and concrete. I work with 
Oracle andI would like to change it to 
Oracle stored procedures but I am not 
sure if the datatypes are equivalent 
then probably giving different 
results.
Do you have an Oracle 
version?
Thanks,
Carmen 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/30/2002 4:32:17 AM:Stuart Dee
Hi,
It should be fairly straight 
forward to convert it to Oracle as the 
dataTypes are similar. I think oracle 
has some built in encryption you can use
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/30/2002 4:33:17 AM:Stuart Dee
It should be fairly easy to convert to 
Oracle dataTypes. Also I think Oracle 
has its own in built encryption 
functions you can use
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | SQL Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.