pythonbitinitialization-vector

Generate new IV in Python


I have a list of 100 bit, for example:

X=''.join([random.choice(['0', '1']) for i in range(100)])

X=[0,1,0,1,1...]

At the beginning of the cycle, I have been generate random initialization vector with n length:

Xlen=5
IV=''.join([random.choice(['0', '1']) for i in range(Xlen)])

The problem is I need to generate new IV based on the previous X[n] and IV[n] using the bit addition function. Note that n=5.

For example X[0]=[0,1,0,0,1] and IV[0]=[1,0,1,0,1]. The new IV should be updated to X[0]+IV[0]=IV[1] which will produce the result of [1,1,1,1,0]. X[0] and IV[0] is the list of bit and initialization vector at cycle 0. And operation of generating new IV will be continuously repeated for the next n=5 until n=100.

Please help me to solve this issue. Thank you.


Solution

  • I think what you want is a function that XORs two lists of integers (the IV and part of the data) and generates the XORed version of it. The function can be implemented using the operator ^ in python

    I am using here the 'bits' as integers, but if you need to use strings, you can just convert each char using int() before calling the function

    def get_new_iv_value(prev_iv, X):
        new_iv = [x ^ y for (x,y) in zip(prev_iv, X)]
        return new_iv
    

    Here is one example of how to use this function to calculate a new IV for each 5 elements of X

    # Create X with 10 'bits' (integers with values 0 or 1)
    X = [random.randint(0,1) for i in range(10)]
    
    # Set initial IV with size 5
    IV = [1,0,1,0,1]
    
    # This will calculate one new IV for each 5 elements in X
    for n in range(0, 100, 5): 
        IV = get_new_iv_value(IV, X[n:n+5])
    

    The result is this:

    IV[0] = [1, 0, 1, 0, 1]
    X = [1, 0, 0, 0, 0, 0, 1, 1, 0, 1]
    IV[1] = IV[0] + X[0:5]
    [0, 0, 1, 0, 1] = [1, 0, 1, 0, 1] + [1, 0, 0, 0, 0]
    IV[2] = IV[1] + X[5:10]
    [0, 1, 0, 0, 0] = [0, 0, 1, 0, 1] + [0, 1, 1, 0, 1]