-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbit_flipper.py
58 lines (41 loc) · 1.88 KB
/
bit_flipper.py
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
from paddings import *
def xor(a, b):
return "".join([chr(ord(a[i]) ^ ord(b[i % len(b)])) for i in range(len(a))])
class BitFlipper:
def __init__(self, ciphertext, plaintext, block_size=16):
self._ciphertext = ciphertext
self._block_size = block_size
if len(ciphertext) % block_size != 0:
raise Exception("Invalid ciphertext length or block size")
self._plaintext = plaintext
if len(self._plaintext) != len(ciphertext):
raise Exception("Invalid plaintext length")
def get_plaintext(self):
return self._plaintext
def get_ciphertext(self):
return self._ciphertext
def flip(self, a, b):
if a not in self._plaintext:
raise Exception("Unable to find occurence of %s in plaintext" % a)
if len(a) != len(b):
raise Exception("Data length mismatch")
plaintextblock = [self._plaintext[i:i + self._block_size] for i in
range(0, len(self._plaintext), self._block_size)]
offset = 0
in_block = -1
for i in range(len(plaintextblock)):
if a in plaintextblock[i]:
in_block = i
offset = plaintextblock[i].find(a)
break
if in_block < 1 or len(a) > self._block_size:
raise Exception("Bit Flipping attack cannot change consecutive blocks")
modif = xor(a, b)
pos = (in_block-1)*self._block_size + offset
self._ciphertext = self._ciphertext[:pos] + xor(modif, self._ciphertext[pos:pos + len(a)]) + self._ciphertext[
pos + len(a):]
return self._ciphertext
def __str__(self):
raise NotImplementedError
def __repr__(self):
return "<%s plaintext=%s>" % (self.__class__.__name__, self.get_plaintext())