Defense Against Integrity and Privacy Attacks in the Internet of Things