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

Method decrypt

pgpy/pgp.py:2567–2596  ·  view source on GitHub ↗

Decrypt a PGPMessage using this key. :param message: An encrypted :py:obj:`PGPMessage` :raises: :py:exc:`~errors.PGPError` if the key is not private, or protected but not unlocked. :raises: :py:exc:`~errors.PGPDecryptionError` if decryption fails for any other reaso

(self, message)

Source from the content-addressed store, hash-verified

2565
2566 @KeyAction(is_unlocked=True, is_public=False)
2567 def decrypt(self, message):
2568 """
2569 Decrypt a PGPMessage using this key.
2570
2571 :param message: An encrypted :py:obj:`PGPMessage`
2572 :raises: :py:exc:`~errors.PGPError` if the key is not private, or protected but not unlocked.
2573 :raises: :py:exc:`~errors.PGPDecryptionError` if decryption fails for any other reason.
2574 :returns: A new :py:obj:`PGPMessage` with the decrypted contents of ``message``.
2575 """
2576 if not message.is_encrypted:
2577 warnings.warn("This message is not encrypted", stacklevel=3)
2578 return message
2579
2580 if self.fingerprint.keyid not in message.encrypters:
2581 sks = set(self.subkeys)
2582 mis = set(message.encrypters)
2583 if sks & mis:
2584 skid = list(sks & mis)[0]
2585 return self.subkeys[skid].decrypt(message)
2586
2587 raise PGPError("Cannot decrypt the provided message with this key")
2588
2589 pkesk = next(pk for pk in message._sessionkeys if pk.pkalg == self.key_algorithm and pk.encrypter == self.fingerprint.keyid)
2590 alg, key = pkesk.decrypt_sk(self._key)
2591
2592 # now that we have the symmetric cipher used and the key, we can decrypt the actual message
2593 decmsg = PGPMessage()
2594 decmsg.parse(message.message.decrypt(key, alg))
2595
2596 return decmsg
2597
2598 def parse(self, data):
2599 unarmored = self.ascii_unarmor(data)

Calls 4

parseMethod · 0.95
PGPErrorClass · 0.85
PGPMessageClass · 0.85
decrypt_skMethod · 0.45