本文整理汇总了Python中Crypto.PublicKey.ElGamal类的典型用法代码示例。如果您正苦于以下问题:Python ElGamal类的具体用法?Python ElGamal怎么用?Python ElGamal使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ElGamal类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ElGamalKey
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_encryption
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'])
示例3: test_decryption
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"])
示例4: test_signing
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'])
示例5: sign_ElGamal
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: verify_ElGamal
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)
示例7: load_unencrypted
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
示例8: test_decryption
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'])
示例9: _test_random_key
def _test_random_key(self, bits):
elgObj = ElGamal.generate(bits, Random.new().read)
self._check_private_key(elgObj)
self._exercise_primitive(elgObj)
pub = elgObj.publickey()
self._check_public_key(pub)
self._exercise_public_primitive(elgObj)
示例10: reencrypt
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)
示例11: test_encryption
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"])
示例12: get_key
def get_key():
key = ElGamal.generate(256, get_random_bytes)
comps = ('p','g','y','x')
out = '\n'.join(["{} = {}".format(comp,getattr(key, comp)) for comp in comps])
with open('./key', 'w+') as k:
k.write(out)
key.set_key(user, out['y'],out['x'])
return key
示例13: test_encryption
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_signing
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"])
示例15: test_signing
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'])