Integrating physical, computational, and networking resources are the goal of cyber‐physical systems, also known as smart‐embedded systems. By investing in a solid foundation, we can improve the usefulness and timeliness of the services we rely on in every facet of our lives and ultimately live more elegantly. Regarding modern technology, data security is a significant factor that must be considered. The complexity of cyber‐physical systems' interacting components and middleware presents serious hurdles when it comes to protecting them from cyber‐attacks without negatively impacting their performance. This article proposes a unique, efficient encryption technique for anticipating cyber assaults in cyber‐physical systems, which addresses these concerns. The suggested method uses Bayesian optimization techniques to fine‐tune the LightGBM algorithm's hyper‐parameters. This proposed algorithm has been implemented on the intrusion detection dataset (UNR‐IDD) from the University of Nevada. Reno has been used to test the suggested approach. The proposed system achieved 99.17% accuracy, 0.9918 precision, and 0.9922 recall values. Our empirical evaluation demonstrates that the algorithm successfully increases accuracy and AUC value, making the cyber‐physical system more secure. In turn, the suggested methodology may offer robust assurance for user data safety.