Encrypted control is a framework for the secure outsourcing of controller computation using homomorphic encryption that allows to perform arithmetic operations on encrypted data without decryption. In a previous study, the security level of encrypted control systems was quantified based on the difficulty and computation time of system identification. This study investigates an optimal design of encrypted control systems when facing an attack attempting to estimate a system parameter by the least squares method from the perspective of the security level. This study proposes an optimal H2 controller that maximizes the difficulty of estimation and an equation to determine the minimum security parameter that guarantee the security of an encrypted control system as a solution to the design problem. The proposed controller and security parameter are beneficial for reducing the computation costs of an encrypted control system, while achieving the desired security level. Furthermore, the proposed design method enables the systematic design of encrypted control systems.