def generatePrimeBetter(low, high):
    primeList = []

    while (primefac.nextprime(low) < high):
        primeList.append(primefac.nextprime(low))
        low = primefac.nextprime(low)

    return random.choice(primeList)
Example #2
0
def getPrime(n):
    n /= 8
    m4x = 1
    while(n):
        m4x = m4x << 8
        n -= 1
    m4x -= 1
    m1n = (m4x + 1) >> 4
    tmp = randint(m1n,m4x)
    return nextprime(tmp)
Example #3
0
def genprime(l):
    l /= 8
    big = 1
    while l:
        big = big << 8
        l -= 1
    big -= 1
    small = (big + 1) >> 4
    temp = random.randint(small, big)
    return primefac.nextprime(temp)
Example #4
0
def problem_yafu_cheat_check(conn, address, teamtoken):
    (ip, port) = address
    conn.send("=next-rsa=\n")
    q = 0xab724df05ca87067ce1573550a6a05f41f93e910b0380c71cdc5de940ef790a475f5c3d512354bc57b3410e7f5158fb287f79397353acb169ef583260eec76f4c46d21e4cb43426e3c66ba9b75d3c1b009ff1f9a0fea9c7d9815eadc5f7ac776d6dcae3c1fa3de865253623b4121e6b4f51deea0b7ae9ca84aad5fe83ba56451L
    seed = int(hashlib.sha256(ip).hexdigest()[0:8], 16)
    p = primefac.nextprime(seed)
    n = p * q
    e = 0x10001
    conn.send("# n=" + hex(n).replace("L", "") + "\n")
    conn.send("# e=" + hex(e).replace("L", "") + "\n")
    m = random.randint(0x100000000000, 0xffffffffffff)
    c = pow(m, e, n)
    conn.send("# c=" + hex(c).replace("L", "") + "\n")
    conn.send("@ m=")
    rec_m = conn.recv(1024).strip()
    if rec_m == hex(m).replace("L", ""):
        d = primefac.modinv(e, (p - 1) * (q - 1))
        if d < 0:
            d += (p - 1) * (q - 1)
        successdn.append((d, n))
        f = open("success/" + ip, "a")
        f.write("teamtoken:" + teamtoken + "\n" + "time:" +
                time.strftime('%Y%m%d%H%M%S') + "\nm:" +
                hex(m).replace("L", "") + "\nd:" + hex(d).replace("L", "") +
                "\nn:" + hex(n).replace("L", "") + "\n\n")
        f.close()
        ok(conn)
        return 0
    else:
        for (cheat_d, cheat_n) in successdn:
            print "cheat!"
            rec_m_int = int(rec_m[0:], 16)
            if rec_m_int == pow(c, cheat_d, cheat_n):
                f = open("cheat/" + ip, "a")
                f.write("teamtoken:" + teamtoken + "\n" + "time:" +
                        time.strftime('%Y%m%d%H%M%S') + "\nm:" +
                        hex(rec_m_int).replace("L", "") + "\nd:" +
                        hex(cheat_d).replace("L", "") + "\nn:" +
                        hex(cheat_n).replace("L", "") + "\n\n")
                f.close()
                ok(conn)
                return 1
        error(conn)
        return 0
Example #5
0
import socket
import thread
import hashlib
import random
import time
import primefac
import os
import hide
import logging
#cheat detect preload
successdn=[]
files = os.listdir("success/")
for filename in files:
    print "load",filename
    seed = int(hashlib.sha256(filename).hexdigest()[0:8], 16)
    p = primefac.nextprime(seed)
    q=0xab724df05ca87067ce1573550a6a05f41f93e910b0380c71cdc5de940ef790a475f5c3d512354bc57b3410e7f5158fb287f79397353acb169ef583260eec76f4c46d21e4cb43426e3c66ba9b75d3c1b009ff1f9a0fea9c7d9815eadc5f7ac776d6dcae3c1fa3de865253623b4121e6b4f51deea0b7ae9ca84aad5fe83ba56451L
    e=65537
    d=primefac.modinv(e,(p-1)*(q-1))
    if d<0:
        d+=(p-1)*(q-1)
    successdn.append((d,p*q))
def error(conn):
    conn.send("sorry!\n")
    conn.close()
def ok(conn):
    conn.send("ok!\n")
def proof(conn):
    conn.send("Firstly, please give me the proof of your work!\n")
    x=chr(random.randint(0,0xff))+chr(random.randint(0,0xff))+chr(random.randint(0,0x1f))
    conn.send("x=chr(random.randint(0,0xff))+chr(random.randint(0,0xff))+chr(random.randint(0,0x1f))\n")