#!/usr/bin/env python3

from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.x509 import load_pem_x509_certificate
import base64

PUBLIC_CERTIFICATE = "public_certificate.pem"
PLAINTEXT = b"\xab\xad\xca\xfe\xab\xad\xca\xfe\xab\xad\xca\xfe\xab\xad\xca\xfe"

# read public certificate
with open(PUBLIC_CERTIFICATE, "rb") as file:
    public_certificate_content = file.read()

# load public certificate and extract public key
certificate = load_pem_x509_certificate(
	data = public_certificate_content,
	backend = default_backend()
)
public_key = certificate.public_key();

# create padding OAEP and SHA1
pad = padding.OAEP(
    mgf = padding.MGF1(algorithm=hashes.SHA1()),
    algorithm = hashes.SHA1(),
    label = None
)

# RSA encryption
ciphertext = public_key.encrypt(
    plaintext = PLAINTEXT,
    padding = pad
)

# Base64 encryption
ciphertext64 = base64.b64encode(ciphertext)

print("Ciphertext - RSA =", ciphertext.hex())
print("Ciphertext - Base64 =", ciphertext64)
