Page 2 of 3
Re: RC4
Posted: Sat Jan 15, 2005 3:42 pm
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.
Re: RC4
Posted: Sat Jan 15, 2005 3:45 pm
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.
Re: RC4
Posted: Sat Jan 15, 2005 3:47 pm
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.
Posted: Sat Jan 15, 2005 10:27 pm
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.
Posted: Mon Jan 17, 2005 3:34 pm
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
Posted: Sat Sep 13, 2008 6:56 am
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
Posted: Sat Sep 13, 2008 9:57 pm
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")
Posted: Sun Sep 14, 2008 2:38 am
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.
Posted: Sun Sep 14, 2008 7:25 am
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
Posted: Sun Sep 14, 2008 6:21 pm
by Paul
RC4 lib works with Unicode, you could give that a try...
http://www.reelmedia.org/cgi-bin/PurePr ... &sub=ASM42
Posted: Sun Sep 14, 2008 6:38 pm
by thyphoon
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.
I have found a solution on the german forum.
Thanks to answer me
,
Thy
Posted: Sun Jun 07, 2009 12:09 pm
by DoubleDutch
Have you a link to the solution you found?
Posted: Sun Jun 07, 2009 12:29 pm
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
Posted: Sun Jun 07, 2009 2:05 pm
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
Posted: Sun Jun 07, 2009 2:15 pm
by DoubleDutch
Thanks.