Pervasive computing applications have, in many cases, hard requirements in terms of security. In particular when deploying a Wireless Sensor Network (WSN), security and privacy exigences must be accommodated with the small computational power and especially with the limited energy of the nodes. In some applications nodes may be equipped with energy harvesting devices. especially solar cells, to keep their small batteries charged. The presence of an harvesting device, while enabling the use of WSN in more application fields, represents an additional challenge in the design phase. Given the stochastic nature of most energy harvesting sources, optimizing system performance requires the capability to evaluate the current system conditions runtime. In this chapter we present a runtime mechanism that optimizes network lifetime and quality of service by adapting network security provisions to the current situation in terms of available energy and recharging rate. By applying our algorithm, network lifetime when the harvester cannot collect energy can be prolonged. The algorithm tries to limit power consumption by suitably changing security settings and by lowering the number of unessential messages sent on the network.