本文整理汇总了Python中Crypto.Util.number.inverse方法的典型用法代码示例。如果您正苦于以下问题:Python number.inverse方法的具体用法?Python number.inverse怎么用?Python number.inverse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Util.number
的用法示例。
在下文中一共展示了number.inverse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: export_privatekeyblob
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def export_privatekeyblob(self):
key = self.key.key
n = key.n
e = key.e
d = key.d
p = key.p
q = key.q
n_bytes = long_to_bytes(n)[::-1]
key_len = len(n_bytes) * 8
result = PUBLICKEYSTRUC_s.pack(bType_PRIVATEKEYBLOB, CUR_BLOB_VERSION, CALG_RSA_KEYX)
result += RSAPUBKEY_s.pack(PRIVATEKEYBLOB_MAGIC, key_len, e)
result += n_bytes
result += long_to_bytes(p, key_len / 16)[::-1]
result += long_to_bytes(q, key_len / 16)[::-1]
result += long_to_bytes(d % (p - 1), key_len / 16)[::-1]
result += long_to_bytes(d % (q - 1), key_len / 16)[::-1]
result += long_to_bytes(inverse(q, p), key_len / 16)[::-1]
result += long_to_bytes(d, key_len / 8)[::-1]
return result
示例2: _decrypt
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
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
示例3: _sign
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
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)
示例4: _unblind
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def _unblind(self, m, r):
# compute m / r (mod n)
return inverse(r, self.n) * m % self.n
示例5: setUp
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
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
示例6: _exercise_primitive
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
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),)))
示例7: rsa_unblind
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def rsa_unblind(message, randint, modulus):
"""
Return message RSA-unblinded with integer randint for a keypair
with the provided modulus.
"""
return number.inverse(randint, modulus) * message % modulus
示例8: derive_d_from_pqe
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def derive_d_from_pqe(p,q,e):
'''
Given p, q, and e from factored RSA modulus, derive the private component d
p - The first of the two factors of the modulus
q - The second of the two factors of the modulus
e - The public exponent
'''
return long(number.inverse(e,(p-1)*(q-1)))
示例9: lcg_prev_states
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def lcg_prev_states(states, num_states=5, a=None, c=None, m=None):
'''
Given the current state of an LCG, return the previous states
in sequence.
Currently, the modulus must be known. Other parameters can be
recovered given enough sequential states.
states - (list of ints) Known sequential states from the LCG.
num_states - (int) The number of past states to generate.
a - (int) The multiplier for the LCG.
c - (int) The addend for the LCG.
m - (int) The modulus for the LCG.
'''
if not all([a,c,m]):
parameters = lcg_recover_parameters(states,a,c,m)
if parameters == False:
return False
else:
(a,c,m) = parameters
current_state = states[0]
prev_states = []
for i in range(num_states):
current_state = (((current_state - c) % m) * number.inverse(a, m)) % m
prev_states.insert(0,current_state)
return prev_states
示例10: dsa_repeated_nonce_attack
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def dsa_repeated_nonce_attack(r,msg1,s1,msg2,s2,n,verbose=False):
'''
Recover k (nonce) and Da (private signing key) from two DSA or ECDSA signed messages
with identical k values
Takes the following arguments:
string: r (r value of signatures)
string: msg1 (first message)
string: s1 (s value of first signature)
string: msg2 (second message)
string: s2 (s value of second signature)
long: n (curve order for ECDSA or modulus (q parameter) for DSA)
adapted from code by Antonio Bianchi (antoniob@cs.ucsb.edu)
<http://antonio-bc.blogspot.com/2013/12/mathconsole-ictf-2013-writeup.html>
'''
r = string_to_long(r)
s1 = string_to_long(s1)
s2 = string_to_long(s2)
# convert messages to sha1 hash as number
z1 = string_to_long(SHA.new(msg1).digest())
z2 = string_to_long(SHA.new(msg2).digest())
sdiff_inv = number.inverse(((s1-s2)%n),n)
k = ( ((z1-z2)%n) * sdiff_inv) % n
r_inv = number.inverse(r,n)
da = (((((s1*k) %n) -z1) %n) * r_inv) % n
if verbose:
print "Recovered k:" + hex(k)
print "Recovered Da: " + hex(da)
return (k, da)
示例11: _decrypt
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def _decrypt(ciphertext, privkey):
"""
Decrypt ciphertext using ElGamal Encryption System
:Parameters:
ciphertext : int/long tuple
Ciphertext of ElGamal encrypted plaintext
privkey : instance of `PrivateKey` class
Receiver's private key used for decryption
:Variables:
g : int/long
Base point for modular exponentiation.
p : int/long
Modulus for modular exponentiation. Should be a safe prime.
q : int/long
Order of group generated by p and equals p-1
c1, c2: int/long
Ciphertext pair
x : int/long
Receiver's private key, should be kept secret.
s : int/long
Shared secret
"""
c1, c2 = ciphertext
g = privkey.g
p = privkey.p
x = privkey.x
s = pow(c1, x, p)
m = (c2*inverse(s, p)) % p
return m
示例12: crack
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def crack(n, e, encrypted_data):
print('n: %s' % n)
print('e: %s' % e)
success, p, q = prime_factor(n)
# success, p, q = True, 323067951880962860113901833788589140869, 263074551335953569706833061753492041353
if success:
d = inverse(e, (p - 1) * (q - 1))
print('d: %s' % d)
rsa = RSA.construct((n, e, d))
plain = rsa.decrypt(encrypted_data)
print(plain)
示例13: curve25519
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def curve25519(secret, basepoint):
a = ord(secret[0])
a &= 248
b = ord(secret[31])
b &= 127
b |= 64
s = chr(a) + secret[1:-1] + chr(b)
s = number.bytes_to_long(s[::-1])
basepoint = number.bytes_to_long(basepoint[::-1])
x, z = curve25519_mult(s, basepoint)
zmone = number.inverse(z, CURVE_P)
z = x * zmone % CURVE_P
return number.long_to_bytes(z)[::-1]
示例14: setUp
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
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 = self.n // self.p
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
示例15: test_construct_bad_key6
# 需要导入模块: from Crypto.Util import number [as 别名]
# 或者: from Crypto.Util.number import inverse [as 别名]
def test_construct_bad_key6(self):
tup = (self.n, self.e, self.d, self.p, self.q, 10)
self.assertRaises(ValueError, self.rsa.construct, tup)
from Crypto.Util.number import inverse
tup = (self.n, self.e, self.d, self.p, self.q, inverse(self.q, self.p))
self.assertRaises(ValueError, self.rsa.construct, tup)