Key management system maintains the confident of secret information from unauthorized users and verifying the integrity of exchanged messages and authenticity. But recent advances in electronics and computer technologies create the complexity of key management in wireless sensor networks (WSN). Additionally, the traditional key management systems are not up to the mark due to limited resources like memory, and energy constraints.In this paper, we propose an optimal cluster based key management system (OC-KMS) for WSNs. The proposed system consist of two contributions, in first, we perform the energy efficient clustering using modified animal Diaspora (MAD)optimization algorithm and cluster head (CH) selection using JAYA trust model. In second contribution, we propose the certificate less signcryption algorithm, which generates and distributes the public and private keys for each node in sensor networks. The proposed system resists various network layer attacks without affecting the network performance. The simulation resultdescribes that the proposed system perform very efficient than existing in terms of both performance and security wise.