Skip to content

Commit

Permalink
Merge pull request #15 from Pharisaeus/improvements
Browse files Browse the repository at this point in the history
py3 compatibility improvements + some useful algorithms
  • Loading branch information
nazywam authored Mar 24, 2021
2 parents 4237a5e + 4f04283 commit cdbf342
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
42 changes: 42 additions & 0 deletions crypto_commons/generic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import math


def long_range(start, stop, step=1):
"""
Sequence generator working with python longs
Expand Down Expand Up @@ -34,6 +37,8 @@ def long_to_bytes(data):
:param data: integer
:return: ascii encoded string
"""
if data == 0:
return "\0"
data = int(data)
data = hex(data).rstrip('L').lstrip('0x')
if len(data) % 2 == 1:
Expand Down Expand Up @@ -250,3 +255,40 @@ def is_printable(data):
import string
printable = set(string.printable).union(string.printable.encode('utf-8'))
return len(set(data).difference(printable)) == 0


def baby_steps_giant_steps(a, b, p, N=None):
if not N:
N = 1 + int(math.sqrt(p))
baby_steps = {}
baby_step = 1
for r in long_range(0, N + 1):
baby_steps[baby_step] = r
baby_step = baby_step * a % p
giant_stride = pow(a, (p - 2) * N, p)
giant_step = b
for q in long_range(0, N ** 8 + 1):
if giant_step in baby_steps:
result = q * N + baby_steps[giant_step]
return result
else:
giant_step = giant_step * giant_stride % p


def jacobi_symbol(a, n):
assert (n > a > 0 and n % 2 == 1)
t = 1
while a != 0:
while a % 2 == 0:
a /= 2
r = n % 8
if r == 3 or r == 5:
t = -t
a, n = n, a
if a % 4 == n % 4 == 3:
t = -t
a %= n
if n == 1:
return t
else:
return 0
12 changes: 6 additions & 6 deletions crypto_commons/netcat/netcat_commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def nc(host, port):


def receive_until(s, delimiters, break_on_empty=False):
all_data = ""
all_data = b""
data = s.recv(1)
while data not in delimiters:
all_data += data
data = s.recv(1)
if data == '' and break_on_empty:
if not data and break_on_empty:
return all_data
return all_data + data

Expand All @@ -30,13 +30,13 @@ def receive_until_match(s, regex, timeout=None, limit=-1, break_on_empty=False):
:return: read data
"""
s.settimeout(timeout)
all_data = ""
all_data = b""
i = 0
try:
while re.search(regex, all_data) is None:
while re.search(regex, all_data.decode("utf-8")) is None:
new_char = s.recv(1)
all_data += new_char
if (limit != -1 and i > limit) or (new_char == '' and break_on_empty):
if (limit != -1 and i > limit) or (not new_char and break_on_empty):
break
i += 1
except Exception as e:
Expand All @@ -47,7 +47,7 @@ def receive_until_match(s, regex, timeout=None, limit=-1, break_on_empty=False):


def send(s, payload):
s.sendall(payload + "\n")
s.sendall(payload + b"\n")


def interactive(s):
Expand Down
2 changes: 1 addition & 1 deletion crypto_commons/xor/repeating_xor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def interactive_hack(xored, ciphertexts, printable=False):
err = "Can't break more than a single block at a time! Taking prefix '%s'"
print(err % potential_plaintext_contents[:ciphertext_len])

max_missing_bytes = max(map(len, xored)) - len(potential_plaintext_contents)
max_missing_bytes = max(map(len, xored)) - len(potential_plaintext_contents) + 1
for start_position in set(range(max_missing_bytes)).union({0}):
for index, xored_ciphertext in enumerate(xored):
number_of_ciphertexts = len(ciphertexts)
Expand Down

0 comments on commit cdbf342

Please sign in to comment.