示例#1
0
def getStrongPrime():
    bit_count = 512

    rand_state = gmp.random_state(getRandomSeed())
    rand_no = gmp.mpz_rrandomb(rand_state, bit_count)
    p1 = getNextPrime(rand_no)
    p2 = getNextPrime(p1)
    p3 = getNextPrime(p2)
    while (not StrongPrime(p1, p2, p3)):
        rand_state = gmp.random_state(getRandomSeed())
        rand_no = gmp.mpz_rrandomb(rand_state, bit_count)
        p1 = getNextPrime(rand_no)
        p2 = getNextPrime(p1)
        p3 = getNextPrime(p2)
    return p2
示例#2
0
def main():
    s = socket.socket()
    s.connect((agree.host, flag.port))
    print "connecting..."
    a = gm.mpz_rrandomb(agree.seed, 20)
    A = gm.powmod(agree.g, a, agree.N)
    message = agree.I + "," + str(A).encode("base_64").replace("\n", "") + "\n"
    s.send(message)
    message = s.recv(1024)
    message = message.split(",")
    salt = int(message[0].decode("base_64"))
    B = int(message[1].decode("base_64"))
    uH = hashlib.sha256(str(A) + str(B)).hexdigest()
    u = int(uH, 16)
    # xH = hashlib.sha256(str(salt) + "wrong_password").hexdigest()
    xH = hashlib.sha256(str(salt) + flag.password).hexdigest()
    x = int(xH, 16)
    S = B - agree.k * gm.powmod(agree.g, x, agree.N)
    S = gm.powmod(S, (a + u * x), agree.N)
    K = hashlib.sha256(str(S)).hexdigest()
    h = hashlib.sha256(K + str(salt)).hexdigest() + "\n"
    s.send(h)
    message = s.recv(1024)
    print message

    s.close()
示例#3
0
文件: crypto.py 项目: hzshang/ctf
def random_prime(size):
    seed = int.from_bytes(os.urandom(16), byteorder='little')
    state = gmpy2.random_state(seed)
    rnd = gmpy2.mpz_rrandomb(state, size)
    while True:
        prime = gmpy2.next_prime(rnd)
        if prime.bit_length() == size:
            return prime
示例#4
0
def test_pack_unpack(repeat):
    """Test gmpy2.pack and gmpy2.unpack."""
    r = gmpy2.random_state(42)
    for counter in range(repeat):
        for t in (10, 1000, 2000, 10000, 100000):
            v = gmpy2.mpz_rrandomb(r, t)
            for b in range(1, max(1001, t)):
                temp = gmpy2.unpack(v, b)
                u = gmpy2.pack(temp, b)
                assert u == v
示例#5
0
def test_pack_unpack(repeat):
    """Test gmpy2.pack and gmpy2.unpack."""
    r = gmpy2.random_state(42)
    for counter in range(repeat):
        for t in (10, 1000, 2000, 10000, 100000):
            v = gmpy2.mpz_rrandomb(r, t)
            for b in range(1, max(1001,t)):
                temp = gmpy2.unpack(v, b)
                u = gmpy2.pack(temp, b)
                assert u == v
示例#6
0
def test(repeat=1):
    """Test gmpy2.pack and gmpy2.unpack."""
    r = gmpy2.random_state(42)
    try:
        for counter in range(repeat):
            for t in (10, 30, 60, 500, 1000, 2000, 10000, 100000):
                v = gmpy2.mpz_rrandomb(r, t)
                for b in range(1, max(1001,t)):
                    temp = gmpy2.unpack(v, b)
                    u = gmpy2.pack(temp, b)
                    if u != v:
                        raise ValueError
        return True
    except ValueError:
        return False
示例#7
0
import gmpy2 as gm
import time

context.log_level = 'debug'

host = "game.suctf.asuri.org"
port = 10002
N = 36193562045147198226267685786822513311372299890288906766214451758408242669170809891962145407277585270045757267352741959591666164323206252522725650747399322790206415639159080845612923648630789946593188406828731691439931929756857366720659518874963050327900921198273525530080232505943436085962475711466384128327
P = "password"
g = 2
k = 3
I = "username"
seed = gm.random_state(int(time.time()))

r = remote(host, port)
a = gm.mpz_rrandomb(seed, 20)
# A = gm.powmod(g, a, N)
A = 0
message = I + "," + str(A).encode("base_64").replace("\n", "")

r.sendline(message)
message = r.recv(1024)
message = message.split(",")
# print message
salt = int(message[0].decode("base_64"))
B = int(message[1].decode("base_64"))
# print salt, B

uH = hashlib.sha256(str(A) + str(B)).hexdigest()
u = int(uH, 16)
xH = hashlib.sha256(str(salt) + "sadhosnaoi").hexdigest()
def generate_prime(bits):
    temp = gmpy2.mpz_rrandomb(rand_state, bits)
    return gmpy2.next_prime(temp)
def generate_prime(state, bit_count):
    rand_state = gmpy2.random_state(state)
    temp = gmpy2.mpz_rrandomb(rand_state, bit_count)
    return gmpy2.next_prime(temp)
示例#10
0
def getRandomPrime():
    bits = random.randrange(5, 100)
    rand_state = gmp.random_state(getRandomSeed())
    rand_no = gmp.mpz_rrandomb(rand_state, bits)
    return getNextPrime(rand_no)
示例#11
0
def generate_prime(bit_count: int, rand_state: int) -> int:
    # temp = 0
    temp = gmpy2.mpz_rrandomb(rand_state, bit_count)
    while not gmpy2.is_bpsw_prp(temp):  # Strong Prime Check
        temp = gmpy2.mpz_rrandomb(rand_state, bit_count)
    return temp
示例#12
0
文件: babymade.py 项目: b04902036/ctf
#!/usr/bin/env python
import gmpy2
import numpy as np
# from secret import exp, key
exp = 3
ran = gmpy2.random_state()
key = gmpy2.mpz_rrandomb(ran, 512)
def encrypt(exp, num, key):
    assert key >> 512 <= 1
    num = num + key
    msg = bin(num)[2:][::-1]
    C, i = 0, 1
    cnt = 0
    now = exp
    now2 = -1
    for b in msg:
        cnt += 1
        C += int(b) * (now + now2)
        i += 1
        now *= exp
        now2 *= -1
        if cnt % 20000 == 0:
            print (cnt * 1.0 / len(msg))
    try:
        enc = hex(C)[2:].rstrip('L').decode('hex')
    except:
        enc = ('0' + hex(C)[2:].rstrip('L')).decode('hex')
    return enc

#-----------------------------
# Encryption: