The paper discusses security issues in wireless sensor networks (WSN) and presents a novel n-tier modeling approach for key management to ensure secure data aggregation. The proposed system is lightweight, optimizing computational efficiency while enhancing security and data performance against various attacks. It emphasizes the need for robust key generation to foster integration with the Internet of Things (IoT) by utilizing unique static and dynamic characteristics of sensor nodes.