Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Crypto.PublicKey import RSA
- from Crypto.Random import get_random_bytes
- from Crypto.Cipher import AES, PKCS1_OAEP
- class Encrypt:
- def __init__(self):
- self.key = RSA.generate(2048)
- def gen_public_key(self):
- self.public_key = self.key.publickey().export_key()
- file_out = open("receiver.pem", "wb")
- file_out.write(self.public_key)
- file_out.close()
- def gen_private_key(self):
- private_key = self.key.export_key()
- file_out = open("private.pem", "wb")
- file_out.write(private_key)
- file_out.close()
- def encode(self):
- data = "I met aliens in UFO. Here is the map,I met aliens in UFO. Here is the mapI met aliens in UFO. Here is the map,I met aliens in UFO. Here is the mapI met aliens in UFO. Here is the map.".encode("utf-8")
- file_out = open("encrypted_data.bin", "wb")
- recipient_key = RSA.import_key(open("receiver.pem").read())
- session_key = get_random_bytes(16)
- # Encrypt the session key with the public RSA key
- cipher_rsa = PKCS1_OAEP.new(recipient_key)
- enc_session_key = cipher_rsa.encrypt(session_key)
- # Encrypt the data with the AES session key
- cipher_aes = AES.new(session_key, AES.MODE_EAX)
- ciphertext, tag = cipher_aes.encrypt_and_digest(data)
- [ file_out.write(x) for x in (enc_session_key, cipher_aes.nonce, tag, ciphertext) ]
- file_out.close()
- def decode(self):
- file_in = open("encrypted_data.bin", "rb")
- private_key = RSA.import_key(open("private.pem").read())
- enc_session_key, nonce, tag, ciphertext = \
- [file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1)]
- # Decrypt the session key with the private RSA key
- cipher_rsa = PKCS1_OAEP.new(private_key)
- session_key = cipher_rsa.decrypt(enc_session_key)
- # Decrypt the data with the AES session key
- cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
- data = cipher_aes.decrypt_and_verify(ciphertext, tag)
- print(data.decode("utf-8"))
- if __name__ == "__main__":
- e = Encrypt()
- # e.gen_public_key()
- # e.encode()
- e.gen_private_key()
- e.decode()
Advertisement
Add Comment
Please, Sign In to add comment