Encryption – How to Encrypt Data Using a Public Key

encryptionhashprivate

I have three data fields in a form which will return a string. I would like to hash the value of these three strings and store it in the blockchain. However the data in these three strings is sensitive and I only want the person who has access to the private key of the public key it was encrypted with to see the data. Is there a possible way to do it. At the moment I am using solidity's sha256() function to hash and store it in a mapping but like that anyone can decrypt it and get the data that I stored.

Best Answer

I only want the person who has access to the private key of the public key it was encrypted with to see the data. Is there a possible way to do it

Yes, you need to encrypt it before you send the data to the blockchain. Everything in the blockchain, including transactions, is public. So if you send the data in plaintext, anyone can see it.

At the moment I am using solidity's sha256() function to hash and store it in a mapping but like that anyone can decrypt it and get the data that I stored.

SHA256 is for hashing, not encryption. No one can "decrypt" something from a hash. (But as mentioned above, if you're hashing in the smart contract, then you've already shared the plaintext, so no one needs to "decrypt" anything.)