-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_RSA.py
31 lines (28 loc) · 1.47 KB
/
test_RSA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
# from binascii import hexlify#The message to be encrypted
message = b'Public and Private keys encryption'
#Generating private key (RsaKey object) of key length of 1024 bits
private_key = RSA.generate(1024)
#Generating the public key (RsaKey object) from the private key
public_key = private_key.publickey()
# print(type(private_key), type(public_key))#Converting the RsaKey objects to string
private_str = private_key.export_key().decode()
public_str = public_key.export_key().decode()
# print(type(private_str), type(public_str))#Writing down the private and public keys to 'pem' files
with open('private.txt', 'w') as pr:
pr.write(private_str)
with open('public.txt', 'w') as pu:
pu.write(public_str)
# #Importing keys from files, converting it into the RsaKey object
# pr_key = RSA.import_key(open('private.txt', 'r').read())
# pu_key = RSA.import_key(open('public.txt', 'r').read())
# # print(type(pr_key), type(pu_key))#Instantiating PKCS1_OAEP object with the public key for encryption
# cipher = PKCS1_OAEP.new(key=pu_key)
# #Encrypting the message with the PKCS1_OAEP object
# cipher_text = cipher.encrypt(message)
# print(cipher_text)#Instantiating PKCS1_OAEP object with the private key for decryption
# decrypt = PKCS1_OAEP.new(key=pr_key)
# #Decrypting the message with the PKCS1_OAEP object
# decrypted_message = decrypt.decrypt(cipher_text)
# print(decrypted_message)