Verfasst: 24.03.2006 13:16
hexenwerk :

Code: Alles auswählen
Procedure.l Hex2Dec(h$)
h$=UCase(h$)
For r=1 To Len(h$)
d<<4 : a$=Mid(h$,r,1)
If Asc(a$)>60
d+Asc(a$)-55
Else
d+Asc(a$)-48
EndIf
Next
ProcedureReturn d
EndProcedure
Procedure RC4Mem(Mem.l, memLen.l, key.s)
; based on source from Pille (German-Forum)
; some extension by ts-soft
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
t = (S(I) + (S(j) & 255)) & 255
y = S(t)
*Memm\b ! y
*Memm + 1
Next
EndIf
ProcedureReturn Mem
EndProcedure
Procedure.s PHP_RC4_Encode(String$,Key$)
memLen=Len(String$)
*Mem=AllocateMemory(memLen)
PokeS(*Mem,String$,memLen)
RC4Mem(*Mem, memLen, key$)
result$=""
For i=0 To memLen-1
result$+RSet(Hex(PeekB(*Mem+i)&$FF),2,"0")
Next
FreeMemory(*Mem)
ProcedureReturn result$
EndProcedure
Procedure.s PHP_RC4_Decode(String$,Key$)
l=Len(String$)/2
If l=0
ProcedureReturn
EndIf
*Mem=AllocateMemory(l)
If *Mem=0
ProcedureReturn
EndIf
For i=0 To l-1
a$=Mid(String$,i*2+1,2)
b=Hex2Dec(a$)
PokeB(*Mem+i,b)
Next
RC4Mem(*Mem,l,Key$)
String$=PeekS(*Mem,l)
FreeMemory(*Mem)
ProcedureReturn String$
EndProcedure
Key$="Test123"
String$="abcdefg"
a$=PHP_RC4_Encode(String$,Key$)
Debug a$
a$=PHP_RC4_Decode(a$,Key$)
Debug a$