Code: Streaming RC4 implementation in VB.NET

I posted this to Planet Source Code and thought some of you might be

interested in looking at it.

Of course, if anyone feels inspired to visit

http://www.*-*-*.com/

for my code, I would greatly appreciate it. I plan on winning that contest,

one of these months! :) Fans of sample projects can find a ZIP file there

containing a very simple implementation example.

The nice thing about the class, I think, is that since it works byte-by-byte

instead of at a string level it can be used for really big files.

Thanks to all the VB.NET designers, too, for finally giving us assignment

operators (+=, *=, etc) and "return (x)"! The framework is slightly

intimidating at first, but VB7 really is feeling more and more like a

bigger, better home.

--- Begin Code ---

Option Explicit On

Option Strict Off

Public Class clsRC4

Dim arrState(255) As Short

Dim arrKey(255) As Short

Public Sub Init(ByVal strKey As String)

Dim temp As Short

Dim i, j As Short

j = 0

For i = 0 To 255

j = j + 1

If j > Len(strKey) Then j = 1

arrKey(i) = Asc(Mid(strKey, j, 1))

Next i

For i = 0 To 255

arrState(i) = i

Next i

j = 0

For i = 0 To 255

j = (j + arrState(i) + arrKey(i)) Mod 256

temp = arrState(i)

arrState(i) = arrState(j)

arrState(j) = temp

Next i

End Sub

Public Function Crypt(ByVal bytSource As Byte) As Byte

Dim temp As Short

Dim i, j, k As Short

i = (i + 1) Mod 256

j = (j + arrState(i)) Mod 256

temp = arrState(i)

arrState(i) = arrState(j)

arrState(j) = temp

k = arrState((arrState(i) + arrState(j)) Mod 256)

Return (bytSource Xor k)

End Function

End Class

--- End Code ---