Submit
Path:
~
/
/
opt
/
alt
/
python37
/
lib64
/
python3.7
/
site-packages
/
Crypto
/
Cipher
/
__pycache__
/
File Content:
PKCS1_v1_5.cpython-37.pyc
B ��c�# � @ sF d Z dZddgZddlmZ ddlT ddlZG dd� d�Zd d� ZdS ) a[ RSA encryption protocol according to PKCS#1 v1.5 See RFC3447__ or the `original RSA Labs specification`__ . This scheme is more properly called ``RSAES-PKCS1-v1_5``. **If you are designing a new protocol, consider using the more robust PKCS#1 OAEP.** As an example, a sender may encrypt a message in this way: >>> from Crypto.Cipher import PKCS1_v1_5 >>> from Crypto.PublicKey import RSA >>> from Crypto.Hash import SHA >>> >>> message = 'To be encrypted' >>> h = SHA.new(message) >>> >>> key = RSA.importKey(open('pubkey.der').read()) >>> cipher = PKCS1_v1_5.new(key) >>> ciphertext = cipher.encrypt(message+h.digest()) At the receiver side, decryption can be done using the private part of the RSA key: >>> From Crypto.Hash import SHA >>> from Crypto import Random >>> >>> key = RSA.importKey(open('privkey.der').read()) >>> >>> dsize = SHA.digest_size >>> sentinel = Random.new().read(15+dsize) # Let's assume that average data length is 15 >>> >>> cipher = PKCS1_v1_5.new(key) >>> message = cipher.decrypt(ciphertext, sentinel) >>> >>> digest = SHA.new(message[:-dsize]).digest() >>> if digest==message[-dsize:]: # Note how we DO NOT look for the sentinel >>> print "Encryption was correct." >>> else: >>> print "Encryption was not correct." :undocumented: __revision__, __package__ .. __: http://www.ietf.org/rfc/rfc3447.txt .. __: http://www.rsa.com/rsalabs/node.asp?id=2125. z$Id$�new�PKCS115_Cipher� )�ceil_div)�*Nc @ s8 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd d� ZdS ) r zAThis cipher can perform PKCS#1 v1.5 RSA encryption or decryption.c C s || _ dS )a Initialize this PKCS#1 v1.5 cipher object. :Parameters: key : an RSA key object If a private half is given, both encryption and decryption are possible. If a public half is given, only encryption is possible. N)�_key)�self�key� r �K/opt/alt/python37/lib64/python3.7/site-packages/Crypto/Cipher/PKCS1_v1_5.py�__init__P s zPKCS115_Cipher.__init__c C s | j �� S )z=Return True if this cipher object can be used for encryption.)r �can_encrypt)r r r r r Z s zPKCS115_Cipher.can_encryptc C s | j �� S )z=Return True if this cipher object can be used for decryption.)r �can_decrypt)r r r r r ^ s zPKCS115_Cipher.can_decryptc C s� | j j}tjj�| j j�}t|d�}t|�}||d kr@t d��G dd� d�}t tt||�||| d ����}t d�| td� | }| j �|d�d } td�|t| � | } | S ) a� Produce the PKCS#1 v1.5 encryption of a message. This function is named ``RSAES-PKCS1-V1_5-ENCRYPT``, and is specified in section 7.2.1 of RFC3447. For a complete example see `Crypto.Cipher.PKCS1_v1_5`. :Parameters: message : byte string The message to encrypt, also known as plaintext. It can be of variable length, but not longer than the RSA modulus (in bytes) minus 11. :Return: A byte string, the ciphertext in which the message is encrypted. It is as long as the RSA modulus (in bytes). :Raise ValueError: If the RSA key length is not sufficiently long to deal with the given message. � � zPlaintext is too long.c @ s e Zd Zdd� Zdd� ZdS )z/PKCS115_Cipher.encrypt.<locals>.nonZeroRandBytec S s || _ d S )N)�rf)r r r r r r � � z8PKCS115_Cipher.encrypt.<locals>.nonZeroRandByte.__init__c S s$ xt |�dkr| �d�d }qW |S )Nr � )Zbordr )r �cr r r �__call__� s z8PKCS115_Cipher.encrypt.<locals>.nonZeroRandByte.__call__N)�__name__� __module__�__qualname__r r r r r r �nonZeroRandByte� s r � z r )r Z _randfunc�Crypto�Util�number�size�nr �len� ValueError�tobytes�list�map�b�bchr�encrypt)r �messageZrandFunc�modBits�kZmLenr Zps�em�mr r r r r&