Python rsa.prime() Examples

The following are 30 code examples of rsa.prime(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rsa , or try the search function .
Example #1
Source File: key.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def calculate_keys(p, q, nbits):
    '''Calculates an encryption and a decryption key given p and q, and
    returns them as a tuple (e, d)

    '''

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

    # A very common choice for e is 65537
    e = 65537

    try:
        d = rsa.common.inverse(e, phi_n)
    except ValueError:
        raise ValueError("e (%d) and phi_n (%d) are not relatively prime" %
                (e, phi_n))

    if (e * d) % phi_n != 1:
        raise ValueError("e (%d) and d (%d) are not mult. inv. modulo "
                "phi_n (%d)" % (e, d, phi_n))

    return (e, d) 
Example #2
Source File: key.py    From baidupan_shell with GNU General Public License v2.0 6 votes vote down vote up
def calculate_keys(p, q, nbits):
    '''Calculates an encryption and a decryption key given p and q, and
    returns them as a tuple (e, d)

    '''

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

    # A very common choice for e is 65537
    e = 65537

    try:
        d = rsa.common.inverse(e, phi_n)
    except ValueError:
        raise ValueError("e (%d) and phi_n (%d) are not relatively prime" %
                (e, phi_n))

    if (e * d) % phi_n != 1:
        raise ValueError("e (%d) and d (%d) are not mult. inv. modulo "
                "phi_n (%d)" % (e, d, phi_n))

    return (e, d) 
Example #3
Source File: key.py    From opsbro with MIT License 6 votes vote down vote up
def calculate_keys(p, q, nbits):
    '''Calculates an encryption and a decryption key given p and q, and
    returns them as a tuple (e, d)

    '''

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

    # A very common choice for e is 65537
    e = 65537

    try:
        d = rsa.common.inverse(e, phi_n)
    except ValueError:
        raise ValueError("e (%d) and phi_n (%d) are not relatively prime" %
                (e, phi_n))

    if (e * d) % phi_n != 1:
        raise ValueError("e (%d) and d (%d) are not mult. inv. modulo "
                "phi_n (%d)" % (e, d, phi_n))

    return (e, d) 
Example #4
Source File: key.py    From plugin.video.bdyun with GNU General Public License v3.0 5 votes vote down vote up
def gen_keys(nbits, getprime_func, accurate=True, exponent=DEFAULT_EXPONENT):
    """Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.

    :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
        ``q`` will use ``nbits/2`` bits.
    :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
        with similar signature.
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int
    """

    # Regenerate p and q values, until calculate_keys doesn't raise a
    # ValueError.
    while True:
        (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
        try:
            (e, d) = calculate_keys_custom_exponent(p, q, exponent=exponent)
            break
        except ValueError:
            pass

    return p, q, e, d 
Example #5
Source File: parallel.py    From xbmc-addons-chinese with GNU General Public License v2.0 5 votes vote down vote up
def _find_prime(nbits, pipe):
    while True:
        integer = rsa.randnum.read_random_odd_int(nbits)

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return 
Example #6
Source File: key.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def gen_keys(nbits, getprime_func, accurate=True):
    '''Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.
    
    :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
        ``q`` will use ``nbits/2`` bits.
    :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
        with similar signature.
    '''

    (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
    (e, d) = calculate_keys(p, q, nbits // 2)

    return (p, q, e, d) 
Example #7
Source File: parallel.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def _find_prime(nbits, pipe):
    while True:
        integer = rsa.randnum.read_random_int(nbits)

        # Make sure it's odd
        integer |= 1

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return 
Example #8
Source File: parallel.py    From luci-py with Apache License 2.0 5 votes vote down vote up
def getprime(nbits, poolsize):
    '''Returns a prime number that can be stored in 'nbits' bits.

    Works in multiple threads at the same time.

    >>> p = getprime(128, 3)
    >>> rsa.prime.is_prime(p-1)
    False
    >>> rsa.prime.is_prime(p)
    True
    >>> rsa.prime.is_prime(p+1)
    False
    
    >>> from rsa import common
    >>> common.bit_size(p) == 128
    True
    
    '''

    (pipe_recv, pipe_send) = mp.Pipe(duplex=False)

    # Create processes
    procs = [mp.Process(target=_find_prime, args=(nbits, pipe_send))
             for _ in range(poolsize)]
    [p.start() for p in procs]

    result = pipe_recv.recv()

    [p.terminate() for p in procs]

    return result 
Example #9
Source File: key.py    From opsbro with MIT License 5 votes vote down vote up
def gen_keys(nbits, getprime_func, accurate=True):
    '''Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.
    
    :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
        ``q`` will use ``nbits/2`` bits.
    :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
        with similar signature.
    '''

    (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
    (e, d) = calculate_keys(p, q, nbits // 2)

    return (p, q, e, d) 
Example #10
Source File: parallel.py    From opsbro with MIT License 5 votes vote down vote up
def _find_prime(nbits, pipe):
    while True:
        integer = rsa.randnum.read_random_int(nbits)

        # Make sure it's odd
        integer |= 1

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return 
Example #11
Source File: parallel.py    From opsbro with MIT License 5 votes vote down vote up
def getprime(nbits, poolsize):
    '''Returns a prime number that can be stored in 'nbits' bits.

    Works in multiple threads at the same time.

    >>> p = getprime(128, 3)
    >>> rsa.prime.is_prime(p-1)
    False
    >>> rsa.prime.is_prime(p)
    True
    >>> rsa.prime.is_prime(p+1)
    False
    
    >>> from rsa import common
    >>> common.bit_size(p) == 128
    True
    
    '''

    (pipe_recv, pipe_send) = mp.Pipe(duplex=False)

    # Create processes
    procs = [mp.Process(target=_find_prime, args=(nbits, pipe_send))
             for _ in range(poolsize)]
    [p.start() for p in procs]

    result = pipe_recv.recv()

    [p.terminate() for p in procs]

    return result 
Example #12
Source File: key.py    From plugin.video.bdyun with GNU General Public License v3.0 5 votes vote down vote up
def calculate_keys_custom_exponent(p, q, exponent):
    """Calculates an encryption and a decryption key given p, q and an exponent,
    and returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int

    """

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

    try:
        d = rsa.common.inverse(exponent, phi_n)
    except ValueError:
        raise ValueError("e (%d) and phi_n (%d) are not relatively prime" %
                         (exponent, phi_n))

    if (exponent * d) % phi_n != 1:
        raise ValueError("e (%d) and d (%d) are not mult. inv. modulo "
                         "phi_n (%d)" % (exponent, d, phi_n))

    return exponent, d 
Example #13
Source File: key.py    From plugin.video.bdyun with GNU General Public License v3.0 5 votes vote down vote up
def calculate_keys(p, q):
    """Calculates an encryption and a decryption key given p and q, and
    returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime

    :return: tuple (e, d) with the encryption and decryption exponents.
    """

    return calculate_keys_custom_exponent(p, q, DEFAULT_EXPONENT) 
Example #14
Source File: parallel.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def _find_prime(nbits, pipe):
    while True:
        integer = rsa.randnum.read_random_odd_int(nbits)

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return 
Example #15
Source File: parallel.py    From plugin.video.bdyun with GNU General Public License v3.0 5 votes vote down vote up
def _find_prime(nbits, pipe):
    while True:
        integer = rsa.randnum.read_random_odd_int(nbits)

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return 
Example #16
Source File: key.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def calculate_keys_custom_exponent(p, q, exponent):
    """Calculates an encryption and a decryption key given p, q and an exponent,
    and returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int

    """

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

    try:
        d = rsa.common.inverse(exponent, phi_n)
    except ValueError:
        raise ValueError("e (%d) and phi_n (%d) are not relatively prime" %
                         (exponent, phi_n))

    if (exponent * d) % phi_n != 1:
        raise ValueError("e (%d) and d (%d) are not mult. inv. modulo "
                         "phi_n (%d)" % (exponent, d, phi_n))

    return exponent, d 
Example #17
Source File: key.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def calculate_keys(p, q):
    """Calculates an encryption and a decryption key given p and q, and
    returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime

    :return: tuple (e, d) with the encryption and decryption exponents.
    """

    return calculate_keys_custom_exponent(p, q, DEFAULT_EXPONENT) 
Example #18
Source File: key.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def gen_keys(nbits, getprime_func, accurate=True, exponent=DEFAULT_EXPONENT):
    """Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.

    :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
        ``q`` will use ``nbits/2`` bits.
    :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
        with similar signature.
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int
    """

    # Regenerate p and q values, until calculate_keys doesn't raise a
    # ValueError.
    while True:
        (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
        try:
            (e, d) = calculate_keys_custom_exponent(p, q, exponent=exponent)
            break
        except ValueError:
            pass

    return p, q, e, d 
Example #19
Source File: parallel.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def _find_prime(nbits, pipe):
    while True:
        integer = rsa.randnum.read_random_odd_int(nbits)

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return 
Example #20
Source File: key.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def calculate_keys_custom_exponent(p, q, exponent):
    """Calculates an encryption and a decryption key given p, q and an exponent,
    and returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int

    """

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

    try:
        d = rsa.common.inverse(exponent, phi_n)
    except ValueError:
        raise ValueError("e (%d) and phi_n (%d) are not relatively prime" %
                         (exponent, phi_n))

    if (exponent * d) % phi_n != 1:
        raise ValueError("e (%d) and d (%d) are not mult. inv. modulo "
                         "phi_n (%d)" % (exponent, d, phi_n))

    return exponent, d 
Example #21
Source File: key.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def calculate_keys(p, q):
    """Calculates an encryption and a decryption key given p and q, and
    returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime

    :return: tuple (e, d) with the encryption and decryption exponents.
    """

    return calculate_keys_custom_exponent(p, q, DEFAULT_EXPONENT) 
Example #22
Source File: key.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
def gen_keys(nbits, getprime_func, accurate=True, exponent=DEFAULT_EXPONENT):
    """Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.

    :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
        ``q`` will use ``nbits/2`` bits.
    :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
        with similar signature.
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int
    """

    # Regenerate p and q values, until calculate_keys doesn't raise a
    # ValueError.
    while True:
        (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
        try:
            (e, d) = calculate_keys_custom_exponent(p, q, exponent=exponent)
            break
        except ValueError:
            pass

    return p, q, e, d 
Example #23
Source File: key.py    From aqua-monitor with GNU Lesser General Public License v3.0 5 votes vote down vote up
def calculate_keys_custom_exponent(p, q, exponent):
    """Calculates an encryption and a decryption key given p, q and an exponent,
    and returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int

    """

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

    try:
        d = rsa.common.inverse(exponent, phi_n)
    except ValueError:
        raise ValueError("e (%d) and phi_n (%d) are not relatively prime" %
                         (exponent, phi_n))

    if (exponent * d) % phi_n != 1:
        raise ValueError("e (%d) and d (%d) are not mult. inv. modulo "
                         "phi_n (%d)" % (exponent, d, phi_n))

    return exponent, d 
Example #24
Source File: key.py    From bash-lambda-layer with MIT License 5 votes vote down vote up
def gen_keys(nbits, getprime_func, accurate=True, exponent=DEFAULT_EXPONENT):
    """Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.

    :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
        ``q`` will use ``nbits/2`` bits.
    :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
        with similar signature.
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int
    """

    # Regenerate p and q values, until calculate_keys doesn't raise a
    # ValueError.
    while True:
        (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
        try:
            (e, d) = calculate_keys_custom_exponent(p, q, exponent=exponent)
            break
        except ValueError:
            pass

    return p, q, e, d 
Example #25
Source File: key.py    From aqua-monitor with GNU Lesser General Public License v3.0 5 votes vote down vote up
def calculate_keys(p, q):
    """Calculates an encryption and a decryption key given p and q, and
    returns them as a tuple (e, d)

    :param p: the first large prime
    :param q: the second large prime

    :return: tuple (e, d) with the encryption and decryption exponents.
    """

    return calculate_keys_custom_exponent(p, q, DEFAULT_EXPONENT) 
Example #26
Source File: key.py    From aqua-monitor with GNU Lesser General Public License v3.0 5 votes vote down vote up
def gen_keys(nbits, getprime_func, accurate=True, exponent=DEFAULT_EXPONENT):
    """Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.

    :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
        ``q`` will use ``nbits/2`` bits.
    :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
        with similar signature.
    :param exponent: the exponent for the key; only change this if you know
        what you're doing, as the exponent influences how difficult your
        private key can be cracked. A very common choice for e is 65537.
    :type exponent: int
    """

    # Regenerate p and q values, until calculate_keys doesn't raise a
    # ValueError.
    while True:
        (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
        try:
            (e, d) = calculate_keys_custom_exponent(p, q, exponent=exponent)
            break
        except ValueError:
            pass

    return p, q, e, d 
Example #27
Source File: parallel.py    From aqua-monitor with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _find_prime(nbits, pipe):
    while True:
        integer = rsa.randnum.read_random_odd_int(nbits)

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return 
Example #28
Source File: key.py    From baidupan_shell with GNU General Public License v2.0 5 votes vote down vote up
def gen_keys(nbits, getprime_func, accurate=True):
    '''Generate RSA keys of nbits bits. Returns (p, q, e, d).

    Note: this can take a long time, depending on the key size.
    
    :param nbits: the total number of bits in ``p`` and ``q``. Both ``p`` and
        ``q`` will use ``nbits/2`` bits.
    :param getprime_func: either :py:func:`rsa.prime.getprime` or a function
        with similar signature.
    '''

    (p, q) = find_p_q(nbits // 2, getprime_func, accurate)
    (e, d) = calculate_keys(p, q, nbits // 2)

    return (p, q, e, d) 
Example #29
Source File: parallel.py    From baidupan_shell with GNU General Public License v2.0 5 votes vote down vote up
def _find_prime(nbits, pipe):
    while True:
        integer = rsa.randnum.read_random_int(nbits)

        # Make sure it's odd
        integer |= 1

        # Test for primeness
        if rsa.prime.is_prime(integer):
            pipe.send(integer)
            return 
Example #30
Source File: parallel.py    From baidupan_shell with GNU General Public License v2.0 5 votes vote down vote up
def getprime(nbits, poolsize):
    '''Returns a prime number that can be stored in 'nbits' bits.

    Works in multiple threads at the same time.

    >>> p = getprime(128, 3)
    >>> rsa.prime.is_prime(p-1)
    False
    >>> rsa.prime.is_prime(p)
    True
    >>> rsa.prime.is_prime(p+1)
    False
    
    >>> from rsa import common
    >>> common.bit_size(p) == 128
    True
    
    '''

    (pipe_recv, pipe_send) = mp.Pipe(duplex=False)

    # Create processes
    procs = [mp.Process(target=_find_prime, args=(nbits, pipe_send))
             for _ in range(poolsize)]
    [p.start() for p in procs]

    result = pipe_recv.recv()

    [p.terminate() for p in procs]

    return result