This paper presents an approach to the synthesis of secure real-time applications mapped on distributed embedded systems, which focuses on preventing fault injection attacks. We utilize symmetric cryptographic service to protect confidentiality, and deploy fault detection within confidential algorithm to resist fault injection attacks. Several fault detection schemes are identified, and their fault coverage rates and time overheads are derived and measured, respectively. Our synthesis approach makes efforts to determine the best fault detection schemes for the encryption/decryption of messages, such that the overall security strength of resisting fault injection attack is minimized, and the deadline constraint of the real-time applications is guaranteed. Since addressing the problem is still a NP-hard problem, we propose an efficient algorithm based on Fruit fly Optimization Algorithm (FOA), which can achieve better results by lower time overheads, compared with simulated annealing algorithm. Extensive experiments demonstrate the superiority of our approach.