This paper presents security and privacy algorithms for cyber-physical systems (CPS) integrated with wireless sensor networks (WSN), emphasizing the need for data protection against cyber-attacks. It proposes using Transport Layer Security (TLS) with the MQTT protocol for secure data exchange and a method to detect and prevent Denial of Service (DoS) attacks on web servers. Experimental results demonstrate the effectiveness of the proposed approach in maintaining security and integrity within CPS environments.