#!/usr/bin/python3 ''' Creates RSA public and private keys using python, saves keys into public.pem and private.pem respectively Author: Tim Pierson, Dartmouth CS55, Winter 2021 Based Wenliang Du https://github.com/kevin-w-du/BookCode/tree/master/Public_Key_Cryptography Usage: python3 rsa_key_gen.py where password used to protect the keys generated python3 rsa_key_gen.py cs55 ''' import sys from Crypto.PublicKey import RSA key_size = 2048 if __name__ == '__main__': #usage check: make sure we got a filename to read if len(sys.argv) != 2: print("Usage: python3 rsa_key_gen.py ") print("Did you provide a password?") exit() print("Generating keys using password",sys.argv[1]) key = RSA.generate(key_size) pem = key.export_key(format='PEM', passphrase=sys.argv[1]) #write private key print("Writing private.pem") f = open('private.pem','wb') f.write(pem) f.close() #write public key print("Writing public.pem") pub = key.publickey() pub_pem = pub.export_key(format='PEM') f = open('public.pem','wb') f.write(pub_pem) f.close()