From bd226ea4958dee6aee82cb1824189b01c9c53bca Mon Sep 17 00:00:00 2001 From: brunombpereira Date: Tue, 16 May 2023 18:31:21 +0100 Subject: [PATCH] [LABI] tema01: added publicKey_RSA.py --- .../labi/tema01/src/publicKey_RSA.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 1ano/2semestre/labi/tema01/src/publicKey_RSA.py diff --git a/1ano/2semestre/labi/tema01/src/publicKey_RSA.py b/1ano/2semestre/labi/tema01/src/publicKey_RSA.py new file mode 100644 index 0000000..f5fbed7 --- /dev/null +++ b/1ano/2semestre/labi/tema01/src/publicKey_RSA.py @@ -0,0 +1,23 @@ +from Crypto.PublicKey import RSA + + +def generate_key_pair(file_name, encryption_key, key_size): + # Generate the RSA key pair + key = RSA.generate(key_size) + + # Write the private key to a file + private_key = key.exportKey("PEM", encryption_key ) + with open(file_name + '_private.pem', 'wb') as f: + f.write(private_key) + + # Write the public key to a file + public_key = key.publickey().exportKey() + with open(file_name + '_public.pem', 'wb') as f: + f.write(public_key) + + +if __name__ == '__main__': + file_name = input('Enter file name to save the key pair: ') + encryption_key = input('Enter encryption key for the private key (press enter for no encryption): ') + key_size = int(input('Enter the key size (in bits): ')) + generate_key_pair(file_name, encryption_key, key_size)