【发布时间】:2019-09-07 18:21:06
【问题描述】:
我正在运行 NodeJS 8.12.0,并且必须在散列上设置签名,而不是重新散列,执行原始签名。或者,换句话说,用私钥加密散列值。
const crypto = require('crypto');
// 4096 bits key.
let pk = "<BASE64 DER>";
let pub = "<BASE64 DER>";
// Transform them to PEM.
pk = `-----BEGIN PRIVATE KEY-----\n${pk.replace('\n', '')}\n-----END PRIVATE KEY-----\n`;
pub = `-----BEGIN PUBLIC KEY-----\n${pub.replace('\n', '')}\n-----END PUBLIC KEY-----\n`;
// Load the data to sign and set the signature.
const fingerprint = Buffer.from('2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824','hex');
const signature = crypto.privateEncrypt({
key: pk,
padding: crypto.constants.RSA_PKCS1_PADDING
},
fingerprint
);
// Unfortunately, the server is not able to verify the signature...
console.log(signature.toString('hex'));
所以我用我的私钥查看了消息哈希的原始加密,最终得到了某种EMSA encoding and followed these steps:
- 对消息应用哈希函数
- 将哈希函数的算法 ID 和哈希编码为 DigestInfo 的 ASN.1 值(附录 A.2.4)
- 生成一个由 emLen - tLen - 0xff 的 3 个八位字节组成的八位字节字符串 PS
- 连接 PS、DER 编码值 T 和其他填充以形成编码消息 EM 为
EM = 0x00 || 0x01 || PS || 0x00 || T
所以,解决这个问题
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
H = 2c f2 4d ba 5f b0 a3 0e 26 e8 3b 2a c5 b9 e2 9e 1b 16 1e 5c 1f a7 42 5e 73 04 33 62 93 8b 98 24
emLen = 512
T = 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 2c f2 4d ba 5f b0 a3 0e 26 e8 3b 2a c5 b9 e2 9e 1b 16 1e 5c 1f a7 42 5e 73 04 33 62 93 8b 98 24
PS = 04 06 02 00 33 ff ff ff
// 00010406020033ffffff003031300d0609608648016503040201050004202cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
EM = 00 01 04 06 02 00 33 ff ff ff 00 30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05 00 04 20 2c f2 4d ba 5f b0 a3 0e 26 e8 3b 2a c5 b9 e2 9e 1b 16 1e 5c 1f a7 42 5e 73 04 33 62 93 8b 98 24
但是当我把它放入privateEncrypt 时,我也没有得到正确的输出。有人可以帮我吗?
【问题讨论】:
标签: node.js rsa signature cryptojs