rewrite the logic of cbc-mode.py