當前位置: 首頁>>代碼示例>>Python>>正文


Python rsa.prime方法代碼示例

本文整理匯總了Python中rsa.prime方法的典型用法代碼示例。如果您正苦於以下問題:Python rsa.prime方法的具體用法?Python rsa.prime怎麽用?Python rsa.prime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在rsa的用法示例。


在下文中一共展示了rsa.prime方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: calculate_keys

# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import prime [as 別名]
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) 
開發者ID:deadblue,項目名稱:baidupan_shell,代碼行數:24,代碼來源:key.py

示例2: calculate_keys_custom_exponent

# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import prime [as 別名]
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 
開發者ID:Deltares,項目名稱:aqua-monitor,代碼行數:28,代碼來源:key.py

示例3: calculate_keys

# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import prime [as 別名]
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) 
開發者ID:Deltares,項目名稱:aqua-monitor,代碼行數:13,代碼來源:key.py

示例4: gen_keys

# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import prime [as 別名]
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 
開發者ID:Deltares,項目名稱:aqua-monitor,代碼行數:28,代碼來源:key.py

示例5: _find_prime

# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import prime [as 別名]
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 
開發者ID:Deltares,項目名稱:aqua-monitor,代碼行數:10,代碼來源:parallel.py

示例6: gen_keys

# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import prime [as 別名]
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) 
開發者ID:deadblue,項目名稱:baidupan_shell,代碼行數:17,代碼來源:key.py

示例7: _find_prime

# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import prime [as 別名]
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 
開發者ID:deadblue,項目名稱:baidupan_shell,代碼行數:13,代碼來源:parallel.py

示例8: getprime

# 需要導入模塊: import rsa [as 別名]
# 或者: from rsa import prime [as 別名]
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 
開發者ID:deadblue,項目名稱:baidupan_shell,代碼行數:33,代碼來源:parallel.py


注:本文中的rsa.prime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。