Skip to content

Instantly share code, notes, and snippets.

@bigbrett
Last active July 12, 2017 18:50
Show Gist options
  • Save bigbrett/9e4a9fa6f3974911b7b359c5362ab4f5 to your computer and use it in GitHub Desktop.
Save bigbrett/9e4a9fa6f3974911b7b359c5362ab4f5 to your computer and use it in GitHub Desktop.
Python RSA parameter generator
#!/usr/bin/python
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
keysize = 2048;
print("Key size = %s" % keysize)
generate = True
if keysize == 1024:
rsaInit_n = 0xb4d92132b03210f62e52129ae31ef25e03c2dd734a7235efd36bad80c28885f3a9ee1ab626c30072bb3fd9906bf89a259ffd9d5fd75f87a30d75178b9579b257b5dca13ca7546866ad9f2db0072d59335fb128b7295412dd5c43df2c4f2d2f9c1d59d2bb444e6dac1d9cef27190a97aae7030c5c004c5aea3cf99afe89b86d6d
rsaInit_e = 0x10001
rsaInit_d = 0x6f1e6ab386677cdc86a18f24f42073b328847724fbbd293eee9cdec29ac4dfe953a4256d7e6b9abee426db3b4ddc367a9fcf68ff168a7000d3a7fa8b9d9064ef4f271865045925660fab620fad0aeb58f946e33bdff6968f4c29ac62bd08cf53cb8be2116f2c339465a64fd02517f2bafca72c9f3ca5bbf96b24c1345eb936d1
elif keysize == 2048:
rsaInit_n = "1"
rsaInit_e = "1"
rsaInit_d = "1"
rsaInitVals= (long(rsaInit_n),long(rsaInit_e),long(rsaInit_d))
# Have library autogenerate keys
if generate:
key = RSA.generate(keysize)
else:
key = RSA.construct(rsaInitVals)
print("RSA key info:\n")
print(" modulus (n) = %s\n" % format(key.n,'#04x'))
print(" Public exponent (e) = %s\n" % format(key.e,'#04x'))
print(" Private exponent (d) = %s\n" % format(key.d,'#04x'))
# Use RSA key to encrypt some data
#message = "Hello, World!"
message = "Hello, World!"
#cipher = PKCS1_OAEP.new(key)
#ciphertext = cipher.encrypt(message)
ciphertext = key.encrypt(message, 3)
print("Message = %s\n" % message)
print("Ciphertext = 0x%s\n" % ciphertext[0].encode("hex"))
plaintext = key.decrypt(ciphertext)
print("Decoded plaintext = %s\n" % plaintext)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment