I'm not getting the expected result (see below), due to the 00 bytes I think...
Note: I"m aware RC4 is not secure, just using this as an exercise.
[EDIT] replaced the example with a shorter one for clarity
Code: Select all
Procedure.s RC4(key.s, str.s)
Dim s.a(255)
Protected i, j = 0, x, res.s = ""
; Initialize the S array
For i = 0 To 255
s(i) = i
Next
; Key scheduling
For i = 0 To 255
j = (j + s(i) + Asc(Mid(key, (i % Len(key)) + 1, 1))) % 256
x = s(i)
s(i) = s(j)
s(j) = x
Next
i = 0
j = 0
; Stream generation and XOR with input
For y = 1 To Len(str)
i = (i + 1) % 256
j = (j + s(i)) % 256
x = s(i)
s(i) = s(j)
s(j) = x
current.s = Hex (Asc(Mid(str, y, 1)) ! s((s(i) + s(j)) % 256) )
current = RSet(current, 2, "0")
res + current.s
Next
ProcedureReturn res
EndProcedure
Procedure.s Hex2Bin(hex.s)
Protected i, result.s = ""
For i = 1 To Len(hex) Step 2
result + Chr(Val("$" + Mid(hex, i, 2)))
Next
; Debug "h2b: " + Len(hex) + " --- " + result
ProcedureReturn result
EndProcedure
Procedure.s MY_RC4(key.s, datachunk.s)
ProcedureReturn LCase(RC4(Hex2Bin(key), Hex2Bin(datachunk)))
EndProcedure
; Debug MY_RC4("75bb8b07f0ddf2898ebab9cb745911983ac6c0da7f3be675fb44c4368818bb8f", "145709a333593ddcc28bb217b5023983101269669e32037b9da0f91c77ffed7f4c")
Debug MY_RC4("75bb8b07f0ddf2898ebab9cb745911983ac6c0da7f3be675fb44c4368818bb8f", "4e434e434e434e43160000002493255b2e0000000000000000000100c04e554c4c")
;; expected: 145709a333593ddcc28bb217b5023983101269669e32037b9da0f91c77ffed7f4c
;; getting: 145709a333593ddcc2af2132cabf1d187047252a

