如何解决带有后端后端的React中的ECDH
我目前正在尝试在我的Node API和React UI之间生成一个共享机密。所以我有一个在Node上本地工作的实现。现在,我正在尝试将其翻译成React。我收到的错误如下:
Error [ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY]: Public key is not valid for specified curve at ECDH.dhComputeSecret [as computeSecret]
从该错误中,我了解到密钥要么错误生成,要么是事物“节点”侧computeSecret
的长度不正确。我可能错了,但这是我的直觉。有人遇到过这样的事情吗?不胜感激。
这就是我在Node端拥有的东西:
/**
* Derives a ECDH shared secret from public key and generates local keys
* @param publicKey from React UI as hex
* @returns {{publicKey: Buffer,sharedSecret: string}}
*/
function generateAndDeriveECDH(publicKey) {
//Set the type of the curve
let type='secp256k1';
//Create ECDH
let ecdh = crypto.createECDH(type);
//Generate keys
let localPublicKey = ecdh.generateKeys();
let privateKey = ecdh.getPrivateKey().toString('hex');
//Compute the shared secret
try {
let sharedSecret = ecdh.computeSecret(publicKey).toString('hex');
//Return the result
return { publicKey: localPublicKey,sharedSecret: sharedSecret };
} catch (err) {
console.log(err);
}
}
因此该代码已在Node本身中进行了测试,并在Node中生成了publicKey
,并且可以正常工作。因此,我不确定这是否是导致错误的运输过程。这是事物的反应方面:
componentDidMount() {
console.log('Starting crypto test');
//Set the type of the curve
let type='secp256k1';
//Create ECDH
let ecdh = crypto.createECDH(type);
//Generate keys
let localPublicKey = ecdh.generateKeys().toString('hex');
let privateKey = ecdh.getPrivateKey().toString('hex');
console.log('PRIVATE KEY:');
console.log(privateKey);
const requestOptions = {
method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({ publicKey: localPublicKey })
};
fetch('http://localhost:8080/api/test/ecdh',requestOptions)
.then((response) => {
console.log('RESPONSE:');
console.log(response);
})
.then((data) => {
console.log('DATA:');
console.log(data);
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。