SummaryMobile ad hoc networks (MANETs) become most vibrant and are widely used in various applications, including military, commercial sectors, and personal area networks. However, security is a major issue in routing; because of moving nodes, mainly it suffers from a black hole and gray hole attack. This kind of attack is difficult to predict and is harmful to the network. To address this problem, here, we introduce the gray wolf optimization (GWO) with trust setup data aggregation called gray wolf trust accumulation in wireless ad hoc network architecture. To maintain and enhance the security in routing, here, we update the trust setup data aggregation in GWO mechanism. After the detection of black hole and gray hole attack, we need to maintain the path stability for data transmission, trust schematic process monitor all the nodes, and maintain path stability. Using the behavior of GWO, we can detect the black hole and gray hole attack at a rate of 98.5% and also improve the packet delivery ratio by 98.2% with 10 m/s. Also, with the help of the trust setup data aggregation model, we can update our trust model and security maintenance. Thus, our proposed technique enhances the packet transmission in MANET and secures the routing layer.