椭圆曲线密码解密输入格式错误如何解决?

问题描述

我有一个客户端/服务器应用程序来加密/解密数据。客户端节点使用来自此库 JavaScript Elliptic curve cryptography library 的 ECIES 加密 JSON 数据。然后,节点使用 MQTT 协议将其发送到服务器端。服务器节点接收此加密数据,并对其进行解密。但是,服务器节点无法解密数据。我认为解密过程的输入数据格式有问题。

加密(客户端):

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);
});

解密(服务器端):

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());
    });
})

服务器端输出

Error: Bad input

加密部分的输出encrypted

{
  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>
}

解密部分的输入o

{
  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,62,205,65,226,26,198,223,254,117,55,115,180,206,85,173,113,191,149,109,112,244,116,18,71,163,249,21,108,212,139,107,213,30,166,106,43,4,96,61,215,13,220,9,231,51,123,129,103,75,244
    ]
  },ciphertext: {
    type: 'Buffer',data: [
       24,148,195,131,192,11,219,49,23,66,239,128,97,162,187,203,86,222,17,225,183,194,74,132,224,90,34,50,47,141,255,124,201,73,147,104,14,159,218,234,5,230,253,238,95
    ]
  },mac: {
    type: 'Buffer',data: [
       7,135,154,246,101,20,251,64,1,214,122,15,7,32,33,181
    ]
  }
}

请问我怎么解决这个问题? 谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)