# 128 bit AES CBC bit flipping on second block

I have a 128 bit AES cipher in CBC Mode with the IV prepended to the message. I am aware of the security issues here, but that is exactly the point of my example. I would like to change the plain text message via bit-flipping, which I have partially implemented in the example below:

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

key = b'WOAH, SO SECRET!'
block_size = 16 # in bytes (128 bit)

def encrypt_data(data):
iv = get_random_bytes(block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + enc

def decrypt_data(data):
iv = data[:block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)

# 128 bit blocks for better visualization
plain = b'Hello everybody!' \
b' This is a very ' \
b'cool message,wow'

wanted = b'THIS CAN CHANGE!' \
b' This is a very ' \
b'cool message,wow'

cipher_text = bytearray(encrypt_data(plain))

for block in range(len(plain)//block_size):
for i in range(block_size):
block_idx = (block*block_size)+i
xor_byte = plain[block_idx] ^ wanted[block_idx]

cipher_text[block_idx] ^= xor_byte

result = decrypt_data(bytes(cipher_text))
print(result)


Above code outputs:

b'THIS CAN CHANGE! This is a very cool message,wow'


Which is exactly what I want!

However, I only ever managed to change the first block sucessfully, by changing the preceding block of the cipher message (in this case IV, which does not affect the rest of the message). If I modify my example with a wanted message like that:

wanted = b'Hello everybody!' \
b'THIS CAN CHANGE ' \
b'cool message,wow'


My code outputs:

b'g\x114\xb4\xc4\xd6\xd6\x16\xcf\x88\x87\x99\xea@\xe4\x11THIS CAN CHANGE cool message,wow'


I understand, that this happens due to the fact of using CBC mode and the impact of blocks on each other as described here. But what exactly am I missing here? How can I change my message to the desired output after decryption?

b'Hello everybody!THIS CAN CHANGE cool message,wow'


I'd appreciate any push in the right direction.