Please correct me if I'm wrong. I'm just curious as to how much smart contracts can do. I'm not too familiar with public key cryptography, but my current understanding is that it is possible for A to send encrypted messages to B using B's public key. (Whether this data be stored within a transaction or a smart contract.) If another individual C wants to pay B for this message, is it possible to write a smart contract which unlocks the same encrypted message but this time encrypted with C's public key upon C sending a transaction to B, without any manipulation by B? Or is it only possible to confirm that the message has not been manipulated by B if A is also privy to this smart contract?

2 Answers 2


I think that there are a few key concepts that you need to understand to fully answer your question.

is it possible to write a smart contract which unlocks the same encrypted message

In theory, but it's likely that the gas cost would be too high and it would probably require the private key being stored in the Smart Contract / publicly known - I suggest you read this answer, to a similar question, for a more in-depth explanation.

unlocks the same encrypted message but this time encrypted with C's public key upon C sending a transaction to B

One problem with this idea is that if the Smart Contract were able to decrypt the data that every node running the code to do so would know contents of the message - as the first step would be to decrypt the message and then re-encrypt it; and the secret message would temporarily be in the clear during this process.

without any manipulation by B

What you really want to know is if you can prove that message has not been modified by B. Along with Public Key encryption, I also suggest that you read up on Digital Signatures. Essentially, the hash of the plaintext that C decrypts should be the same as the hash of the plaintext that A encrypted - Digital Signatures are a cryptographic way to ensure this in a trustless manner.

I hope that helps clear things up a bit for you.

  • Thanks for the help to you both. I wasn't fully aware of hashing and its use in digital signatures until now, but now it's much clearer to me how third parties can verify private messages.
    – Riley
    Feb 6, 2018 at 13:18
(A) => MSG => {ENCRYPT w/ B's Public Key} => {MSG} => (B) 
(B) => {MSG} => {UNENCRYPT w/ private key} => MSG

For (C) to be able to get this {MSG}, it cannot be encrypted with B's public key.

(B) would have to decrypt the {MSG}. Then, encrypt using (C)'s public key.

Now (C) can receive the message, decrypt, and read what it says.

This, I believe, you already understand.

But this process, of decrypting & encrypting, needs to happen off chain as it requires a private key and plain text to be sent via a transaction. You DO NOT do this!

To decrypt using a smart contract would require one to send out their private key to every miner on the blockchain! 😱 The same is true for encrypting, the plain text would be sent to every miner!

See this topic: Cryptography in a smart contract

So, encrypt and decrypt off chain. The payments can still be facilitated via smart contracts. Throw events to trigger the process to encrypt/decrypt off chain and send a new transaction to deliver the encrypted message to a new SC.

The SCs at this point would handle payments and already encrypted messages.

And as mentioned by SteveJaxon, to verify that (B) did not alter the message, you'd provide a hash of the original message that came from (A).

Your Answer

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

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