2

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.

2 Answers 2

3

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.)

4
  • So If i encrypt the data using the receipient's public key and store this encrypted data in the blockchain( via a solidity function call). Then how can I go about retrieving this encrypted data from solidity and decrypting it using the private key of the recipient (meta mask). Thanks for your help
    – David021
    Oct 25, 2018 at 15:44
  • I'm not sure which part you're asking about. On the Ethereum side, there's nothing to it... the contract stores the bytes, and then the recipient retrieves them. For the offline code that does encryption and decryption, you can use whatever cryptography you want for that.
    – user19510
    Oct 25, 2018 at 15:46
  • So basically what my app will be doing is storing medical certificate's data in the blockchain. The doctor will be logged in with his account, fill in the fields and also choose who can view the certificate by inputting their public key. Upon clicking submit, the data fields will be encrypted using the public key provided and the encrypted message will be stored in ethereum. The encrypted message will also be sent via email to the employer. So the employer will then go on the web platform enter the encrypted message, and by being logged in to meta mask they will be able to view the details.
    – David021
    Oct 25, 2018 at 15:53
  • It sounds like you want to reuse people's Ethereum key pairs for encryption. This is possible, but it's not particularly easy... in normal Ethereum use, the user's public key is not revealed (except by recovering it from a transaction they've signed). MetaMask doesn't expose methods for encryption or decryption, nor does it provide an easy way to get a user's private key to do the decryption yourself.
    – user19510
    Oct 25, 2018 at 15:56
1

I have done this multpiple times and also created a tutorial, see here to do it in javascript: https://github.com/pubkey/eth-crypto/blob/master/tutorials/encrypted-message.md

6
  • Looks good but can we actually access the private key from the connected user inside the Dapp for the decryption part ? Sounds like a problem.
    – gneric
    Jan 24, 2022 at 20:30
  • @gneric very good question... from a dapp you don't have access to the private key (for a good reason) so how could this work? Maybe you can call some functions with web3 to metask to decrypt the message?
    – Suisse
    Feb 22, 2022 at 16:58
  • @Suisse Yes, actually I was researching at that point in time. Exactly metamask's personal_sign , signs the message, with the keys that holds.
    – gneric
    Feb 23, 2022 at 15:37
  • @gneric so it is possible that a user1 can encrypt a message "hi" -> so that only another specific user2 (and the user1) can decrypt the message?
    – Suisse
    Feb 23, 2022 at 18:19
  • no, I don't think so, user2 won't have the decryption key (user1's pk) to recover the message.
    – gneric
    Feb 24, 2022 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.