Security is a critical problem in implementing mobile ad hoc networks (MANETs) because of their vulnerability to routing attacks. Although providing authentication to packets at each stage can reduce the risk, routing attacks may still occur due to the delay in time of reporting and analyzing the packets. Therefore, this authentication process must be further investigated to develop efficient security techniques. This paper proposes a solution for detecting black hole attacks on MANET by using harmony search algorithm (DBHSA), which uses harmony search algorithm (HSA) to mitigate the lateness problem caused by cooperative bait detection scheme (CBDS). Data are simulated and analyzed using MATLAB. The simulation results of HSA, DSR, and CBDS-DSR are provided. This study also evaluates the manner through which HSA can reduce the inherent delay of CBDS. The proposed approach detects and prevents malicious nodes, such as black hole attacks that are launched in MANETs. The results further confirm that the HSA performs better than CBDS and DSR.