1

I need to implement a MonteCarlo simulation to an AES to observe the effect of diffusion (if I change a single bit of the plaintext, then half of the bits in the ciphertext should change)... I think I did everything correct here but it says syntax error in the definition of the aes ( aes = AES.new(key, AES.MODE_ECB) ). Where did I make a mistake? Thanks in advance for your help c:

import numpy as np
import random
from Cryptodome.Cipher import AES



def flip_bit(text, ibit):
    flipped_text = bytearray(text) 
    flipped_text[ibit//8] ^= 1 << (ibit%8) 
    return bytes(flipped_text)

def text_distance(textA, textB):
    AxorB = bytes(a ^ b for (a, b) in zip(textA, textB))
    distance = sum([bin(byte).count('1') for byte in AxorB])
    return distance

def mcs_diffusion(key_length):
    
    assert key_length in AES.key_size
    
 
    key = bytes(random.randint( key_length)
    aes = AES.new(key, AES.MODE_ECB)             
   

    x = bytes(random.randint(AES.block_size))
    x1 = flip_bit(x, random.randint(8*AES.block_size))

    y = aes.encrypt(x)
    y1 = aes.encrypt(x1)
    distance = text_distance(y, y1)
    
    return distance


N = 10000 
key_length = 16

results = np.zeros(N)
for i in range(N):
    results[i] = mcs_diffusion(key_length)
    
print('average: {}'.format(np.mean(results)))
print('min, max: {}, {}'.format(min(results), max(results)))

Logan
  • 23
  • 2

0 Answers0