correct bits order for x and y + test function for encryption
This commit is contained in:
parent
7313f3b9d1
commit
05a0cb2aa0
21
main.py
21
main.py
@ -31,6 +31,7 @@ class lfsr(object):
|
|||||||
|
|
||||||
|
|
||||||
def test_lfsr17():
|
def test_lfsr17():
|
||||||
|
print("test lfsr17")
|
||||||
key = [randint(0, 1) for _ in range(16)] # first 16 bits
|
key = [randint(0, 1) for _ in range(16)] # first 16 bits
|
||||||
key.append(1) # prevent initial state from being {0}^17
|
key.append(1) # prevent initial state from being {0}^17
|
||||||
taps = [0, 14]
|
taps = [0, 14]
|
||||||
@ -46,7 +47,7 @@ def test_lfsr17():
|
|||||||
return False
|
return False
|
||||||
n_state = len(sorted_states)
|
n_state = len(sorted_states)
|
||||||
n_state_log = log2(n_state+1)
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
@ -66,8 +67,8 @@ def css_encrypt(text, key):
|
|||||||
for _ in range(8):
|
for _ in range(8):
|
||||||
x_b2 += str(lfsr17.shift())
|
x_b2 += str(lfsr17.shift())
|
||||||
y_b2 += str(lfsr25.shift())
|
y_b2 += str(lfsr25.shift())
|
||||||
x = int(x_b2, 2)
|
x = int(x_b2[::-1], 2)
|
||||||
y = int(y_b2, 2)
|
y = int(y_b2[::-1], 2)
|
||||||
|
|
||||||
z = (x + y + carry) % 256
|
z = (x + y + carry) % 256
|
||||||
carry = 1 if x + y > 255 else 0
|
carry = 1 if x + y > 255 else 0
|
||||||
@ -76,8 +77,14 @@ def css_encrypt(text, key):
|
|||||||
cipher_text = (cipher_text << 8) | cipher_byte
|
cipher_text = (cipher_text << 8) | cipher_byte
|
||||||
return cipher_text
|
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()
|
test_lfsr17()
|
||||||
cipher = css_encrypt(0xffffffffff, [0]*40)
|
print()
|
||||||
print(hex(cipher))
|
test_encrypt()
|
||||||
clear = css_encrypt(cipher, [0]*40)
|
|
||||||
print(hex(clear))
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user