Server IP : 192.158.238.246 / Your IP : 18.222.207.132 Web Server : LiteSpeed System : Linux uniform.iwebfusion.net 4.18.0-553.27.1.lve.1.el8.x86_64 #1 SMP Wed Nov 20 15:58:00 UTC 2024 x86_64 User : jenniferflocom ( 1321) PHP Version : 8.1.32 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /opt/alt/python37/lib64/python3.7/site-packages/Crypto/PublicKey/__pycache__/ |
Upload File : |
B Bd\R�3 � @ sb d Z dZddddgZddlT ddlmZ G d d� de�Zddd�Zdd� Z G d d� de �ZeZd S )aP ElGamal public-key algorithm (randomized encryption and signature). Signature algorithm ------------------- The security of the ElGamal signature scheme is based (like DSA) on the discrete logarithm problem (DLP_). Given a cyclic group, a generator *g*, and an element *h*, it is hard to find an integer *x* such that *g^x = h*. The group is the largest multiplicative sub-group of the integers modulo *p*, with *p* prime. The signer holds a value *x* (*0<x<p-1*) as private key, and its public key (*y* where *y=g^x mod p*) is distributed. The ElGamal signature is twice as big as *p*. Encryption algorithm -------------------- The security of the ElGamal encryption scheme is based on the computational Diffie-Hellman problem (CDH_). Given a cyclic group, a generator *g*, and two integers *a* and *b*, it is difficult to find the element *g^{ab}* when only *g^a* and *g^b* are known, and not *a* and *b*. As before, the group is the largest multiplicative sub-group of the integers modulo *p*, with *p* prime. The receiver holds a value *a* (*0<a<p-1*) as private key, and its public key (*b* where *b*=g^a*) is given to the sender. The ElGamal ciphertext is twice as big as *p*. Domain parameters ----------------- For both signature and encryption schemes, the values *(p,g)* are called *domain parameters*. They are not sensitive but must be distributed to all parties (senders and receivers). Different signers can share the same domain parameters, as can different recipients of encrypted messages. Security -------- Both DLP and CDH problem are believed to be difficult, and they have been proved such (and therefore secure) for more than 30 years. The cryptographic strength is linked to the magnitude of *p*. In 2012, a sufficient size for *p* is deemed to be 2048 bits. For more information, see the most recent ECRYPT_ report. Even though ElGamal algorithms are in theory reasonably secure for new designs, in practice there are no real good reasons for using them. The signature is four times larger than the equivalent DSA, and the ciphertext is two times larger than the equivalent RSA. Functionality ------------- This module provides facilities for generating new ElGamal keys and for constructing them from known components. ElGamal keys allows you to perform basic signing, verification, encryption, and decryption. >>> from Crypto import Random >>> from Crypto.Random import random >>> from Crypto.PublicKey import ElGamal >>> from Crypto.Util.number import GCD >>> from Crypto.Hash import SHA >>> >>> message = "Hello" >>> key = ElGamal.generate(1024, Random.new().read) >>> h = SHA.new(message).digest() >>> while 1: >>> k = random.StrongRandom().randint(1,key.p-1) >>> if GCD(k,key.p-1)==1: break >>> sig = key.sign(h,k) >>> ... >>> if key.verify(h,sig): >>> print "OK" >>> else: >>> print "Incorrect signature" .. _DLP: http://www.cosic.esat.kuleuven.be/publications/talk-78.pdf .. _CDH: http://en.wikipedia.org/wiki/Computational_Diffie%E2%80%93Hellman_assumption .. _ECRYPT: http://www.ecrypt.eu.org/documents/D.SPA.17.pdf z$Id$�generate� construct�error� ElGamalobj� )�*)�numberc @ s e Zd ZdS )r N)�__name__� __module__�__qualname__� r r �K/opt/alt/python37/lib64/python3.7/site-packages/Crypto/PublicKey/ElGamal.pyr s s Nc C sH t � }|r|d� x6tt| d |��}d| d |_tj|j|d�rP qW |rV|d� x�t�d|j|�|_d}t|jd|j�dkr�d}|r�t|j||j�dkr�d}|r�t |jd |j�d dkr�d}t� |j|j�}|r�t |jd |�d dkr�d}|rXP qXW |�r|d� t�d|jd |�|_|�r0|d � t|j|j|j�|_|S ) a' Randomly generate a fresh, new ElGamal key. The key will be safe for use for both encryption and signature (although it should be used for **only one** purpose). :Parameters: bits : int Key length, or size (in bits) of the modulus *p*. Recommended value is 2048. randfunc : callable Random number generation function; it should accept a single integer N and return a string of random data N bytes long. progress_func : callable Optional function that will be called with a short string containing the key parameter currently being generated; it's useful for interactive applications where a user is waiting for a key to be generated. :attention: You should always use a cryptographically secure random number generator, such as the one defined in the ``Crypto.Random`` module; **don't** just use the current time and the ``random`` module. :Return: An ElGamal key object (`ElGamalobj`). zp � � )�randfunczg � r zx zy ) r ZbignumZgetPrime�pr ZisPrimeZgetRandomRange�g�pow�divmod�inverse�x�y)�bitsr Z progress_func�obj�qZsafeZginvr r r r w s>