Important alert: (current site time 10/21/2014 1:59:24 PM EDT)
 

VB icon

RC4 Implementation

Email
Submitted on: 12/12/2001 2:56:00 AM
By: Eric Hodges  
Level: Beginner
User Rating: By 2 Users
Compatibility: VB 5.0
Views: 13403
(About the author)
 
     Encrypt/Decrypt using the RC4 encryption scheme.
 
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 Implementation
' Description:Encrypt/Decrypt using the RC4 encryption scheme.
' By: Eric Hodges
'
' Inputs:String, Password
'
' Returns:encrypted/decrypted string
'
' Assumes:Same function encrypts and decrypts.
debug.? rc4ed("Hello World","eric")
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=29691&lngWId=1'for details.'**************************************

Function RC4ED(InString As String, password As String) As String
Dim S(0 To 255) As Integer
Dim K(0 To 255) As Integer
Dim i As Integer, j As Integer, tmp As Integer
Dim t As Integer
Dim outstring As String
For tmp = 0 To 255
S(tmp) = tmp
K(tmp) = Asc(Mid(password, 1 + (tmp Mod Len(password)), 1))
Next
For i = 0 To 255
j = (j + S(i) + K(i)) Mod 256
Swap S(i), S(j)
Next
i = 0
j = 0
outstring = ""
For tmp = 1 To Len(InString)
i = (i + 1) Mod 256
j = (j + S(i)) Mod 256
Swap S(i), S(j)
t = (S(i) + S(j)) Mod 256
outstring = outstring & Chr((mXor(S(t), Asc(Mid(InString, tmp, 1)))))
Next
RC4ED = outstring
End Function
Function mXor(i As Integer, j As Integer) As Integer
If i = j Then
mXor = j
Else
mXor = i Xor j
End If
End Function
Sub Swap(ByRef a As Integer, ByRef b As Integer)
Dim t As Integer
t = a
a = b
b = t
End Sub


Other 4 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 Beginner 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/13/2001 2:40:20 AMDavid J Midkiff (aka DJ)

If you haven't read there is a new cryptanalytic attack against RC4 developed by Scott Fluhrer, Itsik Mantin and Adi Shamir which exploits weaknesses in the key scheduling. Read about it at http://www.rsasecurity.com/rsalabs/index.html. I would highly recommend against using RC4 as it doesnt support large enough keys and is becoming more vulnerable by the day.
(If this comment was disrespectful, please report it.)

 
12/13/2001 4:39:12 AMdreamvb

I am sure I seen this code on here before.
(If this comment was disrespectful, please report it.)

 
12/13/2001 3:32:30 PMeric256

Thank you for your comments. This code is very simple and proboly has been submitted in a similar form before. I wanted to do it as a project myself and to implement the xor function and swap routines. I found the algorithm at http://www.achtung.com/crypto/rc4.html

I looked at that article and it proposed a couple of ways to help secure the key scheduling portion of the algorithm. I will probobly update this code soon with those additions. Thank you agian for your time.
(If this comment was disrespectful, please report it.)

 
1/9/2004 4:06:29 AM

Can you give an examplem in VB how to call the function. I like to use it like encript(strName,strPassword) butI receive errors like subscription out of range.
A little discription and a sample would help me using it. Thanks
email: mc@296.com
(If this comment was disrespectful, please report it.)

 
1/9/2004 7:37:15 PMEric Hodges

that would be the example usage.

debug.? rc4ed(
(If this comment was disrespectful, please report it.)

 

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.