css encrypt
This commit is contained in:
parent
519676afd8
commit
7313f3b9d1
37
main.py
37
main.py
@ -25,15 +25,15 @@ class lfsr(object):
|
||||
calculate next state and return the output bit
|
||||
'''
|
||||
feedback = sum(self.state[tap] for tap in self.taps) % 2
|
||||
output = self.state[-1]
|
||||
self.state = [feedback] + self.state[:-1]
|
||||
output = self.state[0]
|
||||
self.state = self.state[1:] + [feedback]
|
||||
return output
|
||||
|
||||
|
||||
def test_lfsr17():
|
||||
key = [randint(0, 1) for _ in range(16)] # first 16 bits
|
||||
key.append(1) # prevent initial state from being {0}^17
|
||||
taps = [16-0, 16-14]
|
||||
taps = [0, 14]
|
||||
lfsr17 = lfsr(key, taps)
|
||||
states = [lfsr17.state]
|
||||
for _ in range(2**17-2):
|
||||
@ -49,4 +49,35 @@ def test_lfsr17():
|
||||
print(f'all {n_state} = 2^({n_state_log})-1 states are different')
|
||||
return True
|
||||
|
||||
|
||||
def css_encrypt(text, key):
|
||||
taps17 = [0, 14]
|
||||
lfsr17 = lfsr((key[:16]+[1]), taps17)
|
||||
taps25 = [0, 3, 4, 12]
|
||||
lfsr25 = lfsr((key[16:]+[1]), taps25)
|
||||
cipher_text = 0
|
||||
carry = 0
|
||||
bytes = text.to_bytes((text.bit_length() + 7) // 8, 'big')
|
||||
|
||||
for byte in bytes:
|
||||
x_b2 = ""
|
||||
y_b2 = ""
|
||||
# Generate bytes from lfsr17 and lfsr25
|
||||
for _ in range(8):
|
||||
x_b2 += str(lfsr17.shift())
|
||||
y_b2 += str(lfsr25.shift())
|
||||
x = int(x_b2, 2)
|
||||
y = int(y_b2, 2)
|
||||
|
||||
z = (x + y + carry) % 256
|
||||
carry = 1 if x + y > 255 else 0
|
||||
|
||||
cipher_byte = z ^ byte
|
||||
cipher_text = (cipher_text << 8) | cipher_byte
|
||||
return cipher_text
|
||||
|
||||
test_lfsr17()
|
||||
cipher = css_encrypt(0xffffffffff, [0]*40)
|
||||
print(hex(cipher))
|
||||
clear = css_encrypt(cipher, [0]*40)
|
||||
print(hex(clear))
|
||||
|
Loading…
x
Reference in New Issue
Block a user