Exemple #1
0
def mix_cols(state):
    mod = BitVector(bitstring='100011011')
    bv2 = BitVector(hexstring='2')
    bv3 = BitVector(hexstring='3')
    for j in xrange(0, 4): # to select col
        s0 = state[0][j]
        s1 = state[1][j]
        s2 = state[2][j]
        s3 = state[3][j]

        sp0 = bv2.gf_multiply_modular(s0, mod, 8) ^ bv3.gf_multiply_modular(s1, mod, 8) ^ s2 ^ s3 
        sp1 = s0 ^ bv2.gf_multiply_modular(s1, mod, 8) ^ bv3.gf_multiply_modular(s2, mod, 8) ^ s3
        sp2 = s0 ^ s1 ^ bv2.gf_multiply_modular(s2, mod, 8) ^ bv3.gf_multiply_modular(s3, mod, 8)
        sp3 = bv3.gf_multiply_modular(s0, mod, 8) ^ s1 ^ s2 ^ bv2.gf_multiply_modular(s3, mod, 8)

        state[0][j] = sp0
        state[1][j] = sp1
        state[2][j] = sp2
        state[3][j] = sp3
Exemple #2
0
def mix_cols(state):
    mod = BitVector(bitstring='100011011')
    bv2 = BitVector(hexstring='2')
    bv3 = BitVector(hexstring='3')
    for j in xrange(0, 4):  # to select col
        s0 = state[0][j]
        s1 = state[1][j]
        s2 = state[2][j]
        s3 = state[3][j]

        sp0 = bv2.gf_multiply_modular(s0, mod, 8) ^ bv3.gf_multiply_modular(
            s1, mod, 8) ^ s2 ^ s3
        sp1 = s0 ^ bv2.gf_multiply_modular(
            s1, mod, 8) ^ bv3.gf_multiply_modular(s2, mod, 8) ^ s3
        sp2 = s0 ^ s1 ^ bv2.gf_multiply_modular(
            s2, mod, 8) ^ bv3.gf_multiply_modular(s3, mod, 8)
        sp3 = bv3.gf_multiply_modular(
            s0, mod, 8) ^ s1 ^ s2 ^ bv2.gf_multiply_modular(s3, mod, 8)

        state[0][j] = sp0
        state[1][j] = sp1
        state[2][j] = sp2
        state[3][j] = sp3
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

#check result of part a
endpoint = '{}/{}/{}/{}'.format(API_URL, "mult", my_id, c)
response = requests.put(endpoint) 	
print(response.json())
Exemple #4
0
def decrypt(inputfile, outputfile):
    f = open('decrypted_hex.txt', 'a')
    temp_shift = [0] * 4
    key = get_encryption_key()
    key_words = gen_key_schedule_128(key)
    _, invSubBytesTable = genTables()
    bv = BitVector(filename=inputfile)
    statearray = [[0 for x in range(4)] for x in range(4)]
    FILEOUT = open(outputfile, 'ab')
    while bv.more_to_read:
        bitvec = bv.read_bits_from_file(128)
        if len(bitvec) > 0:
            if len(bitvec) != 128:
                bitvec.pad_from_right(128 - len(bitvec))

            # Filling in statearray and XORing
            for i in range(4):
                for j in range(4):
                    statearray[i][j] = bitvec[32 * i + 8 * j:32 * i + 8 *
                                              (j + 1)]
                    statearray[i][j] ^= key_words[-(4 - i)][8 * j:8 + (8 * j)]

            for round_num in range(10, 0, -1):
                # Inverse ShiftRows
                for i in range(1, 4):
                    for j in range(0, 4):
                        temp_shift[(j + i) % 4] = statearray[j][i]
                    for j in range(0, 4):
                        statearray[j][i] = temp_shift[j]

                # Inverse SubBytes
                for i in range(4):
                    for j in range(4):
                        statearray[i][j] = BitVector(
                            intVal=invSubBytesTable[int(statearray[i][j])])

                # Add round_num Key
                for i in range(4):
                    for j in range(4):
                        statearray[i][j] ^= key_words[(4 * (round_num - 1)) +
                                                      i][8 * j:8 + (8 * j)]

                # Inverse ColumnMixing
                if round_num != 1:
                    zeroE = BitVector(bitstring='00001110')
                    zeroB = BitVector(bitstring='00001011')
                    zeroD = BitVector(bitstring='00001101')
                    zero9 = BitVector(bitstring='00001001')
                    for i in range(4):
                        temp = (zeroE.gf_multiply_modular(statearray[i][0], AES_modulus, 8)) ^ \
                               (zeroB.gf_multiply_modular(statearray[i][1], AES_modulus, 8)) ^ \
                               (zeroD.gf_multiply_modular(statearray[i][2], AES_modulus, 8)) ^ \
                               (zero9.gf_multiply_modular(statearray[i][3], AES_modulus, 8))

                        temp1 = (zeroE.gf_multiply_modular(statearray[i][1], AES_modulus, 8)) ^ \
                                (zeroB.gf_multiply_modular(statearray[i][2], AES_modulus, 8)) ^ \
                                (zeroD.gf_multiply_modular(statearray[i][3], AES_modulus, 8)) ^ \
                                (zero9.gf_multiply_modular(statearray[i][0], AES_modulus, 8))

                        temp2 = (zeroE.gf_multiply_modular(statearray[i][2], AES_modulus, 8)) ^ \
                                (zeroB.gf_multiply_modular(statearray[i][3], AES_modulus, 8)) ^ \
                                (zeroD.gf_multiply_modular(statearray[i][0], AES_modulus, 8)) ^ \
                                (zero9.gf_multiply_modular(statearray[i][1], AES_modulus, 8))

                        temp3 = (zeroE.gf_multiply_modular(statearray[i][3], AES_modulus, 8)) ^ \
                                (zeroB.gf_multiply_modular(statearray[i][0], AES_modulus, 8)) ^ \
                                (zeroD.gf_multiply_modular(statearray[i][1], AES_modulus, 8)) ^ \
                                (zero9.gf_multiply_modular(statearray[i][2], AES_modulus, 8))

                        statearray[i][0] = temp
                        statearray[i][1] = temp1
                        statearray[i][2] = temp2
                        statearray[i][3] = temp3

            for i in range(4):
                for j in range(4):
                    statearray[i][j].write_to_file(FILEOUT)
                    f.write(statearray[i][j].get_bitvector_in_hex())
    f.close()
    FILEOUT.close()
    return
