Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
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
Пример #5
0
from protection import basic
from math import sqrt
from random import randint

print("Quick power: " + str(
    basic.quick_power(randint(2, 100000000), randint(7, 20000000),
                      basic.get_prime())))
print("Euclid: " +
      str(basic.common_euclid(basic.get_prime(), basic.get_prime())))
pair = basic.get_prime_pair()
g = basic.get_g(pair[0], pair[1])
print(pair)
print(g)
print("Check: " + str(basic.quick_power(g, pair[0], pair[1])))
print("Diffie " + str(basic.diff_hell([7, 13], [g, pair[1]])))
m = int(sqrt(pair[0])) + 1
k = m
print("Child & giant steps " + str(basic.steps(9, 2, pair[0], m, k)))