当前位置: 首页>>代码示例>>Python>>正文


Python gmpy2.invert方法代码示例

本文整理汇总了Python中gmpy2.invert方法的典型用法代码示例。如果您正苦于以下问题:Python gmpy2.invert方法的具体用法?Python gmpy2.invert怎么用?Python gmpy2.invert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gmpy2的用法示例。


在下文中一共展示了gmpy2.invert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: invert

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def invert(a, b):
    """
    The multiplicitive inverse of a in the integers modulo b.

    :return int: x, where a * x == 1 mod b
    """
    if HAVE_GMP:
        s = int(gmpy2.invert(a, b))
        # according to documentation, gmpy2.invert might return 0 on
        # non-invertible element, although it seems to actually raise an
        # exception; for consistency, we always raise the exception
        if s == 0:
            raise ZeroDivisionError('invert() no inverse exists')
        return s
    else:
        r, s, _ = extended_euclidean_algorithm(a, b)
        if r != 1:
            raise ZeroDivisionError('invert() no inverse exists')
        return s % b 
开发者ID:data61,项目名称:python-paillier,代码行数:21,代码来源:util.py

示例2: build_table

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def build_table(h, g, p, B):
    table, z = {}, h
    g_inverse = invert(g, p)
    table[h] = 0
    for x1 in range(1, B):
        z = t_mod(mul(z, g_inverse), p)
        table[z] = x1
    return table 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:10,代码来源:mitm.py

示例3: compute_d

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def compute_d(e, N, p, q):
    # d * e mod phi(N) = 1
    # where phi(N) = N - p - q + 1
    phiN = phi(N, p, q)
    d = invert(mpz(e), mpz(phiN))
    x = mul(mpz(d), mpz(e))
    assert t_mod(x, mpz(phiN)) == 1
    return d.digits() 
开发者ID:mithi,项目名称:simple-cryptography,代码行数:10,代码来源:publickeysystem.py

示例4: sign

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def sign(self, m, k):
		h = int(hashlib.md5(m).hexdigest(), 16)
		r = pow(self.g, k, self.p) % self.q
		s = int(((self.x * r + h) * gmpy2.invert(k, self.q)) % self.q)
		return (r, s) 
开发者ID:ashutosh1206,项目名称:Crypton,代码行数:7,代码来源:encrypt.py

示例5: verify

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def verify(self, m, r, s):
		if 0 < r and r < self.q and 0 < s and s < self.q:
			h = int(hashlib.md5(m).hexdigest(), 16)
			w = gmpy2.invert(s, self.q)
			u1 = (h * w) % self.q
			u2 = (r * w) % self.q
			v = ((pow(self.g, u1, self.p) * pow(self.y, u2, self.p)) % self.p) % self.q
			return v == r
		return None 
开发者ID:ashutosh1206,项目名称:Crypton,代码行数:11,代码来源:encrypt.py

示例6: crt

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def crt(list_a, list_m):
    """
    Reference: https://crypto.stanford.edu/pbc/notes/numbertheory/crt.html
    Returns the output after computing Chinese Remainder Theorem on

    x = a_1 mod m_1
    x = a_2 mod m_2
    ...
    x = a_n mod m_n

    input parameter list_a = [a_1, a_2, ..., a_n]
    input parameter list_m = [m_1, m_2, ..., m_n]

    Returns -1 if the operation is unsuccessful due to some exceptions
    """
    try:
        assert len(list_a) == len(list_m)
    except:
        print "[+] Length of list_a should be equal to length of list_m"
        return -1
    for i in range(len(list_m)):
        for j in range(len(list_m)):
            if GCD(list_m[i], list_m[j])!= 1 and i!=j:
                print "[+] Moduli should be pairwise co-prime"
                return -1
    M = 1
    for i in list_m:
        M *= i
    list_b = [M/i for i in list_m]
    assert len(list_b) == len(list_m)
    try:
        list_b_inv = [int(gmpy2.invert(list_b[i], list_m[i])) for i in range(len(list_m))]
    except:
        print "[+] Encountered an unusual error while calculating inverse using gmpy2.invert()"
        return -1
    x = 0
    for i in range(len(list_m)):
        x += list_a[i]*list_b[i]*list_b_inv[i]
    return x % M 
开发者ID:ashutosh1206,项目名称:Crypton,代码行数:41,代码来源:hastad_unpadded.py

