【问题标题】:NodeJS RSA prehashed signNodeJS RSA 预哈希符号
【发布时间】: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

  1. 对消息应用哈希函数
  2. 将哈希函数的算法 ID 和哈希编码为 DigestInfo 的 ASN.1 值(附录 A.2.4)
  3. 生成一个由 emLen - tLen - 0xff 的 3 个八位字节组成的八位字节字符串 PS
  4. 连接 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


    【解决方案1】:

    您正在尝试手动执行PKCS1-padding (RSASSA-PKCS1-V1_5)。但这不是必需的。连接Hash-ID(这里是SHA-256)和你的数据(fingerprint)就足够了,其余的由implicitly选择的填充(crypto.constants.RSA_PKCS1_PADDING)完成,即

    // Signing
    var fingerprint = Buffer.from('2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824','hex');
    var id = Buffer.from([0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20]);
    var allData = Buffer.concat([id, fingerprint]);
    var signature = crypto.privateEncrypt(privateKey, allData); // crypto.constants.RSA_PKCS1_PADDING by default
    
    // Verifying with createVerify
    var verify = crypto.createVerify('RSA-SHA256');
    verify.update('<the signed data>'); 
    var verified = verify.verify(publicKey, signature); // provides true
    
    // Verifying with publicDecrypt
    var decryptedFingerprint = crypto.publicDecrypt(publicKey, Buffer.from(signature)).slice(-32); // provides fingerprint
    

    注意:如果你想手动填充,你必须在allData-buffer之前设置字节序列0x00 || 0x01 || PS || 0x00PS 由达到密钥长度(以字节为单位)所需的0xff-bytes 组成。此外,国旗 crypto.constants.RSA_NO_PADDING 必须在 privateEncrypt 调用中显式设置。但是,这不是必需的,因为结果是相同的。详情见RFC 8017, Section 8.2.1

    【讨论】:

    • @tperfitt:你说得对,前缀丢失了。我已经纠正了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 2014-02-22
    • 1970-01-01
    • 2019-07-24
    • 2022-01-16
    • 2017-02-19
    • 2019-11-20
    • 2019-05-17
    相关资源
    最近更新 更多