I have a client/server application to encrypt/decrypt data. Client node encrypts a JSON data using ECIES from this library JavaScript Elliptic curve cryptography library. Then, node sends it to the server side using MQTT protocol. Server node receives this encrypted data, and decrypts it. However, the server node can not decrypt the data. I think there is problem with format of input data to decryption process.
Encryption (Client side):
var eccrypto = require("eccrypto");
var mqtt = require('mqtt')
var Broker_URL = 'mqtt://localhost';
var client = mqtt.connect(Broker_URL);
client.on('connect', function () {
console.log("MQTT connected "+ client.connected);
})
var privateKeyB = Buffer.from('efae5b8156d785913e244c39ca5b9bee1a46875d123d2f49bbeb0a91474118cf', 'hex');
var publicKeyB = eccrypto.getPublic(privateKeyB);
var msg = '[{"parent":"0","child":"1"},{"parent":"0","child":"2"}]'
eccrypto.encrypt(publicKeyB, Buffer.from(msg)).then(function(encrypted) {
let s = JSON.stringify(encrypted);
let b = Buffer.from(s, 'utf-8');
client.publish("BCencryptedFog", b);
});
Decryption (Server side):
var eccrypto = require("eccrypto");
var mqtt = require('mqtt')
var Broker_URL = 'mqtt://localhost';
var client = mqtt.connect(Broker_URL);
client.on('connect', function () {
console.log("MQTT connected "+ client.connected);
client.subscribe('BCencryptedFog');
})
client.on('message', function (topic, message) {
var privateKeyB = Buffer.from('efae5b8156d785913e244c39ca5b9bee1a46875d123d2f49bbeb0a91474118cf', 'hex');
let s = message.toString('utf-8');
let o = JSON.parse(s);
eccrypto.decrypt(privateKeyB, o).then(function(plaintext) {
console.log("Message to part B:", plaintext.toString());
});
})
Output on server side:
Error: Bad input
The output encrypted
in the encryption part:
{
iv: <Buffer 72 59 ca a1 3c 76 d3 58 d1 ae ed 78 6f 1f f3 3f>,
ephemPublicKey: <Buffer 04 e3 59 3e cd 41 3e e2 1a c6 df fe 75 37 cd 73 37 b4 ce 55 ad 71 bf 58 95 d3 41 6d 70 f4 74 55 12 47 a3 f9 15 6c d4 8b ad 6b d5 1e a6 6a 2b 04 60 3d ... 15 more bytes>,
ciphertext: <Buffer 18 94 c3 83 c0 0b db 31 1e 17 42 ef cd 80 61 a2 bb cb 83 56 de 11 e1 b7 c2 4a 84 db e0 cd 5a 22 71 32 2f 47 a6 8d ff 7c c9 49 d5 ad 93 68 0e 74 dc 71 ... 14 more bytes>,
mac: <Buffer 07 87 c9 9a ce 6c f6 65 14 6a fb fb 84 9a d7 40 01 d6 7a 6a e6 0f ef de 07 71 20 a3 21 3e 21 b5>
}
The input o
in the decryption part:
{
iv: {
type: 'Buffer',
data: [
114, 89, 202, 161, 60,
118, 211, 88, 209, 174,
237, 120, 111, 31, 243,
63
]
},
ephemPublicKey: {
type: 'Buffer',
data: [
4, 227, 89, 62, 205, 65, 62, 226, 26, 198, 223,
254, 117, 55, 205, 115, 55, 180, 206, 85, 173, 113,
191, 88, 149, 211, 65, 109, 112, 244, 116, 85, 18,
71, 163, 249, 21, 108, 212, 139, 173, 107, 213, 30,
166, 106, 43, 4, 96, 61, 115, 215, 30, 13, 220,
9, 231, 51, 237, 123, 51, 129, 103, 75, 244
]
},
ciphertext: {
type: 'Buffer',
data: [
24, 148, 195, 131, 192, 11, 219, 49, 30, 23, 66,
239, 205, 128, 97, 162, 187, 203, 131, 86, 222, 17,
225, 183, 194, 74, 132, 219, 224, 205, 90, 34, 113,
50, 47, 71, 166, 141, 255, 124, 201, 73, 213, 173,
147, 104, 14, 116, 220, 113, 49, 47, 11, 108, 159,
218, 234, 5, 220, 230, 253, 202, 238, 95
]
},
mac: {
type: 'Buffer',
data: [
7, 135, 201, 154, 206, 108, 246, 101,
20, 106, 251, 251, 132, 154, 215, 64,
1, 214, 122, 106, 230, 15, 239, 222,
7, 113, 32, 163, 33, 62, 33, 181
]
}
}
How could I solve this, please? Thanks.