def word_xor(word1, word2):
    result = []
    for idx in range(4):
        b1 = BitVector(intVal=int(word1[idx], 16), size=8)
        b2 = BitVector(intVal=int(word2[idx], 16), size=8)
        result.append(b1.__xor__(b2).get_bitvector_in_hex())
    return result
Пример #2
0
def rowColumnMul(word1, word2):
    bsum = BitVector(intVal=0, size=8)
    for idx in range(len(word1)):
        b1 = BitVector(intVal=int(word1[idx], 16), size=8)
        b2 = BitVector(intVal=int(word2[idx], 16), size=8)
        bmul = b1.gf_multiply_modular(b2, AES_modulus, 8)
        bsum = bsum.__xor__(bmul)
    return bsum.get_bitvector_in_hex()
def add_round_const(word):
    result = []
    add_word = gen_round_const()
    for idx in range(4):
        b1 = BitVector(intVal=int(word[idx], 16), size=8)
        b2 = BitVector(intVal=int(add_word[idx], 16), size=8)
        result.append(b1.__xor__(b2).get_bitvector_in_hex())
    return result
Пример #4
0
def gen_round_const():
    first = '01'
    b2 = BitVector(intVal=2, size=8)
    b11 = BitVector(hexstring="11")
    b = BitVector(intVal=int(first, 16), size=8)

    global REP
    for idx in range(REP):
        b = b.gf_multiply_modular(b2, AES_modulus, 8)
        if idx >= 128:
            b = b.__xor__(b11)
    REP = REP + 1
    return [b.get_bitvector_in_hex(), '00', '00', '00']
    for i in range(16 - more_chars):
        message += '\0'

round_keys = main(key)  # calculates all round keys for encryption/decryption

subBytesTable = []  # for encryption
invSubBytesTable = []  # for decryption
genTables()

# Encryption Algorithm

encrypt = ''
for i in range(len(message) // 16):  # for every 128 bit block
    text_block = message[i * 16:i * 16 + 16]
    bit_block = BitVector(textstring=text_block)
    bit_block = bit_block.__xor__(round_keys[0])  # pre-round XORing

    for j in range(len(round_keys) - 1):  # all rounds
        round_num = j + 1
        # substitute bytes
        for k in range(16):
            sub_val = subBytesTable[ord(bit_block[k * 8:k * 8 +
                                                  8].get_bitvector_in_ascii())]
            bit_block[k * 8:k * 8 + 8] = BitVector(size=8).__xor__(
                BitVector(intVal=sub_val))

        # shift rows; 12 total replacements

        # nothing done for row 0
        # row 1
        byte_num = 1
Пример #6
0
def encryption_round(input, subBytesTable, round_keys):
    encrypt = ''
    bit_block = BitVector(bitstring=input)
    bit_block = bit_block.__xor__(round_keys[0])  # pre-round XORing

    for j in range(len(round_keys) - 1):  # all rounds
        round_num = j + 1
        # substitute bytes
        for k in range(16):
            sub_val = subBytesTable[ord(bit_block[k * 8:k * 8 + 8].get_bitvector_in_ascii())]
            bit_block[k * 8:k * 8 + 8] = BitVector(size=8).__xor__(BitVector(intVal=sub_val))

        # shift rows; 12 total replacements

        # nothing done for row 0
        # row 1
        byte_num = 1
        temp = getByte(bit_block, 1)
        bit_block[byte_num * 8:byte_num * 8 + 8] = getByte(bit_block, 5)
        byte_num = 5
        bit_block[byte_num * 8:byte_num * 8 + 8] = getByte(bit_block, 9)
        byte_num = 9
        bit_block[byte_num * 8:byte_num * 8 + 8] = getByte(bit_block, 13)
        byte_num = 13
        bit_block[byte_num * 8:byte_num * 8 + 8] = temp

        # row 2
        byte_num = 2
        temp = getByte(bit_block, 2)
        bit_block[byte_num * 8:byte_num * 8 + 8] = getByte(bit_block, 10)
        byte_num = 10
        bit_block[byte_num * 8:byte_num * 8 + 8] = temp
        temp = getByte(bit_block, 6)
        byte_num = 6
        bit_block[byte_num * 8:byte_num * 8 + 8] = getByte(bit_block, 14)
        byte_num = 14
        bit_block[byte_num * 8:byte_num * 8 + 8] = temp

        # row 3
        byte_num = 3
        temp = getByte(bit_block, 3)
        bit_block[byte_num * 8:byte_num * 8 + 8] = getByte(bit_block, 15)
        byte_num = 15
        bit_block[byte_num * 8:byte_num * 8 + 8] = getByte(bit_block, 11)
        byte_num = 11
        bit_block[byte_num * 8:byte_num * 8 + 8] = getByte(bit_block, 7)
        byte_num = 7
        bit_block[byte_num * 8:byte_num * 8 + 8] = temp

        # mix cols
        if round_num != len(round_keys) - 1:
            bit_copy = []  # creates a 4 by 4 array of bit_block
            for k in range(4):
                inner = []
                for l in range(4):
                    inner.append(bit_block[32 * l + 8 * k:32 * l + 8 * k + 8])
                bit_copy.append(inner)

            for k in range(4):  # column 0
                bit_block[k * 8 * 4:k * 8 * 4 + 8] = (((
                    bit_copy[0][k].gf_multiply_modular(BitVector(bitstring='00000010'), AES_modulus, 8)).__xor__(
                    bit_copy[1][k].gf_multiply_modular(BitVector(bitstring='00000011'), AES_modulus, 8))).__xor__(
                    bit_copy[2][k])).__xor__(bit_copy[3][k])

            for k in range(4):  # column 1
                index = k * 8 * 4 + 8
                bit_block[index:index + 8] = (((
                    bit_copy[1][k].gf_multiply_modular(BitVector(bitstring='00000010'), AES_modulus, 8)).__xor__(
                    bit_copy[2][k].gf_multiply_modular(BitVector(bitstring='00000011'), AES_modulus, 8))).__xor__(
                    bit_copy[3][k])).__xor__(bit_copy[0][k])

            for k in range(4):  # column 2
                index = k * 8 * 4 + 16
                bit_block[index:index + 8] = (((
                    bit_copy[2][k].gf_multiply_modular(BitVector(bitstring='00000010'), AES_modulus, 8)).__xor__(
                    bit_copy[3][k].gf_multiply_modular(BitVector(bitstring='00000011'), AES_modulus, 8))).__xor__(
                    bit_copy[0][k])).__xor__(bit_copy[1][k])

            for k in range(4):  # column 3
                index = k * 8 * 4 + 24
                bit_block[index:index + 8] = (((
                    bit_copy[3][k].gf_multiply_modular(BitVector(bitstring='00000010'), AES_modulus, 8)).__xor__(
                    bit_copy[0][k].gf_multiply_modular(BitVector(bitstring='00000011'), AES_modulus, 8))).__xor__(
                    bit_copy[1][k])).__xor__(bit_copy[2][k])

        # add round key
        bit_block = bit_block.__xor__(round_keys[round_num])

    encrypt = BitVector(bitstring=bit_block)
    return encrypt