Node.js 加密货币使用 tron 公共(public)地址创建 ecdh

标签 node.js cryptojs ecdh

我的目标是使用 TronWeb.createAccount() 进行钱包地址加密。 ,我以 base58 形式获取钱包的公共(public)地址,并以十六进制形式获取私钥。

Sample Public Address: TPeGpPdJGQoNobV4SEjXLdrjefN3iCAAAA
Sample Private Key: 6B07B82D50B27171F35BF1DEAB14...

我使用以下代码获取 key 。

const TronWeb = require('tronweb');
function createAccount() {
    try {
        const tronWeb = new TronWeb(fullNode, solidityNode, eventServer);
        return tronWeb.createAccount();

    } catch (error) {
        console.log(error);
        throw error;
    }
}

当我在 bob.createECDH() 中设置私钥后使用 getPublicKey() 方法时,代码工作正常,但实际上,当我在 bob 一侧时,我不会为 alice 使用 setPrivateKey() 方法。因此,我必须传递 base58 公共(public)地址,而不是两侧的 bob.getPublicKey() 或 alice.getPublicKey()。

const alice_secret = alice.computeSecret('HEX_PUBLIC_KEY','hex');

以下是加密和解密的完整代码。

const alice = crypto.createECDH('secp256k1');
const bob = crypto.createECDH('secp256k1');
bob.setPrivateKey("PRIVATE_KEY_FOR_BOB", "hex");
alice.setPrivateKey("PRIVATE_KEY_FOR_ALICE", "hex");

const alice_secret = alice.computeSecret(bob.getPublicKey());
console.log("alice's shared Key: " + alice_secret.toString('hex') + "\n");

var algo = 'aes-256-ecb', plainText = "Some secret to share bob";
var cipher = crypto.createCipher(algo, alice_secret)
var encrypted = cipher.update(plainText, 'utf8', 'hex')
encrypted += cipher.final('hex');
console.log("Encrypted: " + encrypted);

const bob_secret = bob.computeSecret(alice.getPublicKey());
console.log("bob's shared Key: " + bob_secret.toString('hex') + "\n");

var decipher = crypto.createDecipher(algo, bob_secret)
var decrypted = decipher.update(encrypted, 'hex', 'utf8')
decrypted += decipher.final('utf8');
console.log("Decrypted: " + decrypted);

if (plainText == decrypted) {
    console.log("ECDH Success")
}

当我使用 setPrivateKey() 然后使用 getPublicKey() 时,输出是预期的

alice's shared Key: 238c3eba08585a5cae1006710c79fe2de329545e9ca4c1ef719c53b55eb337b6
app.js:21 Encrypted: 44184052d9e205fd855aaf5f30b5f186c4bab88a5cfdce58d99cd8c696954c8dd5676807e6fe372fbe3ca5b230e54293
app.js:29 bob's shared Key: 238c3eba08585a5cae1006710c79fe2de329545e9ca4c1ef719c53b55eb337b6
app.js:35 Decrypted: QmdUuJDvgZ7EWEpJmEcFCoYwotn9CHyvK4qEhZs82AhZoQ
app.js:40 ECDH Success

当我使用 bs58 或任何其他包将公钥转换为十六进制时,它会显示

UnhandledPromiseRejectionWarning: Error: Failed to translate Buffer to a EC_POINT

有没有办法转换这个公共(public)地址并在这种情况下使用它?

最佳答案

我必须研究 ECDH 支持的 key 格式并根据新格式重新生成 key 才能解决此问题。 我们可以使用两种格式的公钥来加密数据。

关于Node.js 加密货币使用 tron 公共(public)地址创建 ecdh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57140623/

相关文章:

java - 使用现有的外部公钥从 ECDH 导出共享 secret

security - 为什么P-521公钥X,Y有时是65字节有时是66字节

java - 使用 jose4j ECDH + JWE 加密/解密

node.js - 与 python twisted 相比,node.js 的实际用途是什么

javascript - Meteor MongoDB 集合无法访问 Meteor 发布

javascript - 如何在使用JAVA加密的nodejs中解密

javascript - 为什么digest和digest ('hex')会产生不同的输出?

node.js - VSCode : Prettier does not work with Dart Flutter

node.js - Mongodb Shell命令执行通过nodejs删除集合