def des_3round_cryptanalysis_differential(p: List[bytes], c: List[bytes]) -> List[bytes]: if len(p) != len(c): raise ValueError(__name__ + ": count of ciphertext and plaintext does not match") p_length = len(p) if p_length == 0: raise ValueError(__name__ + ": empty text lists") if p_length % 2 != 0: raise ValueError(__name__ + ": c_list of odd length") p_list = list(map(bit_string.bytes_to_bit_string, p)) c_list = list(map(bit_string.bytes_to_bit_string, c)) possible_key_chunks = _des_3round_cryptanalysis_differential_gen_possible_key_chunks( p_list, c_list) result = [] for possible_key in _des_3round_iter_possible_keys(possible_key_chunks): possible_key_bytes = bit_string.bit_string_to_bytes(possible_key) if list(map(lambda x: des_3round_encrypt(x, possible_key_bytes), p)) == c: result.append(possible_key_bytes) return result
def encrypt(p: bytes, k: bytes) -> bytes: _des_check(p, k) key_list = _des_gen_key_list(k) result = bit_string.bit_string_to_bytes( _des(bit_string.bytes_to_bit_string(p), key_list)) return result
def des_3round_encrypt(p: bytes, k: bytes) -> bytes: _des_check(p, k) key_list = _des_gen_key_list(k) key_list = key_list[:_des_3round_round_count] result = bit_string.bit_string_to_bytes( _des_3round(bit_string.bytes_to_bit_string(p), key_list)) return result
def decrypt(c: bytes, k: bytes) -> bytes: _des_check(c, k) key_list = _des_gen_key_list(k) key_list.reverse() result = bit_string.bit_string_to_bytes( _des(bit_string.bytes_to_bit_string(c), key_list)) return result