# Buggy code
def _encrypt_private(self, ret, dictkey, target):
    """
    The server equivalent of ReqChannel.crypted_transfer_decode_dictentry
    """
    # encrypt with a specific AES key
    pubfn = os.path.join(self.opts["pki_dir"], "minions", target)
    key = salt.crypt.Crypticle.generate_key_string()
    pcrypt = salt.crypt.Crypticle(self.opts, key)
    try:
        with salt.utils.fopen(pubfn) as f:
            pub = RSA.importKey(f.read())
    except (ValueError, IndexError, TypeError):
        return self.crypticle.dumps({})

    pret = {}
    cipher = PKCS1_OAEP.new(pub)
    pret["key"] = cipher.encrypt(key)
    pret[dictkey] = pcrypt.dumps(ret if ret is not False else {})
    return pret


# Traceback (IOError)
2015-12-13 06:53:53,199 [salt.master      ][ERROR   ][17378] Error in function _mine:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1007, in _mine
load = self.__verify_load(load, ('id', 'data', 'tok'))
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 924, in __verify_load
if not self.__verify_minion(load['id'], load['tok']):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 824, in __verify_minion
with salt.utils.fopen(pub_path, 'r') as fp_:
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1204, in fopen
fhandle = open(*args, **kwargs)
IOError: [Errno 2] No such file or directory: '/etc/salt/pki/master/minions/ause1-encoder546'
2015-12-13 06:54:25,104 [salt.transport.zeromq][ERROR   ][17378] Bad load from minion
2015-12-13 06:54:35,228 [salt.transport.zeromq][ERROR   ][17370] Bad load from minion
2015-12-13 06:54:46,744 [salt.transport.zeromq][ERROR   ][17398] Bad load from minion
2015-12-13 06:54:53,352 [salt.master      ][ERROR   ][17317] Error in function _mine:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1007, in _mine
load = self.__verify_load(load, ('id', 'data', 'tok'))
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 924, in __verify_load
if not self.__verify_minion(load['id'], load['tok']):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 824, in __verify_minion
with salt.utils.fopen(pub_path, 'r') as fp_:
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1204, in fopen
fhandle = open(*args, **kwargs)
IOError: [Errno 2] No such file or directory: '/etc/salt/pki/master/minions/ause1-encoder546'
2015-12-13 06:55:00,814 [salt.master      ][ERROR   ][17343] Error in function _mine:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1007, in _mine
load = self.__verify_load(load, ('id', 'data', 'tok'))
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 924, in __verify_load
if not self.__verify_minion(load['id'], load['tok']):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 824, in __verify_minion
with salt.utils.fopen(pub_path, 'r') as fp_:
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1204, in fopen
fhandle = open(*args, **kwargs)
IOError: [Errno 2] No such file or directory: '/etc/salt/pki/master/minions/ause1-encoder646'
2015-12-13 06:55:17,444 [salt.master      ][ERROR   ][17443] Error in function _mine:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1379, in run_func
ret = getattr(self, func)(load)
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 1007, in _mine
load = self.__verify_load(load, ('id', 'data', 'tok'))
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 924, in __verify_load
if not self.__verify_minion(load['id'], load['tok']):
File "/usr/lib/python2.7/dist-packages/salt/master.py", line 824, in __verify_minion
with salt.utils.fopen(pub_path, 'r') as fp_:
File "/usr/lib/python2.7/dist-packages/salt/utils/__init__.py", line 1204, in fopen
fhandle = open(*args, **kwargs)
IOError: [Errno 2] No such file or directory: '/etc/salt/pki/master/minions/ause1-encoder66'

# Fixed code
def _encrypt_private(self, ret, dictkey, target):
    """
    The server equivalent of ReqChannel.crypted_transfer_decode_dictentry
    """
    # encrypt with a specific AES key
    pubfn = os.path.join(self.opts["pki_dir"], "minions", target)
    key = salt.crypt.Crypticle.generate_key_string()
    pcrypt = salt.crypt.Crypticle(self.opts, key)
    try:
        with salt.utils.fopen(pubfn) as f:
            pub = RSA.importKey(f.read())
    except (ValueError, IndexError, TypeError):
        return self.crypticle.dumps({})
    except IOError:
        log.error("AES key not found")
        return "AES key not found"

    pret = {}
    cipher = PKCS1_OAEP.new(pub)
    pret["key"] = cipher.encrypt(key)
    pret[dictkey] = pcrypt.dumps(ret if ret is not False else {})
    return pret