本文整理汇总了Python中Crypto.PublicKey.ElGamal.construct方法的典型用法代码示例。如果您正苦于以下问题:Python ElGamal.construct方法的具体用法?Python ElGamal.construct怎么用?Python ElGamal.construct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.PublicKey.ElGamal
的用法示例。
在下文中一共展示了ElGamal.construct方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ElGamalKey
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def ElGamalKey(pub=None, priv=None, fd=None):
"""
make ElGamal KeyPair Object
"""
if fd is not None:
pub = int.from_bytes(fd.read(256), 'big')
priv = int.from_bytes(fd.read(256), 'big')
if priv:
return ElGamal.construct((elgamal_p, elgamal_g, pub, priv))
return ElGamal.construct((elgamal_p, elgamal_g, pub))
示例2: test_decryption
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_decryption(self):
for tv in self.tve:
for as_longs in (0, 1):
d = self.convert_tv(tv, as_longs)
key = ElGamal.construct(d["key"])
pt = key.decrypt((d["ct1"], d["ct2"]))
self.assertEqual(pt, d["pt"])
示例3: reencrypt
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def reencrypt(self, cipher, pubkey=None):
'''
>>> B = 64
>>> k = AVCrypt(bits=B)
>>> clears = [random.StrongRandom().randint(1, B) for i in range(5)]
>>> cipher = [k.encrypt(i) for i in clears]
>>> cipher2 = [k.reencrypt(i) for i in cipher]
>>> d = [k.decrypt(i) for i in cipher]
>>> d2 = [k.decrypt(i) for i in cipher2]
>>> clears == d == d2
True
>>> cipher != cipher2
True
'''
if pubkey:
p, g, y = pubkey
k = ElGamal.construct((p, g, y))
else:
k = self.k
a, b = cipher
a1, b1 = self.encrypt(1, k=k)
return ((a * a1) % k.p, (b * b1) % k.p)
示例4: test_encryption
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_encryption(self):
for tv in self.tve:
d = self.convert_tv(tv, True)
key = ElGamal.construct(d['key'])
ct = key._encrypt(d['pt'], d['k'])
self.assertEquals(ct[0], d['ct1'])
self.assertEquals(ct[1], d['ct2'])
示例5: sign_ElGamal
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def sign_ElGamal(msg, key_tuple, k=None):
"""Create an ElGamal signature.
:Parameters:
- `msg`: string of data signature applies to
- `key_tuple`: tuple ElGamal key integers (p, g, x)
(see `ElGamal key tuple`_)
- `k`: integer (must be relatively prime to p-1)
:Returns: tuple (integer, integer) ElGamal signature values (a, b)
.. _ElGamal key tuple:
ElGamal key tuple:
- `p`: integer ElGamal prime
- `g`: integer ElGamal random "g" value
- `x`: integer ElGamal private key
"""
import Crypto.PublicKey.ElGamal as ELG
if k is None: # generate our own prime k value (k relatively prime to p-1)
import Crypto.Util.number as NUM
import Crypto.Util.randpool as RND
rnd = RND.RandomPool()
q = key_tuple[0] # no restrictions on bit length for k, good enough?
k = NUM.getPrime(8*len(STN.int2str(q)), rnd.get_bytes)
elg = ELG.construct((key_tuple[0], key_tuple[1], 0, key_tuple[2]))
return elg.sign(msg, k)
示例6: test_signing
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_signing(self):
for tv in self.tvs:
d = self.convert_tv(tv, True)
key = ElGamal.construct(d['key'])
sig1, sig2 = key._sign(d['h'], d['k'])
self.assertEquals(sig1, d['sig1'])
self.assertEquals(sig2, d['sig2'])
示例7: verify_ElGamal
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def verify_ElGamal(msg, sig_tuple, key_tuple):
"""Verify an ElGamal signature.
:Parameters:
- `msg`: string of data signature applies to
- `sig_tuple`: tuple of ElGamal signature integers (a, b)
(see `ElGamal signature tuple`_)
- `key_tuple`: tuple of ElGamal key integers (p, g, y)
(see `ElGamal key tuple`_)
:Returns: tuple (integer, None) where integer == 1 or 0, verification
true or false
.. _ElGamal signature tuple:
ElGamal signature tuple:
- `a`: integer ElGamal "a"
- `b`: integer ElGamal "b"
.. _ElGamal key tuple:
ElGamal key tuple:
- `p`: integer ElGamal prime
- `g`: integer ElGamal group
- `y`: integer ElGamal public key
"""
import Crypto.PublicKey.ElGamal as ELG
elg = ELG.construct(key_tuple) # note change in ordering
return elg.verify(msg, sig_tuple)
示例8: load_unencrypted
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def load_unencrypted(pkeyInfo):
version, pkeyAlgo, pkeyData = pkeyInfo[0:3]
if version != 0:
raise PKCSError('unknown PKCS#8 version: {}'.format(version))
algoId, algoParms = pkeyAlgo.get_pos(0,2)
if algoId == OID_PKCS_RSAPKEY:
from Crypto.PublicKey import RSA
rsakey = asn1.loads(pkeyData)
vals = rsakey[1:6] # n, e, d, p, q
pkey = RSA.construct([long(val) for val in vals])
print 'RSA!', vals
elif algoId == OID_PKCS_DSAPKEY:
from Crypto.PublicKey import DSA
p, q, g = algoParms
x = asn1.loads(pkeyData)
y = pow(g, x, p)
vals = (y, g, p, q, x)
pkey = DSA.construct([long(val) for val in vals])
elif algoId == OID_PKCS_DHPKEY:
from Crypto.PublicKey import ElGamal
p, g = algoParms[0:2]
x = asn1.loads(pkeyData)
y = pow(g, x, p)
vals = (p, g, y, x)
pkey = ElGamal.construct([long(val) for val in vals])
else:
raise PKCSError('unknown PKCS#8 key algorithm: {}'.format(algoId))
return pkey
示例9: test_decryption
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_decryption(self):
for tv in self.tve:
for as_longs in (0,1):
d = self.convert_tv(tv, as_longs)
key = ElGamal.construct(d['key'])
pt = key.decrypt((d['ct1'], d['ct2']))
self.assertEquals(pt, d['pt'])
示例10: test_encryption
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_encryption(self):
for tv in self.tve:
for as_longs in (0, 1):
d = self.convert_tv(tv, as_longs)
key = ElGamal.construct(d["key"])
ct = key.encrypt(d["pt"], d["k"])
self.assertEqual(ct[0], d["ct1"])
self.assertEqual(ct[1], d["ct2"])
示例11: test_signing
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_signing(self):
for tv in self.tvs:
for as_longs in (0,1):
d = self.convert_tv(tv, as_longs)
key = ElGamal.construct(d['key'])
sig1, sig2 = key.sign(d['h'], d['k'])
self.assertEquals(sig1, d['sig1'])
self.assertEquals(sig2, d['sig2'])
示例12: test_signing
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_signing(self):
for tv in self.tvs:
for as_longs in (0, 1):
d = self.convert_tv(tv, as_longs)
key = ElGamal.construct(d["key"])
sig1, sig2 = key.sign(d["h"], d["k"])
self.assertEqual(sig1, d["sig1"])
self.assertEqual(sig2, d["sig2"])
示例13: test_encryption
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_encryption(self):
for tv in self.tve:
for as_longs in (0,1):
d = self.convert_tv(tv, as_longs)
key = ElGamal.construct(d['key'])
ct = key.encrypt(d['pt'], d['k'])
self.assertEquals(ct[0], d['ct1'])
self.assertEquals(ct[1], d['ct2'])
示例14: test_verification
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def test_verification(self):
for tv in self.tvs:
d = self.convert_tv(tv, True)
key = ElGamal.construct(d['key'])
# Positive test
res = key._verify( d['h'], (d['sig1'],d['sig2']) )
self.failUnless(res)
# Negative test
res = key._verify( d['h'], (d['sig1']+1,d['sig2']) )
self.failIf(res)
示例15: decrypt_public
# 需要导入模块: from Crypto.PublicKey import ElGamal [as 别名]
# 或者: from Crypto.PublicKey.ElGamal import construct [as 别名]
def decrypt_public(algorithm, key_tuple, cipher_tuple):
"""Decrypt public key encrypted data.
:Parameters:
- `algorithm`: integer public key algorithm constant
- `key_tuple`: tuple containing a public and private integers of the
target key, RSA values (n, d) or ElGamal values (p, x)
- `cipher_tuple`: tuple containing the integers of the encrypted data,
coerced RSA value (c, ) and ElGamal values (a, b)
:Returns: string cleartext
`decrypt_public()` works with public key encrypted information (information
encrypted to public key values and decrypted using the corresponding secret
key values). This function works with tuples of public key values and
tuples of values that comprise the "ciphertext."
**Use this function to decrypt public key encrypted session key packets.**
RSA key tuple (n, d):
- `n`: integer RSA product of primes p & q
- `d`: integer RSA decryption key
RSA cipher tuple (c, ):
- `c`: integer m**e mod n
ElGamal key tuple (p, x):
- `p`: integer ElGamal prime
- `x`: integer ElGamal private key
ElGamal cipher tuple (a, b):
- `a`: integer ElGamal value g**k mod p
- `b`: integer ElGamal value m * y**k mod p
Use this for decrypting public-key encrypted session keys.
"""
key_tuple = tuple([long(i) for i in key_tuple]) # long(): fastmath dep
if algorithm in [ASYM_RSA_EOS, ASYM_RSA_E]:
from Crypto.PublicKey import RSA
key = RSA.construct((key_tuple[0], 0L, key_tuple[1])) # L for fastmath
a = STN.int2str(cipher_tuple[0])
return key.decrypt((a,))
elif algorithm in [ASYM_ELGAMAL_EOS, ASYM_ELGAMAL_E]:
from Crypto.PublicKey import ElGamal
key = ElGamal.construct((key_tuple[0], 0, 0, key_tuple[1]))
a = STN.int2str(cipher_tuple[0])
b = STN.int2str(cipher_tuple[1])
return key.decrypt((a, b))
else:
raise NotImplementedError, "Unsupported asymmetric algorithm:%s" % algorithm