parallelize attack
This commit is contained in:
parent
7a1bf3b06a
commit
c267c560c0
48
main.py
Normal file → Executable file
48
main.py
Normal file → Executable file
@ -98,10 +98,14 @@ def gen_6_bytes(key=[randint(0, 1) for _ in range(40)]):
|
|||||||
cipher = css_encrypt(text, key)
|
cipher = css_encrypt(text, key)
|
||||||
return cipher
|
return cipher
|
||||||
|
|
||||||
def attack(Bytes=gen_6_bytes()):
|
import multiprocessing
|
||||||
|
|
||||||
|
def attack_worker(start, end, Bytes, result_queue, stop_event):
|
||||||
taps17 = [0, 14]
|
taps17 = [0, 14]
|
||||||
taps25 = [0, 3, 4, 12]
|
taps25 = [0, 3, 4, 12]
|
||||||
for i in range((2**16)-1):
|
for i in range(start, end):
|
||||||
|
if stop_event.is_set():
|
||||||
|
return
|
||||||
lfsr17_init = [int(bit) for bit in bin(i)[2:].zfill(16)]+[1]
|
lfsr17_init = [int(bit) for bit in bin(i)[2:].zfill(16)]+[1]
|
||||||
lfsr17 = lfsr(lfsr17_init, taps17)
|
lfsr17 = lfsr(lfsr17_init, taps17)
|
||||||
x = []
|
x = []
|
||||||
@ -134,17 +138,46 @@ def attack(Bytes=gen_6_bytes()):
|
|||||||
c=1 if x[4]+y[4]>255 else 0
|
c=1 if x[4]+y[4]>255 else 0
|
||||||
z6 = (x[5]+y[5]+c)%256
|
z6 = (x[5]+y[5]+c)%256
|
||||||
if z4 == Bytes[3] and z5 == Bytes[4] and z6 == Bytes[5]:
|
if z4 == Bytes[3] and z5 == Bytes[4] and z6 == Bytes[5]:
|
||||||
print("key found: ", end='\t')
|
|
||||||
key = bin(x[0])[2:].zfill(8)[::-1] + bin(x[1])[2:].zfill(8)[::-1] + bin(y[0])[2:].zfill(8)[::-1] + bin(y[1])[2:].zfill(8)[::-1] + bin(y[2])[2:].zfill(8)[::-1]
|
key = bin(x[0])[2:].zfill(8)[::-1] + bin(x[1])[2:].zfill(8)[::-1] + bin(y[0])[2:].zfill(8)[::-1] + bin(y[1])[2:].zfill(8)[::-1] + bin(y[2])[2:].zfill(8)[::-1]
|
||||||
print(key)
|
result_queue.put(key)
|
||||||
return [int(bit) for bit in key]
|
stop_event.set()
|
||||||
break
|
return
|
||||||
|
|
||||||
|
def attack(Bytes=gen_6_bytes()):
|
||||||
|
result_queue = multiprocessing.Queue()
|
||||||
|
stop_event = multiprocessing.Event()
|
||||||
|
processes = []
|
||||||
|
num_cores = multiprocessing.cpu_count()
|
||||||
|
max_upper_limit = 2**16
|
||||||
|
chunk_size = max_upper_limit // num_cores
|
||||||
|
|
||||||
|
for i in range(num_cores):
|
||||||
|
start = i * chunk_size
|
||||||
|
end = start + chunk_size
|
||||||
|
process = multiprocessing.Process(target=attack_worker, args=(start, end, Bytes, result_queue, stop_event))
|
||||||
|
processes.append(process)
|
||||||
|
process.start()
|
||||||
|
# # last process
|
||||||
|
# start = (num_cores-1) * chunk_size
|
||||||
|
# end = max_upper_limit
|
||||||
|
# process = multiprocessing.Process(target=attack_worker, args=(start, end, Bytes, result_queue, stop_event))
|
||||||
|
# processes.append(process)
|
||||||
|
# process.start()
|
||||||
|
|
||||||
|
for process in processes:
|
||||||
|
process.join()
|
||||||
|
|
||||||
|
while not result_queue.empty():
|
||||||
|
key = result_queue.get()
|
||||||
|
print(f'key found: \t{key}')
|
||||||
|
return [int(bit) for bit in key]
|
||||||
|
|
||||||
def test_attack(n=1):
|
def test_attack(n=1):
|
||||||
success = 0
|
success = 0
|
||||||
print(f'testing attack in 2^16 against CSS {n} times (keys randomly generated each time)\n')
|
print(f'testing attack in 2^16 against CSS {n} times (keys randomly generated each time)\n')
|
||||||
for _ in range(n):
|
for _ in range(n):
|
||||||
key = [randint(0, 1) for _ in range(40)]
|
key = [randint(0, 1) for _ in range(40)]
|
||||||
|
# key = [1]*40
|
||||||
key_string = ''.join(str(bit) for bit in key)
|
key_string = ''.join(str(bit) for bit in key)
|
||||||
print(f'key generated: \t{key_string}')
|
print(f'key generated: \t{key_string}')
|
||||||
Bytes = gen_6_bytes(key)
|
Bytes = gen_6_bytes(key)
|
||||||
@ -159,5 +192,4 @@ test_lfsr17()
|
|||||||
print()
|
print()
|
||||||
test_encrypt()
|
test_encrypt()
|
||||||
print()
|
print()
|
||||||
#gen_6_bytes()
|
test_attack(5)
|
||||||
test_attack(100)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user