Terms:
Client: Browser, mobile app or desktop application written using using JavaScript, HTML, CSS and packaged with Capacitor / Tauri.
Server: NodeJS based API.
Data: Username / password
Vendor: Third party integration
Background:
I am writing an integration with various vendors that requires users to enter their vendors data for our automated processes to log in and perform their task. For the benefit of the customers mindset on security, we're not storing the data on the server (encrypted or otherwise). The processing is done client side and in turn, the server doesn't need to have access to the data.
Problem:
I want to encrypt this data in such a way that it can only be decrypted when the user is logged in and a connection has been made to the server.
My solutions:
Idea 1: Key stored on server
- User adds a vendor integration
- The server generates a "key" specific to that vendor, saves it in the DB for that integration and returns it to the client
- Client encrypts the data and stores it.
- When a sync needs to take place (which would only happen when the user is logged into my platform) we request the vendor key and decrypt the data and use it in memory only.
Idea 2: Asymmetric Encryption
- User adds a vendor integration
- Server generates a public / private key, stores the private key against the users vendor integration in the DB and returns the public key to the client. The public key would be available to the user whenever they're logged in.
- Client then encrypts the data using the public key
- When the sync needs to take place, we send the encrypted data to the server and the server returns a decrypted version for us to use in memory only.
Question:
From my understanding, Idea 1 seems safe and preferable to me because the users data never leaves the device, even to be decrypted. Idea 2 seems safe but I can't think of any advantages over using the stored key approach in idea 1.
Is my understanding correct or should I be using some other method in order to safely encrypt this data?