Example #1
0
def generate_c(bits, randfunc, progress_func=None):
    # Generate the prime factors of n
    if progress_func:
        progress_func('p,q\n')

    p = q = 1L
    while number.size(p * q) < bits:
        p = pubkey.getPrime(bits / 2, randfunc)
        q = pubkey.getPrime(bits / 2, randfunc)

    # p shall be smaller than q (for calc of u)
    if p > q:
        (p, q) = (q, p)
    if progress_func:
        progress_func('u\n')
    u = pubkey.inverse(p, q)
    n = p * q

    e = 65537L
    if progress_func:
        progress_func('d\n')
    d = pubkey.inverse(e, (p - 1) * (q - 1))
    key = _fastmath.rsa_construct(n, e, d, p, q, u)
    obj = RSAobj_c(key)

    ##    print p
    ##    print q
    ##    print number.size(p), number.size(q), number.size(q*p),
    ##    print obj.size(), bits
    assert bits <= 1 + obj.size(), "Generated key is too small"
    return obj
Example #2
0
def generate_c(bits, randfunc, progress_func = None):
    # Generate the prime factors of n
    if progress_func:
        progress_func('p,q\n')

    p = q = 1L
    while number.size(p*q) < bits:
        p = pubkey.getPrime(bits/2, randfunc)
        q = pubkey.getPrime(bits/2, randfunc)

    # p shall be smaller than q (for calc of u)
    if p > q:
        (p, q)=(q, p)
    if progress_func:
        progress_func('u\n')
    u=pubkey.inverse(p, q)
    n=p*q

    e = 65537L
    if progress_func:
        progress_func('d\n')
    d=pubkey.inverse(e, (p-1)*(q-1))
    key = _fastmath.rsa_construct(n,e,d,p,q,u)
    obj = RSAobj_c(key)

##    print p
##    print q
##    print number.size(p), number.size(q), number.size(q*p),
##    print obj.size(), bits
    assert bits <= 1+obj.size(), "Generated key is too small"
    return obj
Example #3
0
def generate_py(bits, randfunc, progress_func=None):
    """generate(bits:int, randfunc:callable, progress_func:callable)

    Generate an RSA key of length 'bits', using 'randfunc' to get
    random data and 'progress_func', if present, to display
    the progress of the key generation.
    """
    obj=RSAobj()
    obj.e = 65537L

    # Generate the prime factors of n
    if progress_func:
        progress_func('p,q\n')
    p = q = 1L
    while number.size(p*q) < bits:
        # Note that q might be one bit longer than p if somebody specifies an odd
        # number of bits for the key. (Why would anyone do that?  You don't get
        # more security.)
        #
        # Note also that we ensure that e is coprime to (p-1) and (q-1).
        # This is needed for encryption to work properly, according to the 1997
        # paper by Robert D. Silverman of RSA Labs, "Fast generation of random,
        # strong RSA primes", available at
        #   http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.17.2713&rep=rep1&type=pdf
        # Since e=65537 is prime, it is sufficient to check that e divides
        # neither (p-1) nor (q-1).
        p = 1L
        while (p - 1) % obj.e == 0:
            if progress_func:
                progress_func('p\n')
            p = pubkey.getPrime(bits/2, randfunc)
        q = 1L
        while (q - 1) % obj.e == 0:
            if progress_func:
                progress_func('q\n')
            q = pubkey.getPrime(bits - (bits/2), randfunc)

    # p shall be smaller than q (for calc of u)
    if p > q:
        (p, q)=(q, p)
    obj.p = p
    obj.q = q

    if progress_func:
        progress_func('u\n')
    obj.u = pubkey.inverse(obj.p, obj.q)
    obj.n = obj.p*obj.q

    if progress_func:
        progress_func('d\n')
    obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))

    assert bits <= 1+obj.size(), "Generated key is too small"

    return obj
def generate_pq(modbits):
	# generate primes p ad q
	# stolen from pycrypt
        p = q = 1L
        while number.size(p*q) < modbits:
                if modbits > 512 :
                        # Note that q might be one bit longer than p if somebody specifies an odd
                        # number of bits for the key. (Why would anyone do that?  You don't get
                        # more security.)
                        p = pubkey.getStrongPrime(modbits>>1, 0, 1e-12, None)
                        q = pubkey.getStrongPrime(modbits - (modbits>>1), 0, 1e-12, None)
                else :
                        p = pubkey.getPrime(modbits>>1, None)
                        q = pubkey.getPrime(modbits - (modbits>>1), None)

	return (p, q)
