Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created July 31, 2025 14:39
Show Gist options
  • Select an option

  • Save Hermann-SW/3d7481e57a4f62a3c37d6a228deec0fc to your computer and use it in GitHub Desktop.

Select an option

Save Hermann-SW/3d7481e57a4f62a3c37d6a228deec0fc to your computer and use it in GitHub Desktop.
""" pylinted, and code formatted with black """
from math import lcm
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
# P and Q are prime factors of largest factored sofar RSA-250
#
P = 641352894770715802787901901705773890848250147429434472081168596 * 10**62
P += 32024532344630238623598752668347708737661925585694639798853367
Q = 333720275949781565562260106053551142279407603447675546667845209 * 10**62
Q += 87023841729210037080257448673296881877565718986258036932062711
E = 0x10001
L = lcm(P - 1, Q - 1)
private_key = rsa.RSAPrivateNumbers(
p=P,
q=Q,
d=pow(E, -1, L),
dmp1=pow(E, -1, P - 1),
dmq1=pow(E, -1, Q - 1),
iqmp=pow(Q, -1, P),
public_numbers=rsa.RSAPublicNumbers(e=E, n=P * Q),
).private_key(default_backend())
pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
encryption_algorithm=serialization.NoEncryption(),
format=serialization.PrivateFormat.TraditionalOpenSSL,
)
with open("private_key.pem", "wb") as f:
f.write(pem)
public_key = private_key.public_key()
pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
with open("public_key.pem", "wb") as f:
f.write(pem)
@Hermann-SW
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment