diff --git a/chapter_one/Verschiebe-Chiffre.py b/chapter_one/Verschiebe-Chiffre.py index b419bd4..9c85324 100644 --- a/chapter_one/Verschiebe-Chiffre.py +++ b/chapter_one/Verschiebe-Chiffre.py @@ -38,8 +38,6 @@ def shift_text(cleartext: str, incrementation: int): return shifted_text - - def brute_force_example(cleartext: str = 'Bitcoin', incrementation: int = 7): """ This is an example of how to bruteforce an encrypted text. (Encrypted with Letter-Shifting) diff --git a/chapter_three/Lineares-Schieberegister.py b/chapter_three/Lineares-Schieberegister.py new file mode 100644 index 0000000..6acb1f2 --- /dev/null +++ b/chapter_three/Lineares-Schieberegister.py @@ -0,0 +1,41 @@ +from utils import CipherUtils + +CLEAR_LIST = [0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1] +INIT_STATE = [0, 1, 1, 0] +COEFFICIENTS = [1, 1, 1, 0] + + +def create_shift_register(init_state: list, coefficients: list): + return_list = [] + current_state = init_state.copy() + current_state_list = [current_state.copy()] + + if (size := len(init_state)) != len(coefficients): + print('Length of lists does not match.') + exit() + + for x in range(2 ** size): + return_list.append(current_state[-1]) + new_state_value = 0 + for i in range(size): + new_state_value = (new_state_value + current_state[i] ^ coefficients[i]) % 2 + + current_state.insert(0, new_state_value) + current_state.pop(-1) + + if current_state in current_state_list: + return_list.extend(current_state[1:][::-1]) + break + current_state_list.append(current_state.copy()) + + return return_list + + +if __name__ == '__main__': + key = create_shift_register(INIT_STATE, COEFFICIENTS) + + encrypted = CipherUtils.xor_two_lists(CLEAR_LIST, key) + decrypted = CipherUtils.xor_two_lists(encrypted, key) + print(CLEAR_LIST) + print(encrypted) + print(decrypted) diff --git a/utils/CipherUtils.py b/utils/CipherUtils.py index 4d90451..2671530 100644 --- a/utils/CipherUtils.py +++ b/utils/CipherUtils.py @@ -93,6 +93,15 @@ def shift_char(char, incrementation: int = 1): return str(new_char_in_bytes)[2] +def xor_two_lists(clear_list, key_list): + xored_list = [] + + for x in range(len(clear_list)): + xored_list.append(clear_list[x] ^ key_list[x % len(key_list)]) + + return xored_list + + if __name__ == '__main__': print( calculate_frequency('Hier den Text eingeben, für den die Wahrscheinlichkeiten berechnet werden sollen', True))