Unencrypted private keys could remain in the wallet.dat file (and other berkely db database files) after wallet encryption.
This pull request does several things to fix the problem:
Completely rewrites the wallet.dat file upon encryption.
Removes all keys from the keypool so they will not be used for new transactions (they are encrypted and stored in the new wallet, in case any bitcoins are sent to them).
Modifies the database code so temporary database files are cleaned up when bitcoin shuts down
Successful encryption of the wallet is now followed by a shutdown, so old unencrypted private keys that might have been in the database's caches will not be written to the new, encrypted wallet file.