
from Crypto.PublicKey import RSA


# Generates and writes byte string with object of RSA key object
def create_key():
    key = RSA.generate(2048)
    f = open('key.pem', 'w')
    f.write(key.exportKey('PEM'))
    f.close()


# Reads an exported key-bytestring from file and returns an RSA key object
def retrieve_key():
    f = open('key.pem', 'r')
    key = RSA.importKey(f.read())
    return key


def get_public_bytestring():
    key = retrieve_key()
    return key.publickey().exportKey()


# Use own private key to decrypt broadcasted message
def decrypt_message(message):
    key_obj = retrieve_key()
    return key_obj.decrypt(message)


# Use given id to encrypt message
def encrypt_message(key_string, message):
    key = RSA.importKey(key_string)
    return key.encrypt(message, 123)

from Crypto.PublicKey import RSA


# Generates and writes byte string with object of RSA key object
def create_key():
    key = RSA.generate(2048)
    with open('key.pem', 'w') as f:
        f.write(key.exportKey('PEM'))


# Reads an exported key-bytestring from file and returns an RSA key object
def retrieve_key():
    with open('key.pem', 'r') as f:
        key = RSA.importKey(f.read())
        return key


def get_public_bytestring():
    key = retrieve_key()
    return key.publickey().exportKey()


# Use own private key to decrypt broadcasted message
def decrypt_message(message):
    key = retrieve_key()
    return key.decrypt(message)


# Use given id to encrypt message
def encrypt_message(key_string, message):
    key = RSA.importKey(key_string)
    return key.encrypt(message, 123)

Use 'with ... as ...' for file opening. Standardize variable names.
Use 'with ... as ...' for file opening. Standardize variable names.

  
  from Crypto.PublicKey import RSA
  
  
  # Generates and writes byte string with object of RSA key object
  def create_key():
      key = RSA.generate(2048)
-     f = open('key.pem', 'w')
+     with open('key.pem', 'w') as f:
-     f.write(key.exportKey('PEM'))
+         f.write(key.exportKey('PEM'))
-     f.close()
  
  
  # Reads an exported key-bytestring from file and returns an RSA key object
  def retrieve_key():
-     f = open('key.pem', 'r')
+     with open('key.pem', 'r') as f:
-     key = RSA.importKey(f.read())
+         key = RSA.importKey(f.read())
-     return key
+         return key
  
  
  def get_public_bytestring():
      key = retrieve_key()
      return key.publickey().exportKey()
  
  
  # Use own private key to decrypt broadcasted message
  def decrypt_message(message):
-     key_obj = retrieve_key()
+     key = retrieve_key()
-     return key_obj.decrypt(message)
+     return key.decrypt(message)
  
  
  # Use given id to encrypt message
  def encrypt_message(key_string, message):
      key = RSA.importKey(key_string)
      return key.encrypt(message, 123)
+ 
Use 'with ... as ...' for file opening. Standardize variable names.
## Code Before:

from Crypto.PublicKey import RSA


# Generates and writes byte string with object of RSA key object
def create_key():
    key = RSA.generate(2048)
    f = open('key.pem', 'w')
    f.write(key.exportKey('PEM'))
    f.close()


# Reads an exported key-bytestring from file and returns an RSA key object
def retrieve_key():
    f = open('key.pem', 'r')
    key = RSA.importKey(f.read())
    return key


def get_public_bytestring():
    key = retrieve_key()
    return key.publickey().exportKey()


# Use own private key to decrypt broadcasted message
def decrypt_message(message):
    key_obj = retrieve_key()
    return key_obj.decrypt(message)


# Use given id to encrypt message
def encrypt_message(key_string, message):
    key = RSA.importKey(key_string)
    return key.encrypt(message, 123)
## Instruction:
Use 'with ... as ...' for file opening. Standardize variable names.
## Code After:

from Crypto.PublicKey import RSA


# Generates and writes byte string with object of RSA key object
def create_key():
    key = RSA.generate(2048)
    with open('key.pem', 'w') as f:
        f.write(key.exportKey('PEM'))


# Reads an exported key-bytestring from file and returns an RSA key object
def retrieve_key():
    with open('key.pem', 'r') as f:
        key = RSA.importKey(f.read())
        return key


def get_public_bytestring():
    key = retrieve_key()
    return key.publickey().exportKey()


# Use own private key to decrypt broadcasted message
def decrypt_message(message):
    key = retrieve_key()
    return key.decrypt(message)


# Use given id to encrypt message
def encrypt_message(key_string, message):
    key = RSA.importKey(key_string)
    return key.encrypt(message, 123)
