VB icon

RC4 Class

Email
Submitted on: 1/5/2015 2:49:00 PM
By: Lewis E. Moten III (from psc cd)  
Level: Advanced
User Rating: By 4 Users
Compatibility: ASP (Active Server Pages)
Views: 1345
 
     Applys Encryption/Decryption to strings. I think just about everyone who has seen my code knows how I love classes. This version is more "cleaned up" and thrown into a nice little class for an object oriented feeling. (If only ASP was object oriented, I would be a happy camper). It is also a little more optimized to run quicker if you change the Key/Password often.

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: RC4 Class
' Description:Applys Encryption/Decryption to strings. I think just about everyone who has seen my code knows how I love classes. This version is more "cleaned up" and thrown into a nice little class for an object oriented feeling. (If only ASP was object oriented, I would be a happy camper). It is also a little more optimized to run quicker if you change the Key/Password often.
' By: Lewis E. Moten III (from psc cd)
'**************************************

<%
Option Explicit
Dim ObjRC4
Set ObjRC4 = New clsRC4
ObjRC4.Key = "Joe"
Response.Write	"""" & ObjRC4.Crypt("hello") & """"
Set ObjRC4 = Nothing
' ------------------------------------------------------------------------------
Class clsRC4
	
	Private mStrKey
	Private mBytKeyAry(255)
	Private mBytCypherAry(255)
	
	Private Sub InitializeCypher()
		
		Dim lBytJump
		Dim lBytIndex
		Dim lBytTemp
	
		For lBytIndex = 0 To 255
		mBytCypherAry(lBytIndex) = lBytIndex
		Next
		' Switch values of Cypher arround based off of index and Key value
		lBytJump = 0
		For lBytIndex = 0 To 255
	
			' Figure index to switch
		lBytJump = (lBytJump + mBytCypherAry(lBytIndex) + mBytKeyAry(lBytIndex)) Mod 256
		
		' Do the switch
		lBytTemp					= mBytCypherAry(lBytIndex)
		mBytCypherAry(lBytIndex)	= mBytCypherAry(lBytJump)
		mBytCypherAry(lBytJump)		= lBytTemp
		
		Next
	End Sub
	
	Public Property Let Key(ByRef pStrKey)
		Dim lLngKeyLength
		Dim lLngIndex
		
		If pStrKey = mStrKey Then Exit Property
		lLngKeyLength = Len(pStrKey)
		If lLngKeyLength = 0 Then Exit Property
		mStrKey = pStrKey
		lLngKeyLength = Len(pStrKey)
		For lLngIndex = 0 To 255
		mBytKeyAry(lLngIndex) = Asc(Mid(pStrKey, ((lLngIndex) Mod (lLngKeyLength)) + 1, 1))
		Next
	End Property
	
	Public Property Get Key()
		Key = mStrKey
	End Property
	Public Function Crypt(ByRef pStrMessage)
		Dim lBytIndex
		Dim lBytJump
		Dim lBytTemp
		Dim lBytY
		Dim lLngT
		Dim lLngX
		
		' Validate data
		If Len(mStrKey) = 0 Then Exit Function
		If Len(pStrMessage) = 0 Then Exit Function
		Call InitializeCypher()
		
		lBytIndex = 0
		lBytJump = 0
		For lLngX = 1 To Len(pStrMessage)
		lBytIndex = (lBytIndex + 1) Mod 256 ' wrap index
		lBytJump = (lBytJump + mBytCypherAry(lBytIndex)) Mod 256 ' wrap J+S()
		
			' Add/Wrap those two	
		lLngT = (mBytCypherAry(lBytIndex) + mBytCypherAry(lBytJump)) Mod 256
		
		' Switcheroo
		lBytTemp					= mBytCypherAry(lBytIndex)
		mBytCypherAry(lBytIndex)	= mBytCypherAry(lBytJump)
		mBytCypherAry(lBytJump)		= lBytTemp
		lBytY = mBytCypherAry(lLngT)
			' Character Encryption ...
		Crypt = Crypt & Chr(Asc(Mid(pStrMessage, lLngX, 1)) Xor lBytY)
		Next
		
	End Function
End Class
%>


Other 63 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced 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


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.