From 05a0cb2aa02ed4f854b12467949682646ab0bf95 Mon Sep 17 00:00:00 2001 From: Sam Hadow Date: Sun, 21 Apr 2024 17:48:31 +0200 Subject: [PATCH] correct bits order for x and y + test function for encryption --- main.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index e17be25..4e200de 100644 --- a/main.py +++ b/main.py @@ -31,6 +31,7 @@ class lfsr(object): def test_lfsr17(): + print("test lfsr17") key = [randint(0, 1) for _ in range(16)] # first 16 bits key.append(1) # prevent initial state from being {0}^17 taps = [0, 14] @@ -46,7 +47,7 @@ def test_lfsr17(): return False n_state = len(sorted_states) n_state_log = log2(n_state+1) - print(f'all {n_state} = 2^({n_state_log})-1 states are different') + print(f'all {n_state} = 2^({n_state_log})-1 generated states are different') return True @@ -66,8 +67,8 @@ def css_encrypt(text, key): for _ in range(8): x_b2 += str(lfsr17.shift()) y_b2 += str(lfsr25.shift()) - x = int(x_b2, 2) - y = int(y_b2, 2) + x = int(x_b2[::-1], 2) + y = int(y_b2[::-1], 2) z = (x + y + carry) % 256 carry = 1 if x + y > 255 else 0 @@ -76,8 +77,14 @@ def css_encrypt(text, key): cipher_text = (cipher_text << 8) | cipher_byte return cipher_text +def test_encrypt(): + print("test encryption: text 0xffffffffff, key 0x0 ∈ {0, 1}^40") + cipher = css_encrypt(0xffffffffff, [0]*40) + print(f'cipher: {hex(cipher)}') + clear = css_encrypt(cipher, [0]*40) + print(f'decrypted: {hex(clear)}') + print(f'original text and decrypted message are the same: {clear==0xffffffffff}') + test_lfsr17() -cipher = css_encrypt(0xffffffffff, [0]*40) -print(hex(cipher)) -clear = css_encrypt(cipher, [0]*40) -print(hex(clear)) +print() +test_encrypt()