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~
Best Answer
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.
To do the same with parity, and directly add the key to your node for later usage, you can use parity_newAccountFromPhrase:
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: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.