Code: Select all
Structure AsciiType
a.a[0]
EndStructure
#Encrypt_DEK_Size = 49
DataSection
; default encryption key
_Encrypt_DEK:
Data.a 16, 125, 235, 199, 173, 38, 21
Data.a 41, 45, 61, 44, 7, 183, 47
Data.a 18, 109, 71, 3, 19, 154, 78
Data.a 106, 9, 122, 91, 8, 34, 11
Data.a 82, 79, 25, 61, 158, 33, 111
Data.a 8, 3, 26, 16, 233, 133, 17
Data.a 26, 78, 253, 156, 227, 89, 43
EndDataSection
Procedure.s Encrypt_DEK()
Protected Key.s{#Encrypt_DEK_Size}
CopyMemory(?_Encrypt_DEK, @Key, SizeOf(Ascii) * #Encrypt_DEK_Size)
ProcedureReturn Key
EndProcedure
Procedure.s Encrypt_RC4(Text.s, Key.s = "")
Protected *TAscText.AsciiType
Protected *TAscKey.AsciiType
Protected tLen.i, kLen.i, i.i
Protected Dim rb.a(255), a.a, b.a
; text length
tLen = Len(Text)
If tLen = 0
ProcedureReturn ""
EndIf
; key length
If Len(Key) = 0
Key = Encrypt_DEK()
EndIf
; key length <= 256
kLen = Len(Key)
If kLen > 256
kLen = 256
Key = Left(Key, kLen)
EndIf
; key address
*TAscKey = @Key
a = 0
; table
For i = 0 To 255
rb(i) = i
a = (a + rb(i) + *TAscKey\a[i % kLen]) & $FF
Swap rb(i), rb(a)
Next
; text address
*TAscText = @Text
a = 0
b = 0
; encrypt/decrypt
For i = 0 To tLen - 1
a = (a + 1) & $FF
b = (b + rb(a)) & $FF
Swap rb(a), rb(b)
PokeA(*TAscText + i, *TAscText\a[i] ! rb((rb(a) + rb(b)) & $FF))
Next
; return result
ProcedureReturn Text
EndProcedure
; Example 1 (using default encryption key)
Define Text.s = Encrypt_RC4("Testing this algorithm")
Debug Text ;encrypted
Debug Encrypt_RC4(Text) ;decrypted
; Example 2 (using a password)
Define Password.s = "my password"
Define Text.s = Encrypt_RC4("Testing this algorithm", Password)
Debug Text ;encrypted
Debug Encrypt_RC4(Text, Password) ;decrypted