node-rsa:编码太长,使用以太坊帐户的 public 密钥
node-rsa: Encoding too long, using the public key of an ethereum account
我有以下问题:
主要目标是使用以太坊账户的 public 密钥(形成密钥库文件)对一些有效负载进行非对称加密,然后使用相应的私钥(也来自同一密钥库)对其进行解密-文件).
我已经在这个包的帮助下从账户地址中提取了私钥:keythereum-node
这导致来自特定帐户地址的以下十六进制格式的私钥:
私钥:6dc5aeb2cf14c748da683d1c16491d5b468b5bb1eea3f98c511b6371fdcfb05f
我设法从帐户中获取了 public 密钥,方法是首先使用私钥对随机选择的字符串进行签名,然后使用签名接收 public 密钥(msgHash,v,r ,s).
为此,我使用了 ethereumjs-util 包中的 ecrecover-method。
这会产生以下十六进制格式的 publicKey:
public键:9F9F445051E7888461952124DC08647035C0B31D51F6B46B4653465723485723F04C9837ADB27ADB27ADB275D4173125C1125CC1125CC11125CC1125CC11114EAMEAYEAKE1125CEC1114EAE115CEC125CERE
到目前为止一切顺利。现在我有两个密钥( public-key 512bit 和 private-key 256bit long)。
现在,我正在尝试通过以 pem 格式传递 base64 编码的 public-key 来生成一个 nodeRsa 密钥(node-Rsa 包)。
获取私钥:
var keyObject = await keythereum.importFromFile(address, keystorePath).then(keyObject => {
return keyObject;
}).catch(err => {
console.log(err);
});
var privateKey = await keythereum.recover(password, keyObject).then(privateKey => {
return privateKey;
}).catch(err => {
console.log(err);
});
return privateKey;
获取公钥:
var sig = await web3.eth.accounts.sign("Hello World", privateKey);
var msgHash = sig.messageHash;
var r = sig.r;
var s = sig.s;
var v = sig.v;
msg = recover.toBuffer(msgHash);
var publicKey = recover.ecrecover(msg,v,r,s);
正在用 rsa-node 生成 rsa-key:
var key = new nodeRsa("-----BEGIN RSA PUBLIC KEY-----\n" +
"n59EUFHniEYZUhJNwIZHA1wLMdUfa0ZTSFcj8EyYN62yddQXMTCfYSXBTqFUbYai\n" +
"cVjuxBZMALq0ck7tkl6cYA==\n" +
"-----END RSA PUBLIC KEY-----", "pkcs1-public");
执行脚本后抛出以下错误:
{ InvalidAsn1Error: encoding too long
at newInvalidAsn1Error (/Users/milandavidovic/node_modules/asn1/lib/ber/errors.js:7:13)
at Reader.readLength (/Users/milandavidovic/node_modules/asn1/lib/ber/reader.js:103:13)
at Reader.readSequence (/Users/milandavidovic/node_modules/asn1/lib/ber/reader.js:136:16)
at Object.publicImport (/Users/milandavidovic/node_modules/node-rsa/src/formats/pkcs1.js:122:14)
at Object.detectAndImport (/Users/milandavidovic/node_modules/node-rsa/src/formats/formats.js:65:48)
at NodeRSA.module.exports.NodeRSA.importKey (/Users/milandavidovic/node_modules/node-rsa/src/NodeRSA.js:183:22)
at new NodeRSA (/Users/milandavidovic/node_modules/node-rsa/src/NodeRSA.js:73:18)
at getPublicKeyFromSignature (/Users/milandavidovic/masterthesis/Scripts/DeployContract.js:280:15)
at <anonymous> name: 'InvalidAsn1Error', message: 'encoding too long' }
(node:1101) UnhandledPromiseRejectionWarning: InvalidAsn1Error: encoding too long
at newInvalidAsn1Error (/Users/milandavidovic/node_modules/asn1/lib/ber/errors.js:7:13)
at Reader.readLength (/Users/milandavidovic/node_modules/asn1/lib/ber/reader.js:103:13)
at Reader.readSequence (/Users/milandavidovic/node_modules/asn1/lib/ber/reader.js:136:16)
at Object.publicImport (/Users/milandavidovic/node_modules/node-rsa/src/formats/pkcs1.js:122:14)
at Object.detectAndImport (/Users/milandavidovic/node_modules/node-rsa/src/formats/formats.js:65:48)
at NodeRSA.module.exports.NodeRSA.importKey (/Users/milandavidovic/node_modules/node-rsa/src/NodeRSA.js:183:22)
at new NodeRSA (/Users/milandavidovic/node_modules/node-rsa/src/NodeRSA.js:73:18)
at getPublicKeyFromSignature (/Users/milandavidovic/masterthesis/Scripts/DeployContract.js:280:15)
at <anonymous>
(node:1101) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1101) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我不明白,为什么编码要太长。我多次检查了字符串。
如果有人有线索,请随时写下答案或建议。
经过进一步研究,我发现,PEM 格式的密钥不仅包含带有页眉和页脚的 base64 编码原始密钥。
在我的例子中,我必须提供一个包含以下数据的 DER 结构:
RSAPublicKey ::= SEQUENCE {
modulus INTEGER, -- n
publicExponent INTEGER -- e
}
所以我上面的 "pem-formatted" 键不正确。
我的密钥也不是 RSA 密钥,而是 secp256k1-Key
我有以下问题:
主要目标是使用以太坊账户的 public 密钥(形成密钥库文件)对一些有效负载进行非对称加密,然后使用相应的私钥(也来自同一密钥库)对其进行解密-文件).
我已经在这个包的帮助下从账户地址中提取了私钥:keythereum-node
这导致来自特定帐户地址的以下十六进制格式的私钥: 私钥:6dc5aeb2cf14c748da683d1c16491d5b468b5bb1eea3f98c511b6371fdcfb05f
我设法从帐户中获取了 public 密钥,方法是首先使用私钥对随机选择的字符串进行签名,然后使用签名接收 public 密钥(msgHash,v,r ,s). 为此,我使用了 ethereumjs-util 包中的 ecrecover-method。
这会产生以下十六进制格式的 publicKey:
public键:9F9F445051E7888461952124DC08647035C0B31D51F6B46B4653465723485723F04C9837ADB27ADB27ADB275D4173125C1125CC1125CC11125CC1125CC11114EAMEAYEAKE1125CEC1114EAE115CEC125CERE
到目前为止一切顺利。现在我有两个密钥( public-key 512bit 和 private-key 256bit long)。
现在,我正在尝试通过以 pem 格式传递 base64 编码的 public-key 来生成一个 nodeRsa 密钥(node-Rsa 包)。 获取私钥: 获取公钥: 正在用 rsa-node 生成 rsa-key: 执行脚本后抛出以下错误: 我不明白,为什么编码要太长。我多次检查了字符串。 如果有人有线索,请随时写下答案或建议。var keyObject = await keythereum.importFromFile(address, keystorePath).then(keyObject => {
return keyObject;
}).catch(err => {
console.log(err);
});
var privateKey = await keythereum.recover(password, keyObject).then(privateKey => {
return privateKey;
}).catch(err => {
console.log(err);
});
return privateKey;
var sig = await web3.eth.accounts.sign("Hello World", privateKey);
var msgHash = sig.messageHash;
var r = sig.r;
var s = sig.s;
var v = sig.v;
msg = recover.toBuffer(msgHash);
var publicKey = recover.ecrecover(msg,v,r,s);
var key = new nodeRsa("-----BEGIN RSA PUBLIC KEY-----\n" +
"n59EUFHniEYZUhJNwIZHA1wLMdUfa0ZTSFcj8EyYN62yddQXMTCfYSXBTqFUbYai\n" +
"cVjuxBZMALq0ck7tkl6cYA==\n" +
"-----END RSA PUBLIC KEY-----", "pkcs1-public");
{ InvalidAsn1Error: encoding too long
at newInvalidAsn1Error (/Users/milandavidovic/node_modules/asn1/lib/ber/errors.js:7:13)
at Reader.readLength (/Users/milandavidovic/node_modules/asn1/lib/ber/reader.js:103:13)
at Reader.readSequence (/Users/milandavidovic/node_modules/asn1/lib/ber/reader.js:136:16)
at Object.publicImport (/Users/milandavidovic/node_modules/node-rsa/src/formats/pkcs1.js:122:14)
at Object.detectAndImport (/Users/milandavidovic/node_modules/node-rsa/src/formats/formats.js:65:48)
at NodeRSA.module.exports.NodeRSA.importKey (/Users/milandavidovic/node_modules/node-rsa/src/NodeRSA.js:183:22)
at new NodeRSA (/Users/milandavidovic/node_modules/node-rsa/src/NodeRSA.js:73:18)
at getPublicKeyFromSignature (/Users/milandavidovic/masterthesis/Scripts/DeployContract.js:280:15)
at <anonymous> name: 'InvalidAsn1Error', message: 'encoding too long' }
(node:1101) UnhandledPromiseRejectionWarning: InvalidAsn1Error: encoding too long
at newInvalidAsn1Error (/Users/milandavidovic/node_modules/asn1/lib/ber/errors.js:7:13)
at Reader.readLength (/Users/milandavidovic/node_modules/asn1/lib/ber/reader.js:103:13)
at Reader.readSequence (/Users/milandavidovic/node_modules/asn1/lib/ber/reader.js:136:16)
at Object.publicImport (/Users/milandavidovic/node_modules/node-rsa/src/formats/pkcs1.js:122:14)
at Object.detectAndImport (/Users/milandavidovic/node_modules/node-rsa/src/formats/formats.js:65:48)
at NodeRSA.module.exports.NodeRSA.importKey (/Users/milandavidovic/node_modules/node-rsa/src/NodeRSA.js:183:22)
at new NodeRSA (/Users/milandavidovic/node_modules/node-rsa/src/NodeRSA.js:73:18)
at getPublicKeyFromSignature (/Users/milandavidovic/masterthesis/Scripts/DeployContract.js:280:15)
at <anonymous>
(node:1101) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1101) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
经过进一步研究,我发现,PEM 格式的密钥不仅包含带有页眉和页脚的 base64 编码原始密钥。 在我的例子中,我必须提供一个包含以下数据的 DER 结构:
RSAPublicKey ::= SEQUENCE {
modulus INTEGER, -- n
publicExponent INTEGER -- e
}
所以我上面的 "pem-formatted" 键不正确。 我的密钥也不是 RSA 密钥,而是 secp256k1-Key