Exemple #5
0
def encrypt(inputfile, outputfile):
    f = open('encrypted_hex.txt', 'a')
    key = get_encryption_key()
    key_words = gen_key_schedule_128(key)
    subBytesTable, _ = genTables()
    bv = BitVector(filename=inputfile)
    statearray = [[0 for x in range(4)] for x in range(4)]
    FILEOUT = open(outputfile, 'ab')
    temp_shift = [0] * 4

    while bv.more_to_read:
        bitvec = bv.read_bits_from_file(128)
        if len(bitvec) > 0:
            if len(bitvec) != 128:
                bitvec.pad_from_right(128 - len(bitvec))

            # Filling in statearray and XORing
            for i in range(4):
                for j in range(4):
                    statearray[i][j] = bitvec[32 * i + 8 * j:32 * i + 8 *
                                              (j + 1)]
                    statearray[i][j] ^= key_words[i][8 * j:8 + (8 * j)]

            for round_num in range(10):

                # SubBytes
                for i in range(4):
                    for j in range(4):
                        statearray[i][j] = BitVector(
                            intVal=subBytesTable[int(statearray[i][j])])

                # ShiftRows
                for i in range(1, 4):
                    for j in range(0, 4):
                        temp_shift[(j - i) % 4] = statearray[j][i]
                    for j in range(0, 4):
                        statearray[j][i] = temp_shift[j]

                # ColumnMixing
                if round_num != 9:
                    two_times = BitVector(bitstring='00000010')
                    three_times = BitVector(bitstring='00000011')
                    for i in range(4):
                        temp = (two_times.gf_multiply_modular(statearray[i][0], AES_modulus, 8)) ^ \
                                           (three_times.gf_multiply_modular(statearray[i][1], AES_modulus, 8)) ^ \
                                           statearray[i][2] ^ \
                                           statearray[i][3]

                        temp1 = (two_times.gf_multiply_modular(statearray[i][1], AES_modulus, 8)) ^ \
                                           (three_times.gf_multiply_modular(statearray[i][2], AES_modulus, 8)) ^ \
                                           statearray[i][3] ^ \
                                           statearray[i][0]

                        temp2 = (two_times.gf_multiply_modular(statearray[i][2], AES_modulus, 8)) ^ \
                                           (three_times.gf_multiply_modular(statearray[i][3], AES_modulus, 8)) ^ \
                                           statearray[i][0] ^ \
                                           statearray[i][1]

                        temp3 = (two_times.gf_multiply_modular(statearray[i][3], AES_modulus, 8)) ^ \
                                           (three_times.gf_multiply_modular(statearray[i][0], AES_modulus, 8)) ^ \
                                           statearray[i][1] ^ \
                                           statearray[i][2]

                        statearray[i][0] = temp
                        statearray[i][1] = temp1
                        statearray[i][2] = temp2
                        statearray[i][3] = temp3

                # Add round_num Key
                for i in range(4):
                    for j in range(4):
                        statearray[i][j] ^= key_words[(4 * (round_num + 1)) +
                                                      i][8 * j:8 + (8 * j)]

            for i in range(4):
                for j in range(4):
                    statearray[i][j].write_to_file(FILEOUT)
                    f.write(statearray[i][j].get_bitvector_in_hex())
    f.close()
    FILEOUT.close()
    return