Code archives/Algorithms/RC4 encryption

This code has been declared by its author to be Public Domain code.

Download source code

RC4 encryption by RepeatUntil2006
This encryption/decryption uses the RC4 algorithm. The same function encrypt and decrypt a string, with the use of a key. See the code to see a simple example.
Function adapted to BlitzMax by Wave and RepeatUntil.

Noel rewrote this function, and now it's 20 times faster than mine!! Use his! See the first comment...
Local pass$ = "mySecretKey"
Local msg$ = "Blitz Research Ltd is a software development company dedicated to bringing you the ultimate in game creation tools and utilities.Our flagship product is Blitz3D, the hit 3D games programming language used by thousands of programmers around the world.Our latest product is BlitzMax, a cross platform programming language based on BASIC, but with many weird And wonderful additions."

Print ""
Print "Message before encryption: " + msg

Print ""

Local crypt$ = RC4(msg$, pass$)

Print "Message after encryption: " + crypt

Print ""
Local decrypt$ = RC4(crypt$,pass$)
Print "Message after decryption: " + decrypt




' This function encrypts and decrypts a string with the use of a key
Function RC4$(inp$, key$)
	Local S[256] ' 255 byte Arrays
	Local K[256]

	Local i,j,t,temp,y
	Local Output$

	For i = 0 To 255
		S[i] = i
	Next

	j = 1
	For i = 0 To 255
		If j > key.length
 			j = 1
		EndIf
		K[i] = Asc(Mid(key,j,1))
		j:+ 1
	Next

	j = 0
	For i = 0 To 255 '
		j = ( j + S[i] + K[i] ) & 255
		temp = S[i]
		S[i] = S[j]
		S[j] = temp
	Next

	i = 0
	j = 0
	For Local x = 1 To Len(inp)
		i = (i + 1) & 255
		j = (j + S[i]) & 255
		temp = S[i]
		S[i] = S[j]
		S[j] = temp
		t = (  S[i] + ( S[j] & 255 )  ) & 255
		y = S[t]
		Output:+ Chr(Asc(Mid(inp,x,1)) ~ Y)
	Next
	
	Return Output$
EndFunction

Comments

N2006
Very nice work, one of the few code archive entries I've actually considered using (and I will be using this).

It's a tad slow though, so here's an optimized version that runs much faster (based on your code, of course) and produces the same results:


I imagine this optimization will benefit more than just myself.


RepeatUntil2006
Wahou, Noel, I am very impressed, this is more than 20 times faster!! So, people, please use the Noel's version.

There is something strange with your long key, though, and I am not able to have the result printed (with both functions). Works as soon as the key has a more reasonable length...


Filax2006
There is a bug when the key is made from numbers :

Try "123456" with key




RepeatUntil2006
Strange, I don't see this bug when using the key 123456 ?? Maybe other can try and report here ??


plash2008
Encrypt a block of memory (directly, no copy(s) made):



Ked2008
Why is there a URL in the new RC4 Function? What was that Local previously?

Edit: Nevermind, I got it. :)


Ked2008
I get an error when I decrypt using Noel's new code. I get a "Unhandled Memory Exception Error":
    Local S%[512+Ceil(inp.Length*.55)]
    Local i%, j%, t%, x%
    Local outbuf:Short Ptr = Short Ptr(Varptr s[512]) !!!! ON THIS LINE



Code Archives Forum