First make sure that you have installed the latest ESPHome add-on (currently 2022.5.0). If your add-on is older and not automatically updated you are still using the version from the Community repo. Check this video on how to change that: ESPHome is moving (deprecated from Community Addons) - here's how to get it now - YouTube
Don’t worry, your existing ESP Home config will be preserved.
When your version is up-to-date, edit the ESPHome YAML node that you want to encrypt. Add an encryption key to the API definition as described here: Native API Component — ESPHome
Compile and upload.
Go to the HA integrations page. You should see a warning now that the integration of your ESPHome device needs reconfiguration. Go through the reconfig and enter the encryption key and your API key.
If you see multiple reconfig warnings for the same device, you can shorten the process by updating only one of them and then restarting HA.