<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def cmd_fernet(key, decrypt, ttl, i, o):
 """Fernet cipher. Uses AES-128-CBC with HMAC Note: You must use a key to cipher with Fernet. Use the -k paramenter or set the FERNET_KEY configuration value. The keys can be generated with the command habu.fernet.genkey Reference: https://github.com/fernet/spec/blob/master/Spec.md Example: \b $ "I want to protect this string" | habu.fernet \b I want to protect this string """


    habucfg = loadcfg()

    if not key:
        if 'FERNET_KEY' in habucfg:
            key = habucfg['FERNET_KEY']
        else:
            print(ERROR_NOKEY, file=sys.stderr)
            sys.exit(1)

    if not ttl:
        ttl=None

    cipher = Fernet(key)

    data = i.read()

    if decrypt:
        try:
            token = cipher.decrypt(data, ttl)
        except Exception as e:
            print("Error decrypting", file=sys.stderr)
            sys.exit(1)
    else:
        token = cipher.encrypt(data)

    print(token.decode(), end='')