Code: Select all
;
; RC4 for PB by Pille, 16.07.2003 - Rev 2
;
;
; RC4 is a stream cipher designed by Rivest for RSA
; Data Security (now RSA Security). It is a variable
; key-size stream cipher with byte-oriented operations.
; The algorithm is based on the use of a random
; permutation. Analysis shows that the period of the
; cipher is overwhelmingly likely to be greater than
; 10^100. Eight to sixteen machine operations are required
; per output byte, and the cipher can be expected to run
; very quickly in software. Independent analysts have
; scrutinized the algorithm and it is considered secure.
;
Procedure.s RC4(Inp.s, Key.s)
;encoded.s = RC4("Message", "Password")
;decoded.s = RC4(encoded, "Password")
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
Message.s = "I'm a secret string!"
Key.s = "I'm the key!"
text.s = "Plaintext: " + Message + chr(13)
text = text + "Key: " + Key + chr(13) + chr(13)
text = text + "RC4 Encoded: " + RC4(Message,Key) + chr(13)
text = text + "RC4 Decoded: " + RC4(RC4(Message,Key),Key) + chr(13)
MessageRequester("RC4 Test:",text,0)