RC4

Share your advanced PureBasic knowledge/code with the community.
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Re: RC4

Post by Max.² »

PB wrote:and it appears that Paul's lib is therefore returning the correct encrypted values.
I don't think so, though the problem is minor - Paul's lib returns 2 characters too much I think.

I can't try it, but I think the lib will return always a "00"+chr(0) at the end, with the "00" being too much.

Len(input)=Len(output)/2 is a must in stream ciphers, when the output is the hexa representation of the values.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: RC4

Post by PB »

> Paul's lib returns 2 characters too much I think

In my example it returns 4 extra characters. I've just noticed that Pille has
a memory-based routine -- don't know why I missed it before! Going crazy.
I'll play with that and see how it compares with Paul's lib then.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Max.²
Enthusiast
Enthusiast
Posts: 175
Joined: Wed Jul 28, 2004 8:38 am

Re: RC4

Post by Max.² »

PB wrote:> Paul's lib returns 2 characters too much I think

In my example it returns 4 extra characters. I've just noticed that Pille has
a memory-based routine -- don't know why I missed it before! Going crazy.
I'll play with that and see how it compares with Paul's lib then.
Can't agree to that (though I am just rationalizing). Paul's lib returns 4 charachters more than Pille's procedure, but wrong is just the last 2 characters. The "00" in the middle are correct, which Pille's output lack.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

The RC4Lib teminates the string with Hex $00

That is why in the example I posted, I did this...
Debug Left(CryptString(test,Key),Len(test)*2)

Which returns the string minus the $00

This is not a bug or mistake.
If you don't want to display the terminating $00 simply chop them off.
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Shannara wrote:If there's a vb code counterpart to this code that produces the same, you'd be my hero.
Shannara,

I have a VB translation of the RC4 code. E-mail or PM me if you're still interested.

Regards,
Eric
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Dec 25, 2004 2:37 pm

Post by thyphoon »

Hello everybody !

Thanks for this great code. But how can make the RC4 procedure unicode compatible ?
I must crypt unicode text but that don't works

Thanks
Best regards

Thyphoon
ps:excuse me for my bad english
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Dec 25, 2004 2:37 pm

Post by thyphoon »

I think there is a bug try this

Code: Select all


ProcedureDLL.s RC4(Inp.s, Key.s)
	;encoded.s = RC4("Message", "Password")
	;decoded.s = RC4(encoded, "Password")
	Protected i.l, j.l, t.l, x.l, temp.w, Y.w, Outp.s
	Dim S.w(255)
	Dim K.w(255)
	i.l = 0 : j.l = 0 : t.l = 0 : x.l = 0
	temp.w = 0 : Y.w = 0
	Outp.s = ""
	
    For i = 0 To 255
        S(i) = i
    Next
	
    j = 1
    For i = 0 To 255
        If j>Len(key)
            j = 1
        EndIf
        K(i) = Asc(Mid(key, j, 1))
        j = j + 1
    Next i
	
    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
	
    i = 0
    j = 0
    For 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)
        Outp = Outp + Chr(Asc(Mid(Inp, x, 1)) ! Y)
    Next
	ProcedureReturn Outp
EndProcedure

ProcedureDLL.s CryptText(Text.s, Key.s)
	
	ProcedureReturn RC4(Text.s, Key)
EndProcedure

ProcedureDLL.s DeCryptText(Text.s, Key.s)
	ProcedureReturn RC4(Text.s, Key)
EndProcedure

code.s="QZARM69HMA8ZKB"
Debug code
crypt.s = CryptText(code, "16384")
Debug crypt
Debug DeCryptText(crypt, "16384")
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I think there is a bug

No bug. You can't do an RC4 routine without using the memory commands,
because RC4 uses byte 0 in some conversions. As your code doesn't have
any memory commands, it's not usable for RC4 at all.
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Dec 25, 2004 2:37 pm

Post by thyphoon »

Ok thanks :? !
I search an idea to crypt text withless use memory command, because i must put the crypt text in Sqlite database, and purebasic don't support Blob field !

best regards

Thy
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

RC4 lib works with Unicode, you could give that a try...
http://www.reelmedia.org/cgi-bin/PurePr ... &sub=ASM42
Image Image
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Dec 25, 2004 2:37 pm

Post by thyphoon »

Paul wrote:RC4 lib works with Unicode, you could give that a try...
http://www.reelmedia.org/cgi-bin/PurePr ... &sub=ASM42
Thanks but i dont like user libs, Because i don't know if the libs will be updated for next purebasic version. And my application must run on Linux, MacOsX and Windows. I prefer to have all in purebasic Code. :wink:
I have found a solution on the german forum.
:D
Thanks to answer me :D ,

Thy
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Have you a link to the solution you found?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 327
Joined: Sat Dec 25, 2004 2:37 pm

Post by thyphoon »

DoubleDutch wrote:Have you a link to the solution you found?
I forget the link ! but if you want the code ...

Code: Select all

Procedure.l RC4Mem(Mem.l, memLen.l, key.s) ;rückgabe = adresse des vrschlüsselten speichers
	; RC4 Verschlüsselung >30MB/s
	Protected i.l, t.l, x.l, j.l, temp.l, y.l, l.l, *Sp.Long, *KeyP.Byte, *Memm.Byte
	If key<>""
		Dim S.l(255)
		Dim K.l(255)
		i = 0 : j = 0 : t = 0 : x = 0
		temp = 0 : y = 0
		j = 1
		l.l = Len(key)
		*Sp = @S()
		*KeyP = @key
		For i = 0 To 255
			*Sp\l = i
			*Sp + 4
			If *KeyP\b = 0
				*KeyP = @key
			EndIf
			K(i) = *KeyP\b
			*KeyP + 1
		Next i
		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
		i = 0
		j = 0
		*Memm = Mem
		For x = 0 To memLen-1
			i = (i + 1) & 255
			j = (j + S(i)) & 255
			;temp = S(i)
			;S(i) = S(j)
			;S(j) = temp
			Swap S(i), S(j)
			t = (S(i) + (S(j) & 255)) & 255
			y = S(t)
			*Memm\b ! y
			*Memm + 1
		Next
		ProcedureReturn Mem
	EndIf
	ProcedureReturn 0
EndProcedure
ProcedureDLL.s CryptText(string.s, Key.s)
	Protected strlen.l, enc.s, *codetmem
	strlen = StringByteLength(string)
	enc.s = Space(Int(strlen*1.35))
	*codetmem = RC4Mem(@string, strlen, key)
	Base64Encoder(*codetmem, strlen, @enc, strlen*1.35)
	ProcedureReturn enc
EndProcedure

ProcedureDLL.s DeCryptText(string.s, Key.s)
	Protected strlen.l, dec.s, decoutp.s, str2len.l, *decodetmem
	strlen = StringByteLength(string)
	dec.s = Space(Int(strlen*0.75))
	str2len.l = Base64Decoder(@string, strlen, @dec, strlen*0.75)
	*decodetmem = RC4Mem(@dec, str2len, key)
	decoutp.s = PeekS(*decodetmem, str2len)
	ProcedureReturn decoutp.s
EndProcedure
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I forget the link !
It's best to stick with original authors if at all possible.

http://www.purebasic.fr/german/viewtopic.php?t=11347
BERESHEIT
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Thanks. :)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Post Reply