So I am using the module (eth-ecies) in npm to perform encryption of a message offchain using the public key of my reciever, and in turn I get a 113+ byte buffer after the encryption is done. Now i want to store this encrypted message in my smart contract, but I can't figure out which dataType I should use to do the storage. I tried using bytes and string but I get errors. This is what happens when I use string:

error when I use string datatype

And this is what happens when I use bytes:

enter image description here

The encrypted data is of the form "UjPuhBwP5fPDtuQrV3Oq6ASZwfQj8sO/jHwOxqj8BXEWvitUYjjhkNWckWQxxtdX9KrFbIYKoiFYb2dZq+QYjk6m+XsmTHRfhrS9AtLWmnyFJtj34pvMTuDTT9ur6ssxIwJSMJkShb1nmk7cfKQOr61+Zxh0UHlRmyFTrEz1fS9nnwDTMcpoPN5vSj7M6hvPKQ==" and is encoded in base64.

I use this code for encryption:

function encrypt(publicKey, data) {
let userPublicKey = new Buffer(publicKey, 'hex');
let bufferData = new Buffer(data);

let encryptedData = ecies.encrypt(userPublicKey, bufferData);

return encryptedData.toString('base64');


And this code to send the encrypted data to blockchain:

const sendDetails = contractInstance.methods.sendEncryptedDetails(enc).encodeABI();

const sendDetailsTx = {

const receipt5 = await web3.eth.sendSignedTransaction(sendDetailsTx);

This might be a very silly issue but I can't figure it out. Thanks in advance!

  • Why not just return encryptedData;? – rustyx Jul 16 '18 at 19:31
  • When I use return encryptedData, it returns a buffer consisting of various numbers and I get this error : T6@�R��^��6V2" at SolidityTypeDynamicBytes.formatInputDynamicBytes [as _inputFormatter] (C:\Users\Akash\WebstormProjects\untitled\node_modules\web3-eth-abi\src\formatters.js:83:15) – akash m verma Jul 16 '18 at 19:40
  • You are trying to send a signed transaction but it is not signed! Take a look at the documentation how to sign a transaction web3js.readthedocs.io/en/1.0/web3-eth.html#id67. Also I'd recommend to use bytes and send data as hexadecimals prefixed with '0x'. – Ismael Jul 17 '18 at 5:12

Your Answer

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

Browse other questions tagged or ask your own question.