Terms of Agreement:
By using this code, you agree to the following terms...
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.
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.
You may link to this code from another website, but ONLY if it is not wrapped in a frame.
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))
For i = 0 To 255
j = (j + S(i) + K(i)) Mod 256
Swap S(i), S(j)
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)))))
RC4ED = outstring
Function mXor(i As Integer, j As Integer) As Integer
If i = j Then
mXor = j
mXor = i Xor j
Sub Swap(ByRef a As Integer, ByRef b As Integer)
Dim t As Integer
t = a
a = b
b = t
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.)
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: firstname.lastname@example.org (If this comment was disrespectful, please report it.)
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.)