A key function of wireless sensor networks (WSN) is data collection. Due to the hot spot issue and the limited energy supply, developing data gathering techniques is complicated. The WSN faces three main challenges: security, data routing, and processing a lot of data. Since compressive sensing can achieve simultaneous sampling and compression, it is widely used in signal processing technique. Due to resource limitations and computational limitations, WSN security solutions are different from those in traditional networks. Compressive sensing (CS) and Elliptical curve Diffie-Hellman key exchange are used to solve these problems. The measurement matrix is configured to be as a public key that is understood by both the sensor node and the base station in order to achieve high safety and efficiency for data gathering in wireless sensor networks. Security and effective data collecting are the main study goals. A prime-numbered address strategy for TPID (tree path identifier) routing and cluster head selection is used. Comparison between seven types of CS algorithms is introduced over different data sparsity levels. The network parameters is being tested are Network life time, throughput, residual node energy and total energy dissipated. The results revels that the compression system can reduce the size of the transmitted data and consequently the energy consumption while still maintains the data security.