Example #5
0
def generate_pq(modBits):
    '''
        Generate p, q (public key) with modBits bits for SRA encryption
        generate primes p and q, from Sefasi and pycrypt
    '''
    assert (modBits % 2 == 0)  # must provide even number of bits
    p = q = 1L
    while number.size(p * q) < modBits:
        if modBits > 512:
            p = pubkey.getStrongPrime(modBits >> 1, 0, 1e-12, None)
            q = pubkey.getStrongPrime(modBits - (modBits >> 1), 0, 1e-12, None)
        else:
            p = pubkey.getPrime(modBits >> 1, None)
            q = pubkey.getPrime(modBits - (modBits >> 1), None)

    return (p, q)
Example #6
0
def generate_pq(modbits):
    # generate primes p ad q
    # stolen from pycrypt
    p = q = 1L
    while number.size(p * q) < modbits:
        if modbits > 512:
            # Note that q might be one bit longer than p if somebody specifies an odd
            # number of bits for the key. (Why would anyone do that?  You don't get
            # more security.)
            p = pubkey.getStrongPrime(modbits >> 1, 0, 1e-12, None)
            q = pubkey.getStrongPrime(modbits - (modbits >> 1), 0, 1e-12, None)
        else:
            p = pubkey.getPrime(modbits >> 1, None)
            q = pubkey.getPrime(modbits - (modbits >> 1), None)

    return (p, q)
Example #7
0
def generate_py(bits, randfunc, progress_func=None):
    """generate(bits:int, randfunc:callable, progress_func:callable)

    Generate an RSA key of length 'bits', using 'randfunc' to get
    random data and 'progress_func', if present, to display
    the progress of the key generation.
    """
    obj=RSAobj()

    # Generate the prime factors of n
    if progress_func:
        progress_func('p,q\n')
    p = q = 1L
    while number.size(p*q) < bits:
        # Note that q might be one bit longer than p if somebody specifies an odd
        # number of bits for the key. (Why would anyone do that?  You don't get
        # more security.)
        p = pubkey.getPrime(bits/2, randfunc)
        q = pubkey.getPrime(bits - (bits/2), randfunc)

    # p shall be smaller than q (for calc of u)
    if p > q:
        (p, q)=(q, p)
    obj.p = p
    obj.q = q

    if progress_func:
        progress_func('u\n')
    obj.u = pubkey.inverse(obj.p, obj.q)
    obj.n = obj.p*obj.q

    obj.e = 65537L
    if progress_func:
        progress_func('d\n')
    obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))

    assert bits <= 1+obj.size(), "Generated key is too small"

    return obj
Example #8
0
def generate(bits, randfunc, progress_func=None):
    """generate(bits:int, randfunc:callable, progress_func:callable)

    Generate an RSA key of length 'bits', using 'randfunc' to get
    random data and 'progress_func', if present, to display
    the progress of the key generation.
    """
    obj=RSAobj()

    # Generate the prime factors of n
    if progress_func:
        progress_func('p,q\n')
    p = q = 1L
    while number.size(p*q) < bits:
        p = pubkey.getPrime(bits/2, randfunc)
        q = pubkey.getPrime(bits/2, randfunc)

    # p shall be smaller than q (for calc of u)
    if p > q:
        (p, q)=(q, p)
    obj.p = p
    obj.q = q

    if progress_func:
        progress_func('u\n')
    obj.u = pubkey.inverse(obj.p, obj.q)
    obj.n = obj.p*obj.q

    obj.e = 65537L
    if progress_func:
        progress_func('d\n')
    obj.d=pubkey.inverse(obj.e, (obj.p-1)*(obj.q-1))

    assert bits <= 1+obj.size(), "Generated key is too small"

    return obj
Example #9
0
def generate(bits, randfunc, progress_func=None):
    """generate(bits:int, randfunc:callable, progress_func:callable)

    Generate an RSA key of length 'bits', using 'randfunc' to get
    random data and 'progress_func', if present, to display
    the progress of the key generation.
    """
    obj = RSAobj()

    # Generate the prime factors of n
    if progress_func:
        progress_func('p,q\n')
    p = q = 1L
    while number.size(p * q) < bits:
        p = pubkey.getPrime(bits / 2, randfunc)
        q = pubkey.getPrime(bits / 2, randfunc)

    # p shall be smaller than q (for calc of u)
    if p > q:
        (p, q) = (q, p)
    obj.p = p
    obj.q = q

    if progress_func:
        progress_func('u\n')
    obj.u = pubkey.inverse(obj.p, obj.q)
    obj.n = obj.p * obj.q

    obj.e = 65537L
    if progress_func:
        progress_func('d\n')
    obj.d = pubkey.inverse(obj.e, (obj.p - 1) * (obj.q - 1))

    assert bits <= 1 + obj.size(), "Generated key is too small"

    return obj
def GetBigPrime(bits = 300):
    "获取大素数"
    return bignum(getPrime(bits-1, Random.new().read))