Skip to content

Commit

Permalink
Merge pull request #394 from ignaloidas/default-to-3DES
Browse files Browse the repository at this point in the history
Default to 3DES when preferred algorithms don't have any supporting algorithm
  • Loading branch information
Commod0re committed Nov 3, 2022
2 parents 0ba2bc5 + e0eb8fb commit 10f7d76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pgpy/pgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2437,7 +2437,7 @@ def encrypt(self, message, sessionkey=None, **prefs):
uid = next(iter(self.userids), None)
if uid is None and self.parent is not None:
uid = next(iter(self.parent.userids), None)
pref_cipher = next(c for c in uid.selfsig.cipherprefs if c.is_supported)
pref_cipher = next((c for c in uid.selfsig.cipherprefs if c.is_supported), SymmetricKeyAlgorithm.TripleDES)
cipher_algo = prefs.pop('cipher', pref_cipher)

if cipher_algo not in uid.selfsig.cipherprefs:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_99_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,25 @@ def test_spurious_dash_escapes():
key = PGPKey.from_file('tests/testdata/keys/rsa.1.pub.asc')[0]
message = PGPMessage.from_blob(message_data)
assert key.verify(message)

@pytest.mark.regression(issue=393)
def test_preference_no_supported_ciphers():
from pgpy import PGPMessage
keyblob = '''-----BEGIN PGP PUBLIC KEY BLOCK-----
xo0EYmAayAEEAPQ0oYaLBPIF7bOA71X6kI0j7xnevfaHWfRzCoOJcDH4WJIRDbXD
cUkcKJoc710mRZREQm6HYcd61DBsohW3HT96h0/1HE4E1SbgN1GIAjh9rFiNy9F4
/M5LHtn+KzsG1yB4eTOyOw5nr+y6DEVaw0a3dnTC541x1LnMplU0HAW3ABEBAAHN
G3Rlc3Qga2V5IDx0ZXN0QGV4YW1wbGUuY29tPsK8BBMBAgAmBQJiYBrLAhsvAhUC
Ah4BFiEErFJI/ShgHoPluKoDdK7/b7+9sGcACgkQdK7/b7+9sGcNlgP8D8hL8g3e
wP0HNxs7lXHyWT/uv6d02qEu/ZZ0fqf3CbEJ0aJQqDL61/utWK9hOt6nwTh+YvQP
VSigo9gDqyHdbq8fvKxeiKJIkCZmESN0RpSjf+JQrBLs9DSOWh4bLQ9EiE6GfgdL
n1hJ4Wx1aFL7WuPz/wj69MxPLh3nrG6i4iw=
=aDo/
-----END PGP PUBLIC KEY BLOCK-----'''

pubkey, _ = PGPKey.from_blob(keyblob)
msg = PGPMessage.new('asdf')
with warnings.catch_warnings():
warnings.simplefilter('ignore')
pubkey.encrypt(msg)

0 comments on commit 10f7d76

Please sign in to comment.