Improved overall error handling and reporting of encryption-related code

There was some inconsistent use of terminating versus nonterminating errors, and potential problems with null reference exceptions in finally blocks; these have been cleaned up.