Beispiel #1
0
def genTables():
    c = BitVector(bitstring='01100011')
    d = BitVector(bitstring='00000101')
    for i in range(0, 256):
        # For the encryption SBox
        a = BitVector(intVal=i, size=8).gf_MI(
            AES_modulus, 8) if i != 0 else BitVector(intVal=0)
        # For bit scrambling for the encryption SBox entries:
        a1, a2, a3, a4 = [a.deep_copy() for x in range(4)]
        a ^= (a1 >> 4) ^ (a2 >> 5) ^ (a3 >> 6) ^ (a4 >> 7) ^ c
        subBytesTable.append(int(a))
        # For the decryption Sbox:
        b = BitVector(intVal=i, size=8)
        # For bit scrambling for the decryption SBox entries:
        b1, b2, b3 = [b.deep_copy() for x in range(3)]
        b = (b1 >> 2) ^ (b2 >> 5) ^ (b3 >> 7) ^ d
        check = b.gf_MI(AES_modulus, 8)
        b = check if isinstance(check, BitVector) else 0
        invSubBytesTable.append(int(b))
    return subBytesTable, invSubBytesTable
def getDecryptionSubBox():
    dbox = [[0 for i in range(16)] for j in range(16)]
    modulus = BitVector(bitstring='100011011')
    n = 8
    d = BitVector(bitstring='00000101')
    for i in range(16):
        for j in range(16):
            i_s = bin(i)[2:].zfill(4)
            j_s = bin(j)[2:].zfill(4)
            bv = BitVector(bitstring=i_s + j_s)
            dboxt = bv.deep_copy()
            for k in range(8):
                bv[k] = dboxt[(k - 2) % 8] ^ dboxt[(k - 5) % 8] ^ dboxt[
                    (k - 7) % 8] ^ d[k]
            if bv.get_hex_string_from_bitvector() != '00':
                dbox[i][j] = bv.gf_MI(modulus, n)
            else:
                dbox[i][j] = BitVector(bitstring='00000000')
            dbox[i][j] = dbox[i][j].get_hex_string_from_bitvector()

    return dbox
def getEncryptionSubBox():
    sbox = [[0 for i in range(16)] for j in range(16)]
    modulus = BitVector(bitstring='100011011')
    n = 8
    c = BitVector(bitstring='01100011')
    for i in range(16):
        for j in range(16):
            if i == 0 and j == 0:
                bv = BitVector(bitstring='00000000')
                sbox[i][j] = bv
            else:
                i_s = bin(i)[2:].zfill(4)
                j_s = bin(j)[2:].zfill(4)
                bv = BitVector(bitstring=i_s + j_s)
                sbox[i][j] = bv
                sbox[i][j] = bv.gf_MI(modulus, n)
            sboxt = sbox[i][j].deep_copy()
            for k in range(8):
                sbox[i][j][k] = sboxt[k] ^ sboxt[(k - 4) % 8] ^ sboxt[
                    (k - 5) % 8] ^ sboxt[(k - 6) % 8] ^ sboxt[(k - 7) %
                                                              8] ^ c[k]
            sbox[i][j] = sbox[i][j].get_hex_string_from_bitvector()

    return sbox
a = 0
b = 0
if response.ok:	
  res = response.json()
  print(res)
  a, b = res['a'], res['b']		#Binary polynomials a and b
else:
  print(response.json())

##SOLUTION  


modulus = BitVector(bitstring = '100011011')
n = 8
a = BitVector(bitstring = '11001001')
multi_inverse = a.gf_MI(modulus, n)
  
modulus = BitVector(bitstring='100011011') # AES modulus
n = 8
a = BitVector(bitstring='11001001')
b = BitVector(bitstring='11011001')
mult = a.gf_multiply_modular(b, modulus, n)


#You need to calculate c and a_inv
#c = a(x)*b(x)
#a_inv is inverse of a
c = mult
a_inv = multi_inverse		

##END OF SOLUTION