correct bits order for x and y + test function for encryption

This commit is contained in:
Sam Hadow 2024-04-21 17:48:31 +02:00
parent 7313f3b9d1
commit 05a0cb2aa0

21
main.py
View File

@ -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()