我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用Crypto.Util.number.inverse()。
def setUp(self): global RSA, Random, bytes_to_long from Crypto.PublicKey import RSA from Crypto import Random from Crypto.Util.number import bytes_to_long, inverse self.n = bytes_to_long(a2b_hex(self.modulus)) self.p = bytes_to_long(a2b_hex(self.prime_factor)) # Compute q, d, and u from n, e, and p self.q = divmod(self.n, self.p)[0] self.d = inverse(self.e, (self.p-1)*(self.q-1)) self.u = inverse(self.p, self.q) # u = e**-1 (mod q) self.rsa = RSA
def _exercise_primitive(self, rsaObj): # Since we're using a randomly-generated key, we can't check the test # vector, but we can make sure encryption and decryption are inverse # operations. ciphertext = a2b_hex(self.ciphertext) # Test decryption plaintext = rsaObj.decrypt((ciphertext,)) # Test encryption (2 arguments) (new_ciphertext2,) = rsaObj.encrypt(plaintext, b("")) self.assertEqual(b2a_hex(ciphertext), b2a_hex(new_ciphertext2)) # Test blinded decryption blinding_factor = Random.new().read(len(ciphertext)-1) blinded_ctext = rsaObj.blind(ciphertext, blinding_factor) blinded_ptext = rsaObj.decrypt((blinded_ctext,)) unblinded_plaintext = rsaObj.unblind(blinded_ptext, blinding_factor) self.assertEqual(b2a_hex(plaintext), b2a_hex(unblinded_plaintext)) # Test signing (2 arguments) signature2 = rsaObj.sign(ciphertext, b("")) self.assertEqual((bytes_to_long(plaintext),), signature2) # Test verification self.assertEqual(1, rsaObj.verify(ciphertext, (bytes_to_long(plaintext),)))
def _unblind(self, m, r): # compute m / r (mod n) return inverse(r, self.n) * m % self.n
def _sign(self, m, k): # alias for _decrypt # SECURITY TODO - We _should_ be computing SHA1(m), but we don't because that's the API. if not self.has_private(): raise TypeError("No private key") if not (1L < k < self.q): raise ValueError("k is not between 2 and q-1") inv_k = inverse(k, self.q) # Compute k**-1 mod q r = pow(self.g, k, self.p) % self.q # r = (g**k mod p) mod q s = (inv_k * (m + self.x * r)) % self.q return (r, s)
def _decrypt(self, M): if (not hasattr(self, 'x')): raise TypeError('Private key not available in this object') ax=pow(M[0], self.x, self.p) plaintext=(M[1] * inverse(ax, self.p ) ) % self.p return plaintext
def _sign(self, M, K): if (not hasattr(self, 'x')): raise TypeError('Private key not available in this object') p1=self.p-1 if (GCD(K, p1)!=1): raise ValueError('Bad K value: GCD(K,p-1)!=1') a=pow(self.g, K, self.p) t=(M-self.x*a) % p1 while t<0: t=t+p1 b=(t*inverse(K, p1)) % p1 return (a, b)
def dict2RSA(**kw): """ Create Crypto.PublicKey.RSA from dict Required RSA priv. key params (as long) n, e - modulus and public exponent (public key only) n, d, e - modulus, private and public exponent or p, q, e - primes p, q, and public exponent e If also dp, dq, qinv present, they are checked to be consistent. Default value for e is 0x10001 Return Crypto.PublicKey.RSA object dp = d mod (p-1), dq = d mod (q-1), q*qinv mod p = 1 """ for par in ('n', 'd', 'p', 'q', 'dp', 'dq', 'qinv'): if par in kw: assert isinstance(long(kw[par]), long), \ "RSA parameter %s must be long" % par e = long(kw.get('e', 0x10001L)) if all([par not in kw for par in ('d', 'p', 'q', 'dp', 'dq', 'qinv')]): assert 'n' in kw, "At least modulus must be in dict" return RSA.construct((kw['n'], e)) if 'n' in kw and 'd' in kw: return RSA.construct((kw['n'], e, kw['d'])) assert 'p' in kw and 'q' in kw, "Either n, d or p, q must be in dict" p = kw['p'] q = kw['q'] n = p*q d = number.inverse(e, (p-1)*(q-1)) if 'd' in kw: assert d == kw['d'], "Inconsinstent private exponent" if 'dp' in kw: assert d % (p-1) == kw['dp'], "Inconsistent d mod (p-1)" if 'dq' in kw: assert d % (q-1) == kw['dq'], "Inconsistent d mod (q-1)" u = number.inverse(q, p) if 'qinv' in kw: assert u == kw['qinv'], "Inconsistent q inv" return RSA.construct((n, e, d, q, p, u))
def chinese_remainder(n, a): ''' Compute a number from its moduluses. From Rosetta Code. ''' sum = 0 prod = reduce(lambda a, b: a*b, n) for n_i, a_i in zip(n, a): p = prod / n_i sum += a_i * number.inverse(p, n_i) * p return sum % prod
def _sign(self, m, k): # alias for _decrypt # SECURITY TODO - We _should_ be computing SHA1(m), but we don't because that's the API. if not self.has_private(): raise TypeError("No private key") if not (1 < k < self.q): raise ValueError("k is not between 2 and q-1") inv_k = inverse(k, self.q) # Compute k**-1 mod q r = pow(self.g, k, self.p) % self.q # r = (g**k mod p) mod q s = (inv_k * (m + self.x * r)) % self.q return (r, s)