Due to their working environments, limited resources and communication characteristics, wireless sensor networks face some challenges including energy optimization and security enhancement to extend the network lifetime and guarantee the network security. Therefore, an energy-aware and trust-based routing protocol for wireless sensor networks using adaptive genetic algorithm called TAGA is proposed to not only resist common routing attacks and special trust attacks, but also minimize the energy consumption caused by data transmission. To this end, TAGA constructs the nodes' comprehensive trust values based on their direct trust values considering the volatilization and adaptive penalty factors, and indirect trust values with the filtering mechanisms. In addition, a novel threshold function is presented to select the optimal cluster heads, which considers the dynamic changes of the nodes' comprehensive trust values and residual energy. Finally, a genetic algorithm with adaptive crossover probability and mutation probability is applied to find the optimal secure routing for the cluster heads. The simulation results show that TAGA can reduce the number of packets discarded by malicious nodes when facing common attacks and special trust attacks, and effectively improve the energy efficiency compared to the relative secure routing protocols EOSR and IASR.INDEX TERMS Wireless sensor networks, secure routing, comprehensive trust, direct trust.