Wireless Sensor Network (WSN) is developed as a promising technology for a wide range of applications due to its low installation cost. However, the nodes in the WSN are susceptible to different security threats, because these nodes are located in hostile or harsh environments. The important goal of this paper is to obtain secure data transmission while minimizing energy consumption. In this paper, an Improved Moth Flame Optimization (IMFO) is used for selecting the optimal Cluster Head (CH) from the network. Moreover, the secure routing path is generated using the IMFO where the fitness function considers residual energy, distance, trust and node degree. Therefore, the Secure Clustering and Routing using IMFO (SCRIMFO) provides the resistance against the Distributed Denial-of-Service (DDoS) attack whereas the energy consumption is minimized by identifying the shortest path. The proposed SCRIMFO method is analysed in terms of Average Energy Consumption (AEC), Packet Loss Ratio (PLR), Packet Delivery Ratio (PDR) and routing load. Here, the SCRIMFO method is compared with the Secure Routing Protocol based on Multi-objective Ant-colony-optimization (SRPMA), Ant Colony Conveyance Algorithm (ACCA) and Energy-aware Trust and Opportunity-based Routing (ETOR) approaches. The PDR of the SCRIMFO is 94.64 % for 2 DDoS attacks that is high when compared to the SRPMA, ACCA and ETOR.