Sorry if this doesn't fit the guidelines, but I believe it is a conceptual issue that is not opinion-based and can be reasonably answered in a single authoritative answer (I'm also struggling to find anywhere else to ask this!).
I'm creating a system that needs to send data securely between a public website and a database, and store some of that data securely in the database. The system will use PHP (either 5.6 or 7.2) and I plan to use Sodium for the encryption (either included in 7.2 or manually included with 5.6). I plan to use libsodium.js on the website side. I'm fairly new to encryption, but I would like to check that the process I am designing - rather than the actual code at this stage - will allow for the system to be theoretically secure.
- Customers will set up an account on the website and log in to use the service.
- The server will create a public/private key pair for each user on registration (stored in a file outside of the root directory). These will change very infrequently (and only via a controlled process to ensure data is still decryptable, if necessary).
- When a customer logs in, their "server" public key will be stored in their session, and libsodium.js will be used to create a public/private key pair on the website side (a new pair will be generated each time someone logs in).
- The "website" public key will be sent to the server via Ajax and stored in the session.
- The server will receive the data, decrypt it, and then process the individual fields.
- Fields that need to be searchable/joinable in the database will be stored in an unencrypted way. Other fields will be encrypted using the server private key and a nonce, using crypto_secretbox. The data and the nonce will then be stored in the database.
- When encrypted data needs to be retrieved from the database, it will be decrypted using crypto_secretbox and then rencrypted using the server private key and website public key, before being made available to the website, where it can be decrypted using libsodium.js.
Actual coding aside, have I missed anything or got anything wrong with this proposed process?