def privatekey_check(N, p, q, d, e):
    ret = False
    txt = ""

    def getbits(n):
        b = int(math.log(n, 2))
        if b % 2 == 0:
            return b
        else:
            return b + 1

    nlen = getbits(N)
    if gmpy2.is_prime(p) == False:
        ret = True
        txt += "p IS NOT PROBABLE PRIME\n"
    if gmpy2.is_prime(q) == False:
        txt = "q IS NOT PROBABLE PRIME\n"
    if gmpy2.gcd(p, e) > 1:
        ret = True
        txt = "p and e ARE NOT RELATIVELY PRIME\n"
    if gmpy2.gcd(q, e) > 1:
        ret = True
        txt += "q and e ARE NOT RELATIVELY PRIME\n"
    if p * q != N:
        ret = True
        txt += "n IS NOT p * q\n"
    if not (abs(p - q) > (2 ** (nlen // 2 - 100))):
        ret = True
        txt += "|p - q| IS NOT > 2^(nlen/2 - 100)\n"
    if not (p > 2 ** (nlen // 2 - 1)):
        ret = True
        txt += "p IS NOT > 2^(nlen/2 - 1)\n"
    if not (q > 2 ** (nlen // 2 - 1)):
        ret = True
        txt += "q IS NOT > 2^(nlen/2 - 1)\n"
    if not (d > 2 ** (nlen // 2)):
        ret = True
        txt += "d IS NOT > 2^(nlen/2)\n"
    if not (d < gmpy2.lcm(p - 1, q - 1)):
        ret = True
        txt += "d IS NOT < lcm(p-1,q-1)\n"
    unc = (gmpy2.gcd(e - 1, p - 1) + 1) * (gmpy2.gcd(e - 1, q - 1) + 1)
    if unc > 9:
        ret = True
        txt += "The number of unconcealed messages is %d > min\n" % unc
    try:
        inv = gmpy2.invert(e, gmpy2.lcm(p - 1, q - 1))
    except:
        inv = None
        ret = True
        txt += "e IS NOT INVERTIBLE mod lcm(p-1,q-1)\n"
    if d != inv:
        ret = True
        txt += "d IS NOT e^(-1) mod lcm(p-1,q-1)"
    return (ret, txt)
def crt(eqn1, eqn2):
    x1, m1 = eqn1
    x2, m2 = eqn2
    d = int(gmpy2.gcd(m1, m2))
    x = x1 + (m1 // d) * (x2 - x1) * int(gmpy2.invert(m1 // d, m2 // d))
    m = int(gmpy2.lcm(m1, m2))
    return x % m, m
示例#3
0
def elemop(N=1000):
    r'''
    (Takes about 40ms on a first-generation Macbook Pro)
    '''
    for i in range(N):
        assert a+b == 579
        assert a-b == -333
        assert b*a == a*b == 56088
        assert b%a == 87
        assert divmod(a, b) == (0, 123)
        assert divmod(b, a) == (3, 87)
        assert -a == -123
        assert pow(a, 10) == 792594609605189126649
        assert pow(a, 7, b) == 99
        assert cmp(a, b) == -1
        assert '7' in str(c)
        assert '0' not in str(c)
        assert a.sqrt() == 11
        assert _g.lcm(a, b) == 18696
        assert _g.fac(7) == 5040
        assert _g.fib(17) == 1597
        assert _g.divm(b, a, 20) == 12
        assert _g.divm(4, 8, 20) == 3
        assert _g.divm(4, 8, 20) == 3
        assert _g.mpz(20) == 20
        assert _g.mpz(8) == 8
        assert _g.mpz(4) == 4
        assert a.invert(100) == 87
示例#4
0
def get_ed(p, q):
    e = _e_candidate[randint(0, len(_e_candidate) - 1)]

    phi = (p - 1) * (q - 1)

    while (gcd(phi, e) != 1):
        b = randint(0, 2)
        if b == 0:
            e = next_prime(e)
        else:
            e += randint(1, 5)
    d_base = invert(e, lcm(p - 1, q - 1))
    k = randint(0, gcd(p - 1, q - 1) - 1)
    d = d_base + k * lcm(p - 1, q - 1)

    return e, d
示例#5
0
def crt(eqn1, eqn2):
    x1, m1 = eqn1
    x2, m2 = eqn2
    d = int(gmpy2.gcd(m1, m2))
    x = x1 + (m1 // d) * (x2 - x1) * int(gmpy2.invert(m1 // d, m2 // d))
    m = int(gmpy2.lcm(m1, m2))
    return x % m, m
示例#6
0
def lcm(item0, *items):
    """Returns the LCM for multiple items"""
    if not items:
        return item0
    result = item0
    for item in items:
        result = gmpy2.lcm(result, item)
    return int(result)
示例#7
0
 def __init__(self):
     p, q = getMyPrime(512), getMyPrime(512)
     n = p * q
     g = random.randint(1, n * n)
     while GCD(self.L(pow(g, lcm(p - 1, q - 1), n * n), n), n) != 1:
         g = random.randint(1, n * n)
     self.g, self.n = g, n
     print("n =", n)
     print("g =", g)
示例#8
0
def generate_paillier_key(bit):
    p = prime_generate(bit)
    q = prime_generate(bit)
    print("p   = ", hex(p))
    print("q   = ", hex(q))
    N = p * q
    g = N + 1
    lambda_ = lcm(p - 1, q - 1)
    puk = public_key(N, g)
    prk = pirvate_key(puk, lambda_)
    return puk, prk
示例#9
0
    def test_get_lcm(self):
        test_data = 3, 4
        right_answer = 12
        my_answer = self.test_solve.get_lcm(*test_data)
        self.assertEqual(right_answer, my_answer)

        for i in range(10**4):
            test_data = random.randint(1, 10**4), random.randint(1, 10**4)
            right_answer = gmpy2.lcm(*test_data)
            my_answer = self.test_solve.get_lcm(*test_data)
            self.assertEqual(right_answer, my_answer)
def chinese_remainder_theorem(eqn):
    '''
        eqn = [ (y_0, n_0), ... ] where
            x = y_i mod n_i
    '''
    x = 0
    m = 1
    for y, n in eqn:
        d = gmpy2.gcd(m, n)
        x += (m // d) * (y - x) * gmpy2.invert(m // d, n // d)
        m = gmpy2.lcm(m, n)
    return x % m
示例#11
0
def chinese_remainder_theorem(eqn):
    '''
        eqn = [ (y_0, n_0), ... ] where
            x = y_i mod n_i
    '''
    x = 0
    m = 1
    for y, n in eqn:
        d = gmpy2.gcd(m, n)
        x += (m // d) * (y - x) * gmpy2.invert(m // d, n // d)
        m = gmpy2.lcm(m, n)
    return x % m
示例#12
0
 def gen_keys(self):
     """Generate private and public keys."""
     if not (self.pubkey and self.privkey):
         phi = gmpy2.lcm(self.p - 1, self.q - 1)
         if gmpy2.gcd(phi, self.e) != 1:
             raise RSAError(
                 "Unable to generate keys: phi and e are not coprimes")
         d = int(gmpy2.invert(self.e, phi))
     n = self.p * self.q
     self.pubkey = PubKey(n, self.e)
     self.privkey = PrivKey(n, d)
     return self.pubkey, self.privkey
示例#13
0
def lcm(num_a: int, num_b: int) -> int:
    """
    Compute the least common multiple of two input numbers. Uses GMPY2 if available.

    :param num_a: First number a.
    :param num_b: Second number b.
    :return: Least common multiple of a and b.
    """
    if USE_GMPY2:
        return gmpy2.lcm(num_a, num_b)
    # else
    return num_a * num_b // gcd(num_a, num_b)
示例#14
0
def main():
    c = 2242935168115062974499751873249754323110921862340005025797429496249617165457457004035424984007628148314470840283322628336277990956956934305264525083803938682928102121588782027436478869856006901933451768987204715587910773098107493173209888339290058783726917348808343191586406306568637517837353899357179182
    n = 2627153264428508244022299245870943256933139743330880416542975797741751309049801231342077641593189843757280969996185613768596522142633781213226541711288701850667973142372467264747367778283723640648970160596683631157326350160186691083243170812162200614410196661211520277251176842534601959297281680522753579
    e = 65537
    factors = [
        2175580103,
        2282408507,
        2295698099,
        2384501797,
        2407758713,
        2531787773,
        2548448501,
        2578840073,
        2583724721,
        2649339439,
        2778665663,
        2795137091,
        2871391183,
        2883925867,
        2934872777,
        2935540277,
        3167018887,
        3193377149,
        3202853603,
        3252684179,
        3309244537,
        3355822619,
        3375565619,
        3497193893,
        3537623773,
        3580533701,
        3698260381,
        3704371501,
        3717246517,
        4026976607,
        4194174541,
        4226633459,
    ]

    divisor = 1
    for x in factors:
        divisor = gmpy2.lcm(x - 1, divisor)

    totient = divisor
    print("totient: %r" % totient)

    d = gmpy2.invert(e, totient)

    print("d: %r" % d)
    m = pow(c, d, n)
    plain = '%x' % m
    plain = bytearray.fromhex(plain)
    print(plain)
示例#15
0
def genKey(size=4096):
    p = gmp.next_prime(secrets.randbits(int(size / 2)))
    q = p
    while p == q:
        q = gmp.next_prime(secrets.randbits(int(size / 2)))

    n = p * q
    m = gmp.lcm(p - 1, q - 1)

    e = gmp.mpz(65537)
    d = gmp.invert(e, m)

    return {"n": n, "e": e, "d": d}
示例#16
0
def solve(p, q, e, dm, m):
    # Solutions step
    c_lambda = lcm(p-1, q-1)

    # Smallest solution
    d_min = invert(e, c_lambda)

    # Residue; k * lambda = a mod m
    a = (dm - d_min)%m

    k = solve_k(c_lambda%m, a, m)

    return ( d_min + c_lambda * k ) #% ( (p-1) * (q - 1) )
示例#17
0
def main():
    bits = 512
    init = 31337
    count = 16
    error = 10
    limit = 50_000

    pt = int.from_bytes(b'the boy who lived', 'big')
    
    while True:
        r, N, ct, flag = load_parameters(('0.0.0.0', 17101))
        primes = recover_primes(bits, N, r, r, error, limit)

        if primes is None:
            continue

        for p, q in primes:
            if p * q == N:
                break
        else:
            continue
        
        D = (pt * pt - 4) % N
        phi = lcm(p - legendre(D, p), q - legendre(D, q))
        e = recover_exponent(pt, ct, p, q, phi, init, count)

        if e is None:
            continue
        
        D = (flag * flag - 4) % N
        phi = lcm(p - legendre(D, p), q - legendre(D, q))
        d = powmod(e, -1, phi)
        flag = binluc(flag, d, N)

        print(int(flag).to_bytes(1024, 'big').strip(b'\x00'))
        break
示例#18
0
def get_least_steps_to_repeat(ps):
    xc, yc, zc = 0, 0, 0
    x0 = get_axis_state(ps, 0)
    y0 = get_axis_state(ps, 1)
    z0 = get_axis_state(ps, 2)
    for i in range(100000000):
        go_step(ps)

        xs = get_axis_state(ps, 0)
        if xc == 0 and xs == x0:
            xc = i

        ys = get_axis_state(ps, 1)
        if yc == 0 and ys == y0:
            yc = i

        zs = get_axis_state(ps, 2)
        if zc == 0 and zs == z0:
            zc = i

        if xc > 0 and yc > 0 and zc > 0:
            break

    return lcm(lcm(xc + 1, yc + 1), zc + 1)
示例#19
0
def chinese_rm():
    p,q = rabins_test()

    n = gmpy2.mul(p, q)
    c = gmpy2.lcm(p-1, q-1)

    # decoding exponent
    d = gmpy2.powmod(3, -1, c)

    print "\n"
    print "--"*78
    print "n = ", n
    print "--"*78
    print "C(n) = ", c
    print "--"*78
    print "decoding exponent = ", d

    return n,d
示例#20
0
def chinese_rm():
    p, q = rabins_test()

    n = gmpy2.mul(p, q)
    c = gmpy2.lcm(p - 1, q - 1)

    # decoding exponent
    d = gmpy2.powmod(3, -1, c)

    print "\n"
    print "--" * 78
    print "n = ", n
    print "--" * 78
    print "C(n) = ", c
    print "--" * 78
    print "decoding exponent = ", d

    return n, d
示例#21
0
def rsa_conspicuous_check(N, p, q, d, e):
    ret = 0
    txt = ""
    nlen = int(math.log(N) / math.log(2))
    if gmpy2.is_prime(p) == False:
        ret = -1
        txt += "p IS NOT PROBABLE PRIME\n"
    if gmpy2.is_prime(q) == False:
        txt = "q IS NOT PROBABLE PRIME\n"
    if gmpy2.gcd(p, e) > 1:
        ret = -1
        txt = "p and e ARE NOT RELATIVELY PRIME\n"
    if gmpy2.gcd(q, e) > 1:
        ret = -1
        txt += "q and e ARE NOT RELATIVELY PRIME\n"
    if p * q != N:
        ret - 1
        txt += "n IS NOT p * q\n"
    if not (abs(p - q) > (2**(nlen // 2 - 100))):
        ret - 1
        txt += "|p - q| IS NOT > 2^(nlen/2 - 100)\n"
    if not (p > 2**(nlen // 2 - 1)):
        ret - 1
        txt += "p IS NOT > 2^(nlen/2 - 1)\n"
    if not (q > 2**(nlen // 2 - 1)):
        ret - 1
        txt += "q IS NOT > 2^(nlen/2 - 1)\n"
    if not (d > 2**(nlen // 2)):
        ret - 1
        txt += "d IS NOT > 2^(nlen/2)\n"
    if not (d < gmpy2.lcm(p - 1, q - 1)):
        txt += "d IS NOT < lcm(p-1,q-1)\n"
    try:
        inv = gmpy2.invert(e, lcm(p - 1, q - 1))
    except:
        inv = None
        ret = -1
        txt += "e IS NOT INVERTIBLE mod lcm(p-1,q-1)\n"
    if d != inv:
        ret = -1
        txt += "d IS NOT e^(-1) mod lcm(p-1,q-1)"
    return (ret, txt)
示例#22
0
def keygen(n_len=2048):
    n = 0
    p = q = None
    while n.bit_length() != n_len and p == q:
        p = generate_prime(n_len // 2)
        q = generate_prime(n_len // 2)
        n = p * q        

    l = lcm(p-1, q-1)
    if gcd(p*q, (p-1)*(q-1)) != 1:
        raise RuntimeException("Critical error: gcd(N,tot(N)) != 1")

    g = mpz()
    n_sqr = n**2
    while gcd(g, n_sqr) != 1:
        g = random_lt_n(n)

    u = invert(lf(powmod(g, l, n_sqr), n), n)
    public_key = PaillierPublicKey(n, g)
    private_key = PaillierPrivateKey(l, u, n)
    return PaillierKeyPair(public_key, private_key)
from Crypto.Util.number import getPrime, bytes_to_long
import gmpy2
from secret import hint, flag

assert len(hint) == 28

p, q = getPrime(1024), getPrime(1024)
N = p * q
# N = 13210885841475518997568732164505399160965975732709166440636514294147889012939962719747539825751923393125345841105194204835106865501597494306950242851021806919844457001207940087403826453676960820293604664852256329422339043270905696645969167335653171499672006516853649796861604166761519002960115698076234349938629174196811631219016260092670861554168242659812550025656836617235464100043662486564305360514744784304117455647758598410090081092904051134426154172210241012032835402789503490147182192969447649332989505033061036275185561204430838918866613413178664432425710430112085580833238357752115397317954683681495273481389
lam = gmpy2.lcm(p - 1, q - 1)
d1, d2 = getPrime(700), getPrime(700)
e1, e2 = gmpy2.invert(d1, lam), gmpy2.invert(d2, lam)
# e1 = 2362084139893405093100851500036437427130753588874770144214789282673606056995400452788220040281778791657867579436414252823552777228138008313512389117391660371862515728725314731064077161987981532020815534191123548980295455247064358562847786091773315905741298461223652815627950324240999370034314278563662587111397996345472564435937644492350734358340274983731113113635179437404528435163643061230423800728267343681110923935960537320270088154823842363204982299121314474873903268514696308154685325185965860497240479658442957806291665318879425962161464553028085194356710217166887400805442731868081113238642645694081223918279
# e2 = 3414563644994824673043321903579545055064146345197255378925441682500004270176946976155926310465466980633244227451321649151667496995568059258205501094869245772888762971415898595773497552721465370122131433357444025304164265928843912360131588986154074442926163483255945248639016376316121291379194754917958825314218152923102469383434911228730021468050093165348698614766633636600000928215593854869167057725013823299777115974128549538363855171997713965108293531424982357531477437917670830142806697234090847241145606725000986664742983223795093957257772115045975670612945728721064289031105561620681130223256874848014328627387
m = bytes_to_long(flag)
c = pow(m, 65537, N)
# c = 7509553860783555621436770331937084571808765391952325412927571489888110953335079989946907174565869802637698429436373777452178173489559054167708787358950733332569061899871857436220178404228920207316305775763298358672742916672692448339586826683320641684274797705468243189922260974401036329516370582452616835330578465913847623960113615530202243536961528874664359484225419012604926739741446652801517754617125097714308718696159416774441152786882175474276850952376624168400656872284235826980641374657158962776898393544419094412396806119989817549523336265300100681211520861433014873204547877014541979273900480702892070278171

#p1, q1 = getPrime(1024), getPrime(1024)
#N1 = p1 * q1
#p0 = p1 ^ (bytes_to_long(hint)<<444)
#assert N1==22752894188316360092540975721906836497991847739424447868959786578153887300450204451741779348632585992639813683087014583667437383610183725778340014971884694702424840759289252997193997614461702362265455177683286566007629947557111478254578643051667900283702126832059297887141543875571396701604215946728406574474496523342528156416445367009267837915658813685925782997542357007012092288854590169727090121416037455857985211971777796742820802720844285182550822546485833511721384166383556717318973404935286135274368441649494487024480611465888320197131493458343887388661805459986546580104914535886977559349182684565216141697843L
#assert p0==165268930359949857026074503377557908247892339573941373503738312676595180929705525120390798235341002232499096629250002305840384250879180463692771724228098578839654230711801010511101603925719055251331144950208399022480638167824839670035053131870941541955431984347563680229468562579668449565647313503239028017367L
示例#24
0
e = 59107
p = 228517792080140341
q = 1675909164550923263854591345270445396052847869117231939809062226222204253885693425526434134321712288675268468398852452684029376569327518089966506865838909486699078280423099271324646863671350838232140981094611254627738568184261530942469845202934677427234062382272736609418198352717

n = p * q


def cadd(a, b, n):
    return (a[0] + b[0] % n, a[1] + b[1] % n)


def cmul(a, b, n):
    return ((a[0] * b[0] - a[1] * b[1]) % n, (a[0] * b[1] + a[1] * b[0]) % n)


def cpow(a, k, n):
    if (k == 0):
        return (1, 0)
    if (k == 1):
        return a
    if (k % 2 == 0):
        a = cmul(a, a, n)
        return cpow(a, k / 2, n)
    else:
        return cmul(a, cpow(cmul(a, a, n), (k - 1) / 2, n), n)


o = lcm((p * p - 1), (q * q - 1))
fm = cpow(f, invert(e, o), n)
print fm
示例#25
0
文件: lcm2.py 项目: b04902036/ctf
from gmpy2 import lcm
e1 = 114194
e2 = 130478
e3 = 122694
e4 = 79874
ret = (lcm(e1, lcm(e2, lcm(e3, e4))))
e11 = ret // e1
e21 = ret // e2
e31 = ret // e3
e41 = ret // e4
print(e11)
print(e21)
print(e31)
print(e41)
示例#26
0
from pwn import *
from gmpy2 import lcm,iroot,invert

def fermat(n, verbose=True):
    a = iroot(n,2)[0]+1
    b = a*a - n
    while not iroot(b,2)[1]:
        a = a + 1
        b = a*a - n
    b = iroot(b,2)[0]
    p = a + b
    q = a - b
    return p, q

n = 0x6216bfea994a31f3a352e8f162b1b6896025ba91188a458daa0aa758d4ecb595089aaa379a8b3c2c1bde708e6bbb0fa99dce996b8d9f259e319c881e41bc8635d348c6004325dae4d3a6bfe78e62499f819cd9bd74686943c7cbe9b68372bb43dc375341bae69120ee763cb282ddf0f117a150aa3c862bdad372401220caa3a1fb1dd6c369d4d5dbd78f15f40e0bbebb6f3fa123a5756d10fb62e49f7c73aa171b007a281de6910dfc67aae5a691c3329a5c64700b0b54ceaeaa95639c6030925f190f587f53ee9d718e0e7dfa2b059b1a6a701620b058498cd2c2ebeac76153150b8886fcfc99d35d10139f9d364c7393c70181569e2269d5ff214d5e6a253
c = 0x725f17256760e05a02359447947aeb7d83fa7350408dc1abb1ba03ff9e1c15167171c6f493620005ac5e2c641912e5c09cbeea9fed542bf7dbd8016bf3ce5758bf83f1d941b8926992816912e47a62344c4d0ca068a071933c98300a073930be59f051cda8bc8fc69e61a490090a95c18380877c73b230b4a65ecb676bcfa0a7b2d8ce61a6857b822bea9304686e6393f03040489926e940e1f098fdd9ceb74d54e972625141bc7fb322c68bb00f27c030286c1d1e71e861f70ff08c17d5461261ee44614c6fb94e5490d86669f6b9af6fd6a141cb0c39ac692e7d51f3ab28bd71b3b5502c5be6ebbec6622065ce60be64247e7cd2298c746716fc686981f7

p, q = fermat(n)
d = invert(65537,lcm(p-1,q-1))
m = pow(c,d,n)
print hex(m)[2:].decode('hex')
示例#27
0
# l=set()
# for i in range(2,n+1):
#     for j in range(i+1,n+1):
#         a=set()
#         if math.gcd(i,j)<2:
#             a.add(i)
#             a.add(j)
#         l.add(a)

# print(l)
# def coprime(A,B):
#     return len([(x,y) for x in range(1,A+1) for y in range(x+1,B+1) if math.gcd(x,y) == 1])
# print(coprime(1,5))
# from collections import deque

# def coprimes():
#     tree = deque([[2, 1], [3, 1]])
#     while True:
#         m, n = tree.popleft()
#         yield m, n
#         tree.append([2 * m - n, m])
#         tree.append([2 * m + n, m])
#         tree.append([m + 2 * n, n])
#     print(tree)
# coprimes()
from gmpy2 import lcm
from itertools import combinations

for a, b in combinations(range(2, 101), 2):
    if lcm(a, b) == a * b:
        print('({}, {}), '.format(a, b), end=' ')
示例#28
0
def getLCM(a, b):
    return gmpy2.lcm(mpz(a), mpz(b))
示例#29
0
from gmpy2 import invert, lcm

p = 2**1023 + 1155
q = 2**1024 + 643
n = p * q
e = 65537
l = lcm(p - 1, q - 1)
d = invert(e, l)
c = 2426759448926300374092713562742363835064624596734803758771558596637476435050549322291385219226509562405891022145248939846259187578407323336264977796035006051101456740659780915043839753835115842400821067812149630208916162819303078177680527198216553101252744997907265952123874930177443569829481627316683242014717052901748219013647067408530007363343371724907285664511595816386074309444579803282707739982294866598538682087302380145036139213779079021485602590035788284515090342916438525807114219193157567634647634786348429443139872928990365019329208810611558637131836578485907804972498521065186490984311407009061159731115
m = pow(c, d, n)
print hex(m)[2:].decode('hex')
示例#30
0
from Crypto.Util.number import long_to_bytes
from Crypto.PublicKey.RSA import import_key
from gmpy2 import lcm

c = 16267540901004879123859424672087486188548628828063789528428674467464407443871599865993337555869530486241139138650641838377419734897801380883629894166353225288006148210453677023750688175192317241440457768788267270422857060534261674538755743244831152470995124962736526978165448560149498403762447372653982922113772190234143253450918953235222315161964539311032659628670417496174123483045439359846360048774164337257829398345686635091862306204455687347443958931441225500856408331795261329035072585605404416473987280037959184981453888701567175803979981461050532113072292714696752692872526424122826696681194705563391161137426703690900733706866842363055967856443765215723398555522126909749236759332964873221973970368877565410624895160438695006432021529071866881905134494489266801004903504121740435965696128048690741210812963902631391765192187570107372453917327060678806282122942318369245760773848604249664378721970318257356486696764545

# Use RSACTFTOOL with e,n create private.key

pk = import_key(open('private.key', 'r').read())

# See note (p > q)
q, p = pk.p, pk.q

# See https://en.wikipedia.org/wiki/Schmidt-Samoa_cryptosystem

n = p * p * q
d = pow(n, -1, lcm(p - 1, q - 1))
m = long_to_bytes(pow(c, d, p * q)).decode()
print(m)
示例#31
0
    while True:
        u = getPrime(bit / 4 - num)
        if gmpy2.gcd(u, phi_n) != 1:
            continue
        t = gmpy2.invert(u, phi_n)
        e = bytes_to_long(des_encrypt(long_to_bytes(t)))
        if gmpy2.gcd(e, phi_n) == 1:
            break
    return (n, e)


P = getPrime(1024)
Q = getPrime(1024)
N = P * Q
E = 65537
lcm = gmpy2.lcm(P - 1, Q - 1)
e1 = gmpy2.invert(getPrime(730), lcm)
e2 = gmpy2.invert(getPrime(730), lcm)
m = bytes_to_long(flag)
c = pow(m, E, N)
print "N = " + str(N)
print "e2 = " + str(e2)
print "c = " + str(c)
_n, _e = gen_key()
_c = pow(e1, _e, _n)
print "_n = " + str(_n)
print "_e = " + str(_e)
print "_c = " + str(_c)

# N = 14922959775784066499316528935316325825140011208871830627653191549546959775167708525042423039865322548420928571524120743831693550123563493981797950912895893476200447083386549353336086899064921878582074346791320104106139965010480614879592357793053342577850761108944086318475849882440272688246818022209356852924215237481460229377544297224983887026669222885987323082324044645883070916243439521809702674295469253723616677245762242494478587807402688474176102093482019417118703747411862420536240611089529331148684440513934609412884941091651594861530606086982174862461739604705354416587503836130151492937714365614194583664241
# e2 = 27188825731727584656624712988703151030126350536157477591935558508817722580343689565924329442151239649607993377452763119541243174650065563589438911911135278704499670302489754540301886312489410648471922645773506837251600244109619850141762795901696503387880058658061490595034281884089265487336373011424883404499124002441860870291233875045675212355287622948427109362925199018383535259913549859747158348931847041907910313465531703810313472674435425886505383646969400166213185676876969805238803587967334447878968225219769481841748776108219650785975942208190380614555719233460250841332020054797811415069533137170950762289
示例#32
0
文件: api.py 项目: Captaincheq/tools
def big_num():
    operator = request.values.get('operator')
    result = '1'

    try:
        if operator == 'FACT' or operator == 'isPrime':
            p = gmpy2.mpz(int(request.values.get('p')))
            if not p:
                resu = {'code': 10001, 'message': '参数不能为空!'}
                return json.dumps(resu, ensure_ascii=False)
            if operator == 'FACT':  #p的阶层
                result = gmpy2.fac(p)
            elif operator == 'isPrime':  #判断p是否是素数
                result = gmpy2.is_prime(p)
        elif operator == 'MULMN' or operator == 'POWMN':
            p = gmpy2.mpz(int(request.values.get('p')))
            q = gmpy2.mpz(int(request.values.get('q')))
            n = gmpy2.mpz(int(request.values.get('n')))
            if not p and not q and not n:
                resu = {'code': 10001, 'message': '参数不能为空!'}
                return json.dumps(resu, ensure_ascii=False)
            if operator == 'POWMN':  #计算p**q mod n
                result = gmpy2.powmod(p, q, n)
            elif operator == 'MULMN':  #计算p*q mod n
                result = gmpy2.modf(gmpy2.mul(p, q), n)
        else:
            p = gmpy2.mpz(int(request.values.get('p')))
            q = gmpy2.mpz(int(request.values.get('q')))
            if not p and not q:
                resu = {'code': 10001, 'message': '参数不能为空!'}
                return json.dumps(resu, ensure_ascii=False)
            if operator == 'ADD':  #相加
                print('good')
                result = gmpy2.add(p, q)
            elif operator == 'SUB':  #相减
                result = gmpy2.sub(p, q)
            elif operator == 'MUL':  #相乘
                result = gmpy2.mul(p, q)
            elif operator == 'DIV':  #相除
                result = gmpy2.div(p, q)
            elif operator == 'MOD':  #取余
                result = gmpy2.f_mod(p, q)
            elif operator == 'POW':
                result = gmpy2.powmod(p, q)
            elif operator == 'GCD':  #最大公因数
                result = gmpy2.gcd(p, q)
            elif operator == 'LCM':
                result = gmpy2.lcm(p, q)
            elif operator == 'OR':  #或
                result = p | q
            elif operator == 'AND':  #与
                result = p & q
            elif operator == 'XOR':  #抑或
                result = p ^ q
            elif operator == 'SHL':  #左移
                result = p << q
            elif operator == 'SHR':  #右移
                result = p >> q
        resu = {'code': 200, 'result': str(result)}
        return json.dumps(resu, ensure_ascii=False)
    except:
        resu = {'code': 10002, 'message': '异常。'}
        return json.dumps(resu, ensure_ascii=False)
示例#33
0
for k in to_remove:
    del constraints[k]

lcm = 1
modulo = 0

to_remove = set()

for k, v in constraints.items():
    all_allowed = list(filter(lambda x: v[x], range(0, k)))
    if len(all_allowed) == 1:
        allowed = all_allowed[0]
        while modulo % k != allowed:
            modulo += lcm
        lcm = gmpy2.lcm(lcm, k)
        to_remove.add(k)

for k in to_remove:
    del constraints[k]

complete = False
while not complete:
    complete = True
    for k, v in constraints.items():
        if not v[modulo % k]:
            complete = False
            break

    if complete:
        break