# RC4 in Sinclair BASIC; Working around one-indexed arrays

This is a program written in Sinclair BASIC (48 BASIC) for the Sinclair ZX Spectrum. It uses a hardcoded key (null right now) as seen in the bottom and outputs a stream of integers generated with RC4. Is there any way I can improve this particularly to get around 48 BASIC using one-indexed arrays (which makes RC4 hard to do without a lot of integer additions)?

  10 READ L
20 DIM K(L)
30 FOR I=1 TO L
50 NEXT I
60 DEF FN M(A,B)=A-INT (A/B)*B
65 REM KSA
70 DIM S(256)
80 FOR I=0 TO 255
90 LET S(I+1)=I
100 NEXT I
110 LET J=0
120 FOR I=0 TO 255
130 LET T=S(I+1)+K(FN M(I,L)+1)
140 LET J=FN M(J+T,256)
150 GO SUB 250
160 NEXT I
165 REM PRGA
170 LET I=0
180 LET J=0
190 LET I=FN M(I+1, 256)
200 LET J=FN M(J+S(I+1),256)
210 GO SUB 250
220 LET T=S(I+1)+S(J+1)
230 PRINT S(FN M(T,256)+1)
240 GO TO 190
245 REM SWAP
250 LET T=S(I+1)
260 LET S(I+1)=S(J+1)
270 LET S(J+1)=T
280 RETURN
285 REM KEY
290 DATA 5
300 DATA 0,0,0,0,0


KSA is the Key Scheduling Algorithm which initializes a 256-element array S with a permutation based on the key K (an array of size L). PRGA is the Pseudo-Random Generator Algorithm which prints a single integer to the screen at a time based on the contents of S.

• Oh, this would have been so cool in the eighties! Now... why, just why? – Maarten Bodewes Mar 3 '20 at 2:15
• Maybe try and implement it in Z80 assember? That would still be kinda cool, and it is rather easy to learn. 8 bit opcodes :) – Maarten Bodewes Mar 3 '20 at 2:20