Skip to content Skip to sidebar Skip to footer

Translating Python Code For Rc4 To Pascal

I'm attempting to translate RC4 from Python to Pascal. This is the working Python code (not written by me): def KSA(key): key_length = len(key) S = list(range(256)) j =

Solution 1:

Strings in Pascal are generally one-based (with the notable exception of the Delphi mobile compilers). I get the correct output if I change:

j := (j + S[i] + ord(key[i mod key_length])) mod 256;
...
cipher[i] := (keystream[i] xor ord(plaintext[i]));

to:

j := (j + S[i] + ord(key[i mod key_length + 1])) mod 256; // note: + 1
...
cipher[i] := (keystream[i] xorord(plaintext[i + 1]));    // note: + 1

If I do (e.g. in Delphi or FreePascal):

key := 'Secret';
plaintext := 'Attack at dawn';

...    

for I := 0 to Length(plaintext) - 1 do
  Write(Format('%.2x', [Cipher[I]]));
Writeln;

I get the cipher as:

45A01F645FC35B383552544B9BF5

Post a Comment for "Translating Python Code For Rc4 To Pascal"