def shamir_handler(filename, mode='encode'): fd = open(filename, 'rb') if mode == 'encode': out = open(filename + ".shmr", 'wb') kfile = open(filename + ".shmrk", 'wb') p = basic.get_prime() alice = basic.get_cd(basic.rand(3, int(sqrt(p))), p) bob = basic.get_cd(basic.rand(3, int(sqrt(p))), p) kfile.write(bob[1].to_bytes(4, byteorder='big')) kfile.write(p.to_bytes(4, byteorder='big')) kfile.close() while True: char = fd.read(1) if not char: break out.write( shamir_encode(ord(char), alice, bob, p).to_bytes(2, byteorder='big')) fd.close() out.close() output = [filename + ".shmr", filename + ".shmrk"] elif mode == 'decode': out = open(filename[:-len(".shmr")], 'wb') kfile = open(filename + "k", 'rb') d_bob = int.from_bytes(kfile.read(4), byteorder='big') p = int.from_bytes(kfile.read(4), byteorder='big') kfile.close() while True: char = fd.read(2) if not char: break out.write( shamir_decode(int.from_bytes(char, byteorder='big'), d_bob, p).to_bytes(1, byteorder='big')) fd.close() out.close() output = [filename[:-len(".shmr")], "DECODED"] else: print("Unknown parameter") fd.close() output = ["", ""] return output
def bank_init(client_value=999999, store_value=0): p, q = basic.get_prime_pair() n = p * q phi = (p - 1) * (q - 1) c, d = basic.get_cd(basic.rand(3, int(sqrt(phi))), phi + 1) accounts["client"] = client_value accounts["store"] = store_value print("Sberbank accounts: " + str(accounts)) return n, d, c
def rsa_handler(filename, mode='encode'): fd = open(filename, 'rb') if mode == 'encode': out = open(filename + ".rsa", 'wb') kfile = open(filename + ".rsak", 'wb') p = basic.get_prime() q = basic.get_prime() n = p * q phi = (p - 1) * (q - 1) keys = basic.get_cd(basic.rand(3, int(sqrt(phi))), phi + 1) c = keys[0] d = keys[1] kfile.write(c.to_bytes(4, byteorder='big')) kfile.write(n.to_bytes(4, byteorder='big')) kfile.close() while True: char = fd.read(1) if not char: break out.write(rsa_encode(ord(char), d, n).to_bytes(4, byteorder='big')) fd.close() out.close() output = [filename + ".rsa", filename + ".rsak"] elif mode == 'decode': out = open(filename[:-len(".rsa")], 'wb') kfile = open(filename + "k", 'rb') c = int.from_bytes(kfile.read(4), byteorder='big') n = int.from_bytes(kfile.read(4), byteorder='big') kfile.close() while True: char = fd.read(4) if not char: break out.write( rsa_decode(int.from_bytes(char, byteorder='big'), c, n).to_bytes(1, byteorder='big')) fd.close() out.close() output = [filename[:-len(".rsa")], "DECODED"] else: print("Unknown parameter") output = ["", ""] fd.close() return output
def get_gost_base(): while True: # prime number must be greater than any number can be represented in 1 byte q = basic.rand(2**8, 2**9 - 1) if basic.check_prime(q): for i in range(1, 1000): p = i * q + 1 if basic.check_prime(p): break break a = 2 while True: if basic.quick_power(a, q, p) == 1: break else: a += 1 return p, q, a
def prove(sock, n, s): data = sock.recv(1000000) t = int.from_bytes(data, byteorder='big') # print(t) for i in range(0, t): r = basic.rand(1, n - 1) x = (r * r) % n sock.send(x.to_bytes(4, byteorder='big')) data = sock.recv(1000000) e = int.from_bytes(data, byteorder='big') y = (r * pow(s, e)) % n sock.send(y.to_bytes(4, byteorder='big')) data = sock.recv(1000000) ans = data.decode() print(ans)
def elgamal_handler(filename, mode='encode'): fd = open(filename, 'rb') if mode == 'encode': out = open(filename + ".elg", 'wb') kfile = open(filename + ".elgk", 'wb') pair = basic.get_prime_pair() p = pair[1] q = pair[0] g = basic.get_g(p, q) c = basic.rand(2, p - 2) d = basic.quick_power(g, c, p) k = basic.rand(2, p - 2) r = basic.quick_power(g, k, p) kfile.write(r.to_bytes(2, byteorder='big')) kfile.write(p.to_bytes(2, byteorder='big')) kfile.write(c.to_bytes(2, byteorder='big')) kfile.close() while True: char = fd.read(1) if not char: break out.write( elgamal_encode(ord(char), d, k, p).to_bytes(2, byteorder='big')) fd.close() out.close() output = [filename + ".elg", filename + ".elgk"] elif mode == 'decode': out = open(filename[:-len(".elg")], 'wb') kfile = open(filename + "k", 'rb') r = int.from_bytes(kfile.read(2), byteorder='big') p = int.from_bytes(kfile.read(2), byteorder='big') c = int.from_bytes(kfile.read(2), byteorder='big') kfile.close() while True: char = fd.read(2) if not char: break out.write( elgamal_decode(int.from_bytes(char, byteorder='big'), r, p, c).to_bytes(1, byteorder='big')) fd.close() out.close() output = [filename[:-len(".elg")], "DECODED"] else: print("Unknown parameter") fd.close() output = ["", ""] return output
def init_session(n_cards): p = basic.get_prime() pack = {basic.rand(2, p - 1): i for i in range(1, n_cards + 1)} return pack, p
def init_player(p): c, d = basic.get_cd(basic.rand(3, int(sqrt(p))), p) return c, d
def sign(filename, method='rsa'): with open(filename, 'rb') as fd: string = fd.read() m = md5(string) h = m.hexdigest() # print(h) if method == 'rsa': p = basic.get_prime() q = basic.get_prime() n = p * q phi = (p - 1) * (q - 1) keys = basic.get_cd(basic.rand(3, int(sqrt(phi))), phi + 1) c = keys[0] d = keys[1] with open(filename + '-sgn.rsa', 'wb') as sgn: sgn.write(c.to_bytes(4, byteorder='big')) sgn.write(n.to_bytes(4, byteorder='big')) for i in range(0, len(h), 2): x = int(h[i:i + 2], base=16) s = cipher.rsa_encode(x, d, n) sgn.write(s.to_bytes(4, byteorder='big')) elif method == 'elgamal': pair = basic.get_prime_pair() p = pair[1] q = pair[0] g = basic.get_g(p, q) c = basic.rand(2, p - 2) d = basic.quick_power(g, c, p) k = basic.rand(2, p - 2) k, inversed_k = basic.get_cd(k, p) r = basic.quick_power(g, k, p) with open(filename + '-sgn.elg', 'wb') as sgn: sgn.write(p.to_bytes(4, byteorder='big')) sgn.write(g.to_bytes(4, byteorder='big')) sgn.write(r.to_bytes(4, byteorder='big')) sgn.write(d.to_bytes(4, byteorder='big')) for i in range(0, len(h), 2): x = int(h[i:i + 2], base=16) u = (x - c * r) % (p - 1) s = (inversed_k * u) % (p - 1) sgn.write(s.to_bytes(4, byteorder='big')) elif method == 'gost': p, q, a = get_gost_base() c = basic.rand(1, q - 1) d = basic.quick_power(a, c, p) k = basic.rand(1, q - 1) r = basic.quick_power(a, k, p) % q while r == 0: k = basic.rand(1, q - 1) r = basic.quick_power(a, k, p) % q with open(filename + '-sgn.gost', 'wb') as sgn: sgn.write(p.to_bytes(4, byteorder='big')) sgn.write(q.to_bytes(4, byteorder='big')) sgn.write(r.to_bytes(4, byteorder='big')) sgn.write(d.to_bytes(4, byteorder='big')) sgn.write(a.to_bytes(4, byteorder='big')) for i in range(0, len(h), 2): x = int(h[i:i + 2], base=16) s = (k * x + c * r) % q if s == 0: print("Error! Please, resign file.") break else: sgn.write(s.to_bytes(4, byteorder='big')) else: print('Unknown method!') return h
def get_e(): return basic.rand(0, 1)