0

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

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

  • 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 '18 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 '18 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 '18 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 '18 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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