1

I try to develop a storing image file dapp with Parity these days. And I asked a question about private key some days ago. And Someone said that each account has a private key in Ethereum so that I can use it to encrypt.

But today I try to find the API to encrypt with private key. But I just found encrypt with public key, like this web says:parity_encryptMessage

So how to use the private key of Ethereum? Are there any APIs or something else that I can use to encrypt message? Thanks a lot~

1
  • You never use private keys to encrypt, you always use the public key to encrypt and the private key to decrypt.
    – q9f
    Jun 19, 2017 at 8:09

1 Answer 1

2

You never use private keys to encrypt, you always use the public key to encrypt and the private key to decrypt. Assuming your are looking for the public key: To generate a new keypair you can use ethkey. For simplicity, I will generate a brain wallet from your nick, so you can reuse these params. Do not use this on public network.

$ ethkey generate brain YangYifei
secret:  c3d09aa314f216b618c84b3592d4d6992096ad544158a511148450771882c16e
public:  8b22ba82b80cb8d9e5e6207cbd8039bdb84398aaa63e5dd0684786456cd69a7708d334c92c540ed1d1764736b51fdcb5b7b95540cc8663851a03da99862e305e
address: 0052afd86e17e4cf7163a619a4cc9724dd04506c

To do the same with parity, and directly add the key to your node for later usage, you can use parity_newAccountFromPhrase:

 $ curl --data '{"method":"parity_newAccountFromPhrase","params":["YangYifei","password1337"],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545
{"jsonrpc":"2.0","result":"0x0052afd86e17e4cf7163a619a4cc9724dd04506c","id":1}

But note, that we also need the public key from ethkey above. Now let me encrypt a message for you, it requires the message in hex (use an ascii-to-hex converter) and the 64 bytes public key. Now feed this to parity_encryptMessage:

 $ curl --data '{"method":"parity_encryptMessage","params":["0x8b22ba82b80cb8d9e5e6207cbd8039bdb84398aaa63e5dd0684786456cd69a7708d334c92c540ed1d1764736b51fdcb5b7b95540cc8663851a03da99862e305e","0x48656c6c6f2c2059616e67596966656921"],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545
{"jsonrpc":"2.0","result":"0x04f0f2a86c0f172d1bc897c23af03d23e8a3d66b84eac9d201f942b11350b4e48e0abcc0dacff1fe75a6ee1b6caa2eb489126336146d38192e656a208c5f22575b94083909c157ce1e0b6182b11bc1aa845ee0f501c2c13134be2b143fe31dd1682ca0be95bdd33c0663c29b760a2ef31c3aa3d0309d79dcf55cae5ebfa50e74c706","id":1}

The result is your encrypted message. To decrypt it, you can do it with parity_decryptMessage, not this only works if you added this account to Parity before.

Since decrypting requires your private key, you either have to unlock your account with personal_unlockAccount or using the wallet's signer functionality.

3
  • Wow!! That helps me a lot!!! THANKS !!! Thank you your detailed answers! :)
    – YangYifei
    Jun 26, 2017 at 6:15
  • Hi. Today I try this method. It works successfully. But when I want to decrypt more than once at the same time, it just unlock some of them. I think it may because I call more than once decrypt at the same time. Can u give me some advice? Such as unlock forever. Thanks a lot~
    – YangYifei
    Jul 8, 2017 at 17:13
  • 1
    @5chdn - would you like to add that using the private key to encrypt creates a digital signature? Any other party can decrypt the signature with the corresponding public key of the signer's account. If the decrypted output matches the input that was encrypted, then that verifies that the signature was created by someone with access to the private key of that account. Dec 26, 2017 at 20:56

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.