Node.js crypto publicEncrypt返回的值太长

问题描述 投票:0回答:1

我正在尝试通过iOS设备生成的RSA公钥对字符串进行加密(快速)。我正在使用crypto.publicEncrypt函数,但是返回的base64字符串对于密钥来说太长。

就我而言,是3072位RSA密钥,因此预期的加密值长度为384字节,但是crypto.publicEncrypt()返回512。

我确实尝试了此功能的所有选项以及客户端的所有修改。我可以肯定问题出在加密上,因为客户端可以使用相同的密钥进行加密。

如果有人可以保存我的周末!!

我的代码:

// Dependencies
const crypto = require('crypto')

// The secret data
const message = Buffer.from("Secret message")

// Secret data encryption
const publicKey // Buffer from Binary Data of the public key, in PEM format
const pem = publicKey.toString('utf8')
const opts = {
    key: pem, // Public key
    oaepHash: 'RSA-SHA384', // Algorithm used by the client
    padding: crypto.constants.RSA_PKCS1_OAEP_PADDING // Every other paddings throw an exception
}
var bufferEncryption = crypto.publicEncrypt(opts, message)
const encryptedMessage = bufferEncryption.toString('base64')

// Sending encryptedMessage to the client...

// My issue
console.log(encryptedMessage.length) // 512 (expected: 384)
node.js swift encryption rsa pem
1个回答
0
投票

我不确定您为什么希望密钥为384个字符。 Base 64使用4个字符编码3个字节(或每个字符6位)。如果我们假设相同数量的字节-对于US-ASCII兼容方案(例如Windows-1252或实际上是UTF-8),则(384/3)* 4 =512。您所讨论的字节大小为大小无对文本的任何编码

© www.soinside.com 2019 - 2024. All rights reserved.