當前位置: 首頁>>代碼示例>>Python>>正文


Python PublicKey.ElGamal類代碼示例

本文整理匯總了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))
開發者ID:rfree,項目名稱:python-i2cp,代碼行數:10,代碼來源:crypto.py

示例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'])
開發者ID:Gnof,項目名稱:pycryptodome,代碼行數:7,代碼來源:test_ElGamal.py

示例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"])
開發者ID:rashbhat,項目名稱:BankApplication,代碼行數:7,代碼來源:test_ElGamal.py

示例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'])
開發者ID:Gnof,項目名稱:pycryptodome,代碼行數:7,代碼來源:test_ElGamal.py

示例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)
開發者ID:Ando02,項目名稱:wubiuefi,代碼行數:28,代碼來源:crypto.py

示例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)
開發者ID:Ando02,項目名稱:wubiuefi,代碼行數:31,代碼來源:crypto.py

示例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
開發者ID:tlhquynh,項目名稱:python-asn1,代碼行數:28,代碼來源:pkcs.py

示例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'])
開發者ID:amos402,項目名稱:Arianrhod,代碼行數:7,代碼來源:test_ElGamal.py

示例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)
開發者ID:amos402,項目名稱:Arianrhod,代碼行數:7,代碼來源:test_ElGamal.py

示例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)
開發者ID:danigm,項目名稱:avmixnet,代碼行數:25,代碼來源:avcrypt.py

示例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"])
開發者ID:rashbhat,項目名稱:BankApplication,代碼行數:8,代碼來源:test_ElGamal.py

示例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
開發者ID:Shodhanth,項目名稱:SecureSync,代碼行數:8,代碼來源:keygen.py

示例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'])
開發者ID:amos402,項目名稱:Arianrhod,代碼行數:8,代碼來源:test_ElGamal.py

示例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"])
開發者ID:rashbhat,項目名稱:BankApplication,代碼行數:8,代碼來源:test_ElGamal.py

示例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'])
開發者ID:amos402,項目名稱:Arianrhod,代碼行數:8,代碼來源:test_ElGamal.py


注:本文中的Crypto.PublicKey.ElGamal類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。