Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created July 31, 2025 14:39
Show Gist options
  • Save Hermann-SW/3d7481e57a4f62a3c37d6a228deec0fc to your computer and use it in GitHub Desktop.
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

Generate public and private key with modulus RSA-250 given its factors P and Q:

pi@raspberrypi5:~/RSA-250 $ ls
generate_rsa_keys_from_p_and_q.py
pi@raspberrypi5:~/RSA-250 $ ~/venv/bin/python generate_rsa_keys_from_p_and_q.py 
pi@raspberrypi5:~/RSA-250 $ ls
generate_rsa_keys_from_p_and_q.py  private_key.pem  public_key.pem
pi@raspberrypi5:~/RSA-250 $ 

@Hermann-SW
Copy link
Author

Encrypt and sign message with private key:

pi@raspberrypi5:~/RSA-250 $ echo "This is v3ry SECRET!" |
> openssl pkeyutl -sign -inkey private_key.pem -in "-" -out "msg.sig"
pi@raspberrypi5:~/RSA-250 $ ls
generate_rsa_keys_from_p_and_q.py  msg.sig  private_key.pem  public_key.pem
pi@raspberrypi5:~/RSA-250 $ 

@Hermann-SW
Copy link
Author

Verify signature, and recover message:

pi@raspberrypi5:~/RSA-250 $ openssl pkeyutl -verifyrecover -pubin -inkey public_key.pem -in msg.sig -pkeyopt rsa_padding_mode:none -hexdump
0000 - 00 01 ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0010 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0020 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0030 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0040 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0050 - ff ff 00 54 68 69 73 20-69 73 20 76 33 72 79 20   ...This is v3ry 
0060 - 53 45 43 52 45 54 21 0a-                          SECRET!.
pi@raspberrypi5:~/RSA-250 $ 

@Hermann-SW
Copy link
Author

Hermann-SW commented Jul 31, 2025

Verification of RSA-250 modulus ...

pi@raspberrypi5:~/RSA-250 $ gp -q
? printf("%x",\
641352894770715802787901901705773890848250147429434472081168596\
32024532344630238623598752668347708737661925585694639798853367\
*\
333720275949781565562260106053551142279407603447675546667845209\
87023841729210037080257448673296881877565718986258036932062711)
1321d2fddde8bd9dff379aff030de205b846eb5cecc40fa8aa9c2a85ce3e992193e873b2bc667dabe2ac3ee9dd23b3a9ed9ec0c3c7445663f5455469b727dd6fbc03b1bf95d03a13c0368645767630c7eabf5e7ab5fa27b94ade7e1e23bcc65d2a7ded1c5b364b51
?

... in public key:

pi@raspberrypi5:~/RSA-250 $ openssl rsa -pubin -inform PEM -text -noout < public_key.pem 
Public-Key: (829 bit)
Modulus:
    13:21:d2:fd:dd:e8:bd:9d:ff:37:9a:ff:03:0d:e2:
    05:b8:46:eb:5c:ec:c4:0f:a8:aa:9c:2a:85:ce:3e:
    99:21:93:e8:73:b2:bc:66:7d:ab:e2:ac:3e:e9:dd:
    23:b3:a9:ed:9e:c0:c3:c7:44:56:63:f5:45:54:69:
    b7:27:dd:6f:bc:03:b1:bf:95:d0:3a:13:c0:36:86:
    45:76:76:30:c7:ea:bf:5e:7a:b5:fa:27:b9:4a:de:
    7e:1e:23:bc:c6:5d:2a:7d:ed:1c:5b:36:4b:51
Exponent: 65537 (0x10001)
pi@raspberrypi5:~/RSA-250 $ 

@Hermann-SW
Copy link
Author

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