1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| from numpy import array from PIL import Image from secret import Key
Plaintext1 = "RC4IsInteresting" Plaintext2 = "ThisIsAEasyGame" cnt = 0
class RC4(): def __init__(self, Key): self.S = [i for i in range(256)] self.K = [ord(Key[i % len(Key)])*2 for i in range(256)] self.I, self.J = 0, 0 self.KSA()
def KSA(self): for i in range(256): j = (i+self.K[i]+self.S[i]) % 256 self.S[i], self.S[j] = self.S[j], self.S[i]
def next(self): self.I = (self.I+1) % 256 self.J = (self.J+self.S[self.I]) % 256 self.S[self.J], self.S[self.I] = self.S[self.I], self.S[self.J] tmp = (self.S[self.J] + self.S[self.I]) % 256 return self.S[tmp]
class Encrypt(): def __init__(self, plain): global cnt cnt += 1 self.rc4 = RC4(Key) self.testRC4(plain) flag_file = Image.open(r"flag.png") img = array(flag_file) self.enc(img)
def testRC4(self, plain): ciphertext = 0 for i in plain: ciphertext = (ciphertext << 8)+ord(i) ^ self.rc4.next() print("ciphertext{} = {}".format(cnt, ciphertext))
def enc(self, img): a, b, _ = img.shape for x in range(0, a): for y in range(0, b): pixel = img[x, y] for i in range(0, 3): pixel[i] = pixel[i] ^ self.rc4.next() img[x][y] = pixel enc = Image.fromarray(img) enc.save("enc{}.png".format(cnt))
Encrypt(Plaintext1) Encrypt(Plaintext2)
|