MCPcopy Create free account
hub / github.com/SecurityInnovation/PGPy / encrypt

Method encrypt

pgpy/pgp.py:2499–2564  ·  view source on GitHub ↗

encrypt(message[, sessionkey=None], **prefs) Encrypt a PGPMessage using this key. :param message: The message to encrypt. :type message: :py:obj:`PGPMessage` :param sessionkey: Provide a session key to use when encrypting something. Default is ``None``.

(self, message, sessionkey=None, **prefs)

Source from the content-addressed store, hash-verified

2497
2498 @KeyAction(KeyFlags.EncryptCommunications, KeyFlags.EncryptStorage, is_public=True)
2499 def encrypt(self, message, sessionkey=None, **prefs):
2500 """encrypt(message[, sessionkey=None], **prefs)
2501
2502 Encrypt a PGPMessage using this key.
2503
2504 :param message: The message to encrypt.
2505 :type message: :py:obj:`PGPMessage`
2506 :param sessionkey: Provide a session key to use when encrypting something. Default is ``None``.
2507 If ``None``, a session key of the appropriate length will be generated randomly.
2508
2509 .. warning::
2510
2511 Care should be taken when making use of this option! Session keys *absolutely need*
2512 to be unpredictable! Use the ``gen_key()`` method on the desired
2513 :py:obj:`~constants.SymmetricKeyAlgorithm` to generate the session key!
2514 :type sessionkey: ``bytes``, ``str``
2515
2516 :raises: :py:exc:`~errors.PGPEncryptionError` if encryption failed for any reason.
2517 :returns: A new :py:obj:`PGPMessage` with the encrypted contents of ``message``
2518
2519 The following optional keyword arguments can be used with :py:meth:`PGPKey.encrypt`:
2520
2521 :keyword cipher: Specifies the symmetric block cipher to use when encrypting the message.
2522 :type cipher: :py:obj:`~constants.SymmetricKeyAlgorithm`
2523 :keyword user: Specifies the User ID to use as the recipient for this encryption operation, for the purposes of
2524 preference defaults and selection validation.
2525 :type user: ``str``, ``unicode``
2526 """
2527 user = prefs.pop('user', None)
2528 uid = None
2529 if user is not None:
2530 uid = self.get_uid(user)
2531 else:
2532 uid = next(iter(self.userids), None)
2533 if uid is None and self.parent is not None:
2534 uid = next(iter(self.parent.userids), None)
2535 pref_cipher = next((c for c in uid.selfsig.cipherprefs if c.is_supported), SymmetricKeyAlgorithm.TripleDES)
2536 cipher_algo = prefs.pop('cipher', pref_cipher)
2537
2538 if cipher_algo not in uid.selfsig.cipherprefs:
2539 warnings.warn("Selected symmetric algorithm not in key preferences", stacklevel=3)
2540
2541 if message.is_compressed and message._compression not in uid.selfsig.compprefs:
2542 warnings.warn("Selected compression algorithm not in key preferences", stacklevel=3)
2543
2544 if sessionkey is None:
2545 sessionkey = cipher_algo.gen_key()
2546
2547 # set up a new PKESessionKeyV3
2548 pkesk = PKESessionKeyV3()
2549 pkesk.encrypter = bytearray(binascii.unhexlify(self.fingerprint.keyid.encode('latin-1')))
2550 pkesk.pkalg = self.key_algorithm
2551 pkesk.encrypt_sk(self._key, cipher_algo, sessionkey)
2552
2553 if message.is_encrypted: # pragma: no cover
2554 _m = message
2555
2556 else:

Calls 8

get_uidMethod · 0.95
encrypt_skMethod · 0.95
encryptMethod · 0.95
PKESessionKeyV3Class · 0.85
PGPMessageClass · 0.85
gen_keyMethod · 0.80
__bytes__Method · 0.45