Maybe you are referring to encrypting data rather than encoding data.
There is no secure way to do this from a smart contract, since there is nothing actually private in a smart contract or blockchain, and if you send the data to the smart contract, it's visible to miners and attackers.
Also, you would need something the user's public key to be able to encrypt data that only the user can decrypt. But for decrypting data from the smart contract itself you would need a private key, the user's private key, which is not a good idea to have it in a smart contract, not even for a second, since miners and attackers can see it when you send the transaction.
I recommend that if you really need a functionality like this, to do it off-chain, in a secure server, then you save the encrypted data in the smart contract in a mapping
that holds the msg.sender
address as the key and the encrypted data as the value and return that data when msg.sender
requests it and let the user decrypt it off-chain. You could also provide an off-chain tool to help the user decrypt the data.
Anyways, it doesn't seem that the data you are trying to save is too sensitive. Analyze if it's worth the effort.