示例7: neg_pow

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def neg_pow(a, b, n):
	assert b < 0
	assert GCD(a, n) == 1
	res = int(gmpy2.invert(a, n))
	res = pow(res, b*(-1), n)
	return res

# e1 --> Public Key exponent used to encrypt message m and get ciphertext c1
# e2 --> Public Key exponent used to encrypt message m and get ciphertext c2
# n --> Modulus
# The following attack works only when m^{GCD(e1, e2)} < n 
开发者ID:ashutosh1206,项目名称:Crypton,代码行数:13,代码来源:exploit.py

示例8: invert

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def invert(a, b):
    """return int: x, where a * x == 1 mod b
    """    
    x = int(gmpy2.invert(a, b))
   
    if x == 0:
        raise ZeroDivisionError('invert(a, b) no inverse exists')
    
    return x 
开发者ID:FederatedAI,项目名称:FATE,代码行数:11,代码来源:gmpy_math.py

示例9: EEA

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def EEA(n1, n2):
    print('x*y == 1 (mod p)')
    print('y = invmod(x, p)')
    print('\n\033[1;34m[*]\033[0m Calculating...\n')
    n3 = gmpy2.invert(n1, n2)
    n3 = str(n3)
    n3 = n3.replace('mpz(', '')
    n3 = n3.replace(')', '')
    print('\033[1;34m[*]\033[0m ' + str(n1) + ' * ' + str(n2) + ' == 1 (mod ' + str(n2) + ')')
    print('\033[1;32m[+]\033[0m invmod(' + str(n1) + ', ' + str(n2) + ') = ' + str(n3)) 
开发者ID:lockedbyte,项目名称:cryptovenom,代码行数:12,代码来源:main.py

示例10: inverse

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def inverse(n1,n2):
    try:
        n3 = gmpy2.invert(n1,n2)
    except ZeroDivisionError:
        n3 = 'ERR'
    return n3 
开发者ID:lockedbyte,项目名称:cryptovenom,代码行数:8,代码来源:main.py

示例11: embedPriv

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def embedPriv(p, q, e):
    p = bytes_to_long(long_to_bytes(p))
    q = bytes_to_long(long_to_bytes(q))
    e = bytes_to_long(long_to_bytes(e))
    n = p * q
    n = bytes_to_long(long_to_bytes(n))
    phi = (p - 1) * (q - 1)
    d = gmpy2.invert(e, phi)
    d = bytes_to_long(long_to_bytes(d))
    key = RSA.construct((n,e,d,p,q))
    key = RSA._RSAobj.exportKey(key).decode('utf-8')
    return key 
开发者ID:lockedbyte,项目名称:cryptovenom,代码行数:14,代码来源:main.py

示例12: RSAsolverpqec

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def RSAsolverpqec(p, q, e, c):

    print('\033[1;34m[*]\033[0m Calculating Plain-Text')
    n = int(p) * int(q)
    
    phi = (int(p) - 1) * (int(q) - 1)
    
    d = gmpy2.invert(int(e), phi)
    
    m = pow(int(c),int(d),int(n))
    
    return m 
开发者ID:lockedbyte,项目名称:cryptovenom,代码行数:14,代码来源:main.py

示例13: fermatAttack

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def fermatAttack(n, e):
    try:
        (p, q) = Fermat.fermat(n, e)
    except:
        print("\n\033[1;31m[-]\033[0m This RSA key is not valid for a Fermat Attack\n")
        exit()
        phi = indicatrice_euler(p,q)
        d = gmpy2.invert(e, phi)       
        return d 
开发者ID:lockedbyte,项目名称:cryptovenom,代码行数:11,代码来源:main.py

示例14: invert

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def invert(x, m):
        """Return y such that x*y == 1 (mod m), assuming m is prime.

        Raises ZeroDivisionError if no inverse exists.
        """
        if m == 2:
            y = x%2
        else:
            y = pow(x, m-2, m)
        if y == 0:
            raise ZeroDivisionError

        return y 
开发者ID:lschoe,项目名称:mpyc,代码行数:15,代码来源:gmpy.py

示例15: neg_pow

# 需要导入模块: import gmpy2 [as 别名]
# 或者: from gmpy2 import invert [as 别名]
def neg_pow(a, b, n):
        assert b < 0
        assert GCD(a, n) == 1
        res = int(gmpy2.invert(a, n))
        res = pow(res, b*(-1), n)
        return res 
开发者ID:X-Vector,项目名称:X-RSA,代码行数:8,代码来源:RSA_common_modulus.py


注:本文中的gmpy2.invert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。