feat(rsa): implement oaep encryption, decryption, and failure handling
This commit is contained in:
1 parent
9cedb879d2
commit
b7d304ba6f
3 files changed
+75
-3
No files matched your search
@@ -1,2 +1,2 @@
|
||||
def main() -> None:
|
||||
print("Hello from encryption-tutorial!")
|
||||
print("Hello from myencrypter!")
|
||||
@@ -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
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user