finish key encoding port. untested.