Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jvdsn
GitHub Repository: jvdsn/crypto-attacks
Path: blob/master/attacks/cbc/bit_flipping.py
2589 views
1
def attack(iv, c, pos, p, p_):
2
"""
3
Replaces the original plaintext with a new plaintext at a position in the ciphertext.
4
:param iv: the initialization vector
5
:param c: the ciphertext
6
:param pos: the position to modify at
7
:param p: the original plaintext
8
:param p_: the new plaintext
9
:return: a tuple containing the modified initialization vector and the modified ciphertext
10
"""
11
iv_ = bytearray(iv)
12
c_ = bytearray(c)
13
for i in range(len(p)):
14
if pos + i < 16:
15
iv_[pos + i] = iv[pos + i] ^ p[i] ^ p_[i]
16
else:
17
c_[pos + i - 16] = c[pos + i - 16] ^ p[i] ^ p_[i]
18
19
return iv_, c_
20
21