def e_SDES_CBC(plaintext, key): undefined = utilities.get_undefined(plaintext, utilities.get_B6Code()) text = utilities.remove_undefined(plaintext, utilities.get_B6Code()) size = int(int(get_SDES_value('block_size')) / 6) blocks = utilities.text_to_blocks(text, size) while len(blocks[-1]) != size: blocks[-1] += 'Q' IV = get_IV() ciphertext = '' for block in blocks: cipher_block = '' for b in block: cipher_block += encode_B6(b) cipher_block = utilities.xor(cipher_block, IV) for i in range(int(get_SDES_value('rounds'))): cipher_block = feistel(cipher_block, get_subKey(key, i + 1)) cipher_block = cipher_block[ int(len(cipher_block) / 2):] + cipher_block[:int(len(cipher_block) / 2)] b_blocks = utilities.text_to_blocks(cipher_block, 6) for b_b in b_blocks: ciphertext += decode_B6(b_b) IV = cipher_block ciphertext = utilities.insert_undefinedList(ciphertext, undefined) return ciphertext
def correct_hamming(message, parite_pair): # Use an inverted array (where the first two control bits are on the left of the array) correction_bits = [] reset_message = message.copy() for i in [0, 1, 3, 7, 15]: correction_bits.append(message[i]) reset_message[i] = 0 array = hamming(reset_message, parite_pair) calculated_bits = [] for i in [0, 1, 3, 7, 15]: calculated_bits.append(array[i]) print(f'Bits from message {correction_bits}') print(f'Recalculated bits {calculated_bits}') position_error = int(xor(correction_bits[::-1], calculated_bits[::-1]), 2) - 1 print(f"L'erreur est à la position {position_error}") if position_error == -1: print('Le message est correct!') else: # print(f'Avant correction, le message est {message}') if message[position_error] == 0: message[position_error] = 1 else: message[position_error] = 0 print(f'Après correction, le message est {message}') return message
def d_SDES_OFB(ciphertext, key): undefined = utilities.get_undefined(ciphertext, utilities.get_B6Code()) text = utilities.remove_undefined(ciphertext, utilities.get_B6Code()) size = int(int(get_SDES_value('block_size')) / 6) blocks = utilities.text_to_blocks(text, size) IV = get_IV() plaintext = '' counter = 0 for block in blocks: temp = '' plain_block = IV rounds = int(get_SDES_value('rounds')) for i in range(rounds): plain_block = feistel(plain_block, get_subKey(key, i + 1)) plain_block = plain_block[int( len(plain_block) / 2):] + plain_block[:int(len(plain_block) / 2)] IV = plain_block for b in block: temp += encode_B6(b) if counter <= len(blocks) - 2: plain_block = utilities.xor(plain_block, temp) else: plain_block = utilities.xor(temp, plain_block[:len(temp)]) b_blocks = utilities.text_to_blocks(plain_block, 6) for b_b in b_blocks: plaintext += decode_B6(b_b) counter += 1 plaintext = utilities.insert_undefinedList(plaintext, undefined) return plaintext
def feistel(bi,ki): if len(bi)%2!= 0: print('Error(feistel): Invalid block',end='') return '' elif not utilities.is_binary(ki) or len(ki)%2!= 0: print('Error(feistel): Invalid key',end='') return '' else: # print(bi, ki) m = int(len(bi)//2) Li = bi[:m] Ri = bi[m:] Li1 = Ri Ri1 = utilities.xor(Li, F(Ri,ki)) bi2 = Li1 + Ri1 return bi2
def F(Ri,ki): if utilities.is_binary(ki) and len(Ri)%2 == 0 and len(ki)%2== 0: expander = expand(Ri) xor_output = utilities.xor(expander,ki) mid = len(xor_output)//2 bx1 = xor_output[:mid] bx2 = xor_output[mid:] sx1 = sbox1(bx1) sx2 = sbox2(bx2) Ri2 = sx1+sx2 return Ri2 elif not utilities.is_binary(ki) or len(ki)%2!= 0: print('Error(F): invalid key',end='') return '' elif len(Ri)%2 != 0: print('Error(F): invalid input',end='') return ''
def feistel(bi, ki): # your code here if not utilities.is_binary(ki) or len(ki) != int( get_SDES_value('key_size')) - 1: print('Error(feistel): Invalid key', end='') return '' if not utilities.is_binary(bi) or len(bi) != int( get_SDES_value('block_size')): print('Error(feistel): Invalid block', end='') return '' after_F = F(bi[int(len(bi) / 2):], ki) after_xor = utilities.xor(after_F, bi[:int(len(bi) / 2)]) bi2 = bi[int(len(bi) / 2):] + after_xor return bi2
def d_SDES_CBC(ciphertext, key): undefined = utilities.get_undefined(ciphertext, utilities.get_B6Code()) text = utilities.remove_undefined(ciphertext, utilities.get_B6Code()) size = int(int(get_SDES_value('block_size')) / 6) blocks = utilities.text_to_blocks(text, size) plaintext = '' IV = get_IV() temp = '' for block in blocks: plain_block = '' for b in block: plain_block += encode_B6(b) temp = plain_block rounds = int(get_SDES_value('rounds')) for i in range(rounds): plain_block = feistel(plain_block, get_subKey(key, rounds - i)) plain_block = plain_block[int( len(plain_block) / 2):] + plain_block[:int(len(plain_block) / 2)] plain_block = utilities.xor(IV, plain_block) b_blocks = utilities.text_to_blocks(plain_block, 6) for b_b in b_blocks: plaintext += decode_B6(b_b) IV = temp while plaintext[-1] == 'Q': plaintext = plaintext[:-1] plaintext = utilities.insert_undefinedList(plaintext, undefined) return plaintext
def F(Ri, ki): # your code here if not utilities.is_binary(ki): print('Error(F): invalid key', end='') return '' if len(ki) != int(get_SDES_value('key_size')) - 1: print('Error(F): invalid key', end='') return '' if not utilities.is_binary(Ri): print('Error(F): invalid input', end='') return '' if len(Ri) != int(get_SDES_value('block_size')) / 2: print('Error(F): invalid input', end='') return '' expanded = expand(Ri) xored = utilities.xor(expanded, ki) b1 = sbox1(xored[:int(len(xored) / 2)]) b2 = sbox2(xored[int(len(xored) / 2):]) Ri2 = b1 + b2 return Ri2
def fiestel(input_block, round_keys): rounds = 16 length = len(input_block) left_half = input_block[: length / 2] right_half = input_block[length / 2 :] for round in range(rounds): # Current right half is the left half of next iteration. next_left_half = right_half # Expand the right half from 32 bits to 48 bits. expanded_right_half = "" for i in range(4, (length / 2) + 1, 4): try: # Duplicate every 4th and 5th bits. expanded_right_half += right_half[i - 4 : i] expanded_right_half += right_half[i] + right_half[i - 1] except IndexError: pass expanded_right_half = right_half[length / 2 - 1] + expanded_right_half + right_half[0] right_half = expanded_right_half # XOR the expanded right half with round key. right_half = xor(right_half, round_keys[round]) # Split the right half into 8 blocks of 6 bits each. splitted_right_half = [] for i in range(0, 48, 6): splitted_right_half.append(right_half[i : i + 6]) s_box_tables = [ [ [ "1110", "0100", "1101", "0001", "0010", "1111", "1011", "1000", "0011", "1010", "0110", "1100", "0101", "1001", "0000", "0111", ], [ "0000", "1111", "0111", "0100", "1110", "0010", "1101", "0001", "1010", "0110", "1100", "1011", "1001", "0101", "0011", "1000", ], [ "0100", "0001", "1110", "1000", "1101", "0110", "0010", "1011", "1111", "1100", "1001", "0111", "0011", "1010", "0101", "0000", ], [ "1111", "1100", "1000", "0010", "0100", "1001", "0001", "0111", "0101", "1011", "0011", "1110", "1010", "0000", "0110", "1101", ], ], [ [ "1111", "0001", "1000", "1110", "0110", "1011", "0011", "0100", "1001", "0111", "0010", "1101", "1100", "0000", "0101", "1010", ], [ "0011", "1101", "0100", "0111", "1111", "0010", "1000", "1110", "1100", "0000", "0001", "1010", "0110", "1001", "1011", "0101", ], [ "0000", "1110", "0111", "1011", "1010", "0100", "1101", "0001", "0101", "1000", "1100", "0110", "1001", "0011", "0010", "1111", ], [ "1101", "1000", "1010", "0001", "0011", "1111", "0100", "0010", "1011", "0110", "0111", "1100", "0000", "0101", "1110", "1001", ], ], [ [ "1010", "0000", "1001", "1110", "0110", "0011", "1111", "0101", "0001", "1101", "1100", "0111", "1011", "0100", "0010", "1000", ], [ "1101", "0111", "0000", "1001", "0011", "0100", "0110", "1010", "0010", "1000", "0101", "1110", "1100", "1011", "1111", "0001", ], [ "1101", "0110", "0100", "1001", "1000", "1111", "0011", "0000", "1011", "0001", "0010", "1100", "0101", "1010", "1110", "0111", ], [ "0001", "1010", "1101", "0000", "0110", "1001", "1000", "0111", "0100", "1111", "1110", "0011", "1011", "0101", "0010", "1100", ], ], [ [ "0111", "1101", "1110", "0011", "0000", "0110", "1001", "1010", "0001", "0010", "1000", "0101", "1011", "1100", "0100", "1111", ], [ "1101", "1000", "1011", "0101", "0110", "1111", "0000", "0011", "0100", "0111", "0010", "1100", "0001", "1010", "1110", "1001", ], [ "1010", "0110", "1001", "0000", "1100", "1011", "0111", "1101", "1111", "0001", "0011", "1110", "0101", "0010", "1000", "0100", ], [ "0011", "1111", "0000", "0110", "1010", "0001", "1101", "1000", "1001", "0100", "0101", "1011", "1100", "0111", "0010", "1110", ], ], [ [ "0010", "1100", "0100", "0001", "0111", "1010", "1011", "0110", "1000", "0101", "0011", "1111", "1101", "0000", "1110", "1001", ], [ "1110", "1011", "0010", "1100", "0100", "0111", "1101", "0001", "0101", "0000", "1111", "1010", "0011", "1001", "1000", "0110", ], [ "0100", "0010", "0001", "1011", "1010", "1101", "0111", "1000", "1111", "1001", "1100", "0101", "0110", "0011", "0000", "1110", ], [ "1011", "1000", "1100", "0111", "0001", "1110", "0010", "1101", "0110", "1111", "0000", "1001", "1010", "0100", "0101", "0011", ], ], [ [ "1100", "0001", "1010", "1111", "1001", "0010", "0110", "1000", "0000", "1101", "0011", "0100", "1110", "0111", "0101", "1011", ], [ "1010", "1111", "0100", "0010", "0111", "1100", "1001", "0101", "0110", "0001", "1101", "1110", "0000", "1011", "0011", "1000", ], [ "1001", "1110", "1111", "0101", "0010", "1000", "1100", "0011", "0111", "0000", "0100", "1010", "0001", "1101", "1011", "0110", ], [ "0100", "0011", "0010", "1100", "1001", "0101", "1111", "1010", "1011", "1110", "0001", "0111", "0110", "0000", "1000", "1101", ], ], [ [ "0100", "1011", "0010", "1110", "1111", "0000", "1000", "1101", "0011", "1100", "1001", "0111", "0101", "1010", "0110", "0001", ], [ "1101", "0000", "1011", "0111", "0100", "1001", "0001", "1010", "1110", "0011", "0101", "1100", "0010", "1111", "1000", "0110", ], [ "0001", "0100", "1011", "1101", "1100", "0011", "0111", "1110", "1010", "1111", "0110", "1000", "0000", "0101", "1001", "0010", ], [ "0110", "1011", "1101", "1000", "0001", "0100", "1010", "0111", "1001", "0101", "0000", "1111", "1110", "0010", "0011", "1100", ], ], [ [ "1101", "0010", "1000", "0100", "0110", "1111", "1011", "0001", "1010", "1001", "0011", "1110", "0101", "0000", "1100", "0111", ], [ "0001", "1111", "1101", "1000", "1010", "0011", "0111", "0100", "1100", "0101", "0110", "1011", "0000", "1110", "1001", "0010", ], [ "0111", "1011", "0100", "0001", "1001", "1100", "1110", "0010", "0000", "0110", "1010", "1101", "1111", "0011", "0101", "1000", ], [ "0010", "0001", "1110", "0111", "0100", "1010", "1000", "1101", "1111", "1100", "1001", "0000", "0011", "0101", "0110", "1011", ], ], ] right_half = "" for i in range(8): row = int(splitted_right_half[i][0] + splitted_right_half[i][5], 2) column = int(splitted_right_half[i][1:5], 2) right_half += s_box_tables[i][row][column] permutation_table = [ 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25, ] permuted_right_half = "" for i in range(32): permuted_right_half += right_half[permutation_table[i] - 1] right_half = xor(permuted_right_half, left_half) left_half = next_left_half # 32-bit swap of left half and right half output = right_half + left_half return output