1
def get_prg(plaintext_size, k):
    j = 0
    S = list(range(32))
    for i in range(32):
        j = (j + S[i] + ord(k[i % len(k)])) % 32
        S[i], S[j] = S[j], S[i]
    return S


def fake_rc4(plaintext, keystream):
    i = 0
    j = 0
    i = (i + 1) % 32
    j = (j + keystream[i]) % 32
    keystream[i], keystream[j] = keystream[j], keystream[i]
    t = (keystream[i] + keystream[j]) % 32
    return t

i need at the end to return a cipher text. at the fake_rc4 I return the index of the keystream. How at the other func do I encode the text using xor on the plain text?

I use mod 32 and not 256 because of an exercise.

this is the exercise:

Write the stream cipher fake-RC4: implement a function encrypt that given a plaintext and a 32-bytes key k, returns a ciphertext encrypted with a weak variant of RC4 which we describe here.

First, implement the fake-RC4 pseudo-random generator (PRG):

It starts with i = j = 0, and to generate the next byte in the keystream it:

  • Increments i by 1 (modulo 32),
  • Increments j by the ith character of the key (modulo 32),
  • Swaps the ith character of the key with its jth character,
  • Adds the ith character of the key and its jth character, modulo 32, and returns the key's character at that index.

So for example, if the ith character of the key was 'a' (whose ASCII value is 97), and its jth character was '3' (whose ASCII value is 51), their sum would be 148. Modulo the length of the key, the result will be 148 % 32 = 20, so the pseudo random generator would return the 20th character of the key as the next byte.

Once you have the pseudo-random generator working, the rest is easy:

  • Iterate over the plaintext
  • XOR every character with the next byte of the pseudo-random generator's keystream
  • Return the result as the ciphertext!
Barmar
  • 741,623
  • 53
  • 500
  • 612
Amit Hadad
  • 35
  • 5
  • What's the point of `i = (i + 1) % 32`? Since `i` is always `0`, that's just `i = 1` – Barmar Jul 25 '21 at 22:25
  • Is there supposed to be a loop in `fake_rc4`? – Barmar Jul 25 '21 at 22:26
  • What do you mean by "I return the index of the keystream"? – Barmar Jul 25 '21 at 22:30
  • Why doesn't `fake_rc4()` use `plaintext`? – Barmar Jul 25 '21 at 22:31
  • you mean like this? `def fake_rc4(plaintext, keystream): i = 0 j = 0 S = [] for char in plaintext: i = (i + 1) % 32 j = (j + keystream[i]) % 32 keystream[i], keystream[j] = keystream[j], keystream[i] t = (keystream[i] + keystream[j]) % 32 S.append(keystream[t]) return S` – Amit Hadad Jul 26 '21 at 12:17
  • `fake_rc4` needs to be a generator, or `i` and `j` need to be global variables so they retain their values between calls. – Barmar Jul 26 '21 at 14:36

0 Answers0