From b7d304ba6f675c6a601296cb6c3cd24f5376e846 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Thu, 29 Oct 2026 07:20:24 -0500 Subject: [PATCH] feat(rsa): implement oaep encryption, decryption, and failure handling --- src/myencrypter/__init__.py | 2 +- src/myencrypter/algorithms/rsa.py | 35 ++++++++++++++++++++++++-- tests/test_rsa_encryption.py | 41 +++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 tests/test_rsa_encryption.py diff --git a/src/myencrypter/__init__.py b/src/myencrypter/__init__.py index 40274ee..f65a290 100644 --- a/src/myencrypter/__init__.py +++ b/src/myencrypter/__init__.py @@ -1,2 +1,2 @@ def main() -> None: - print("Hello from encryption-tutorial!") + print("Hello from myencrypter!") diff --git a/src/myencrypter/algorithms/rsa.py b/src/myencrypter/algorithms/rsa.py index 78e4075..fc9dbb2 100644 --- a/src/myencrypter/algorithms/rsa.py +++ b/src/myencrypter/algorithms/rsa.py @@ -1,8 +1,8 @@ from dataclasses import dataclass from pathlib import Path -from cryptography.hazmat.primitives import serialization -from cryptography.hazmat.primitives.asymmetric import rsa +from cryptography.hazmat.primitives import hashes, serialization +from cryptography.hazmat.primitives.asymmetric import padding, rsa @dataclass(frozen=True) @@ -16,6 +16,10 @@ class KeySizeError(ValueError): pass +class DecryptionError(Exception): + pass + + def _validate_rsa_key_size(key_size: int) -> int: if key_size < 2048: raise KeySizeError(f"Key size must be at least 2048 bits, got {key_size}") @@ -107,3 +111,30 @@ def load_rsa_public_key_pem(pub_path: Path) -> rsa.RSAPublicKey: if not isinstance(key, rsa.RSAPublicKey): raise TypeError(f"Expected RSA public key, got {type(key)}") return key + + +def encrypt_rsa(public_key: rsa.RSAPublicKey, plaintext: bytes) -> bytes: + """Encrypt plaintext bytes using RSA-OAEP with SHA-256.""" + return public_key.encrypt( + plaintext=plaintext, + padding=padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None, + ), + ) + + +def decrypt_rsa(private_key: rsa.RSAPrivateKey, ciphertext: bytes) -> bytes: + """Decrypt ciphertext bytes using RSA-OAEP with SHA-256.""" + try: + return private_key.decrypt( + ciphertext=ciphertext, + padding=padding.OAEP( + mgf=padding.MGF1(algorithm=hashes.SHA256()), + algorithm=hashes.SHA256(), + label=None, + ), + ) + except ValueError as exc: + raise DecryptionError("Failed to decrypt with RSA: invalid padding") from exc diff --git a/tests/test_rsa_encryption.py b/tests/test_rsa_encryption.py new file mode 100644 index 0000000..a332510 --- /dev/null +++ b/tests/test_rsa_encryption.py @@ -0,0 +1,41 @@ +import pytest +from cryptography.hazmat.primitives.asymmetric import rsa + +from myencrypter.algorithms.rsa import DecryptionError, decrypt_rsa, encrypt_rsa + + +def test_rsa_encryption_decryption() -> None: + private_key = rsa.generate_private_key( + public_exponent=65537, + key_size=2048, + ) + + public_key = private_key.public_key() + + message: bytes = b"Hello, from kiskaadee!" + ciphertext: bytes = encrypt_rsa(public_key, message) + + assert isinstance(ciphertext, bytes) + decrypted_message: bytes = decrypt_rsa(private_key, ciphertext) + assert decrypted_message == message + + +def test_rsa_decryption_with_wrong_key_fails() -> None: + private_key = rsa.generate_private_key( + public_exponent=65537, + key_size=2048, + ) + + wrong_private_key = rsa.generate_private_key( + public_exponent=65537, + key_size=2048, + ) + + public_key = private_key.public_key() + + message: bytes = b"Hello, from kiskaadee!" + ciphertext: bytes = encrypt_rsa(public_key, message) + + assert isinstance(ciphertext, bytes) + with pytest.raises(DecryptionError): + decrypt_rsa(wrong_private_key, ciphertext)