Given a block of ciphertext as a string, and a gpg object, try to decrypt
    the cipher and return the decrypted string. If the cipher cannot be
    decrypted, log the error, and return the ciphertext back out.

def _decrypt_ciphertext(cipher):
    '''
    Given a block of ciphertext as a string, and a gpg object, try to decrypt
    the cipher and return the decrypted string. If the cipher cannot be
    decrypted, log the error, and return the ciphertext back out.
    '''
    try:
        cipher = salt.utils.stringutils.to_unicode(cipher).replace(r'\n', '\n')
    except UnicodeDecodeError:
        # ciphertext is binary
        pass
    cipher = salt.utils.stringutils.to_bytes(cipher)
    cmd = [_get_gpg_exec(), '--homedir', _get_key_dir(), '--status-fd', '2',
           '--no-tty', '-d']
    proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
    decrypted_data, decrypt_error = proc.communicate(input=cipher)
    if not decrypted_data:
        try:
            cipher = salt.utils.stringutils.to_unicode(cipher)
        except UnicodeDecodeError:
            # decrypted data contains undecodable binary data
            pass
        log.warning(
            'Could not decrypt cipher %s, received: %s',
            cipher,
            decrypt_error
        )
        return cipher
    else:
        try:
            decrypted_data = salt.utils.stringutils.to_unicode(decrypted_data)
        except UnicodeDecodeError:
            # decrypted data contains undecodable binary data
            pass
        return decrypted_data