NodeJS rsa例程::数据对于密钥大小太小,rsa例程::数据对于密钥大小太大

qij5mzcb  于 2023-03-08  发布在  Node.js
关注(0)|答案(1)|浏览(540)

当我分别使用crypto.constants.RSA_NO_PADDING时,我收到一个错误。

node:internal/crypto/cipher:79
    return method(data, format, type, passphrase, buffer, padding, oaepHash,
           ^

Error: error:0200007A:rsa routines::data too small for key size
    at Object.publicEncrypt (node:internal/crypto/cipher:79:12)
    at Object._encryptFile (C:\Users\GB\Documents\projects\customprojects\numericalarrays\pickler\node_modules\hasher-apis\src\hasher.js:260:28)
    at Object.<anonymous> (C:\Users\GB\Documents\projects\customprojects\numericalarrays\pickler\demos\demos.jscertificate.pickle.js:44:49)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  library: 'rsa routines',
  reason: 'data too small for key size',
  code: 'ERR_OSSL_RSA_DATA_TOO_SMALL_FOR_KEY_SIZE'
}

Node.js v18.10.0

我使用crypto.constants.RSA_PKCS1_OAEP_PADDING时出现错误。

node:internal/crypto/cipher:79
    return method(data, format, type, passphrase, buffer, padding, oaepHash,
           ^

Error: error:0200006E:rsa routines::data too large for key size
    at Object.publicEncrypt (node:internal/crypto/cipher:79:12)
    at Object._encryptFile (C:\Users\GB\Documents\projects\customprojects\numericalarrays\pickler\node_modules\hasher-apis\src\hasher.js:260:28)
    at Object.<anonymous> (C:\Users\GB\Documents\projects\customprojects\numericalarrays\pickler\demos\demos.jscertificate.pickle.js:44:49)
    at Module._compile (node:internal/modules/cjs/loader:1149:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Module._load (node:internal/modules/cjs/loader:868:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  library: 'rsa routines',
  reason: 'data too large for key size',
  code: 'ERR_OSSL_RSA_DATA_TOO_LARGE_FOR_KEY_SIZE'
}

Node.js v18.10.0

这是加密文件函数

function _encryptFile(remotePath, remoteDestPath, algorithm = "sha256", keyAlgorithm = "rsa", digest = "base64", keyOptions = { modulusLength: 2048 }, options = { modulusLength: 2048 }) {
    const crypto = require('crypto');
    let data = fs.readFileSync(remotePath, { encoding: options.encoding ? options.encoding : "utf-8", flag: "r" });

    algorithm = algorithm || "sha256";
    keyAlgorithm = keyAlgorithm || "rsa";
    digest = digest || "base64";
    keyOptions = keyOptions || { modulusLength: 2048 };
    options = options || { modulusLength: 2048 };

    const { privateKey, publicKey } = _genKeyPair(keyAlgorithm, keyOptions);

    let encrypted = crypto.publicEncrypt({
        key:  publicKey,
        padding: crypto.constants.RSA_PKCS1_PADDING,
        oaepHash: algorithm
    },
        Buffer.from(data)
    ).toString(digest);

    fs.writeFileSync(remoteDestPath, encrypted);
    return {
        privateKey: privateKey,
        publicKey: publicKey,
        encrypted: encrypted
    }
}

这是解密文件函数
一个三个三个一个
这是函数的加密解密文件用法

// Hash P3 File to P3
let { privateKey, publicKey, encrypted } = hash.encrypt(p1, p1, "sha256", "rsa", "base64", { modulusLength: 2048 }, { modulusLength: 2048 });
// let { privateKey, publicKey, encrypted } = hash.encrypt(p3, p1);
console.log("[hasher._fileHash]: File hashed");
console.log(encrypted);

// DeHash P3 File hashed content to P3
let r3 = hash.decrypt(p1, p1, privateKey, "sha256", "rsa", "base64", { modulusLength: 2048 });
// let r3 = hash.decrypt(p1, p1, privateKey);
console.log("[hasher._fileHash]: File dehashed");
console.log(r3);

这是I do not get数据的文件内容,对于密钥大小而言太大':

1. C
2. C++
3. Java

This is a file containing a collection of interpreted programming languages.
1. Python
2. Nodejs
3. Ruby

以下是I do not get "data too small for key size"的文件内容:

1. C
2. C++
3. Java

当我使用"crypto.constants.RSA_PKCS1_PADDING"时,我没有得到这个错误。这里的问题是什么?我如何为动态用例获得正确的缓冲区长度?

mefy6pfw

mefy6pfw1#

使用RSA加密时,出于安全原因,您始终必须使用填充。消息加上填充的长度最多可以与密钥大小(或以字节为单位的模数)一样大。由于填充具有最小长度,因此这会导致最大消息长度等于密钥大小减去填充的最小长度。
对于PKCS#1 v1.5填充(RSAES-PKCS 1-v1_5),填充的最小长度是11字节,因此,2048位密钥可以加密最大256 - 11 = 244字节,另见here
对于OAEP(RSAES-OAEP),填充的最小长度取决于OAEP摘要,为2 * (hLen + 1),其中hLen是摘要的输出大小(以字节为单位)。
因此,对于输出大小为20字节的SHA-1,填充的最小长度为42字节,对于输出大小为32字节的SHA-256,填充的最小长度为66字节。
因此,2048位密钥可以加密最大256 - 42 = 214字节(对于OAEP/SHA-1)和最大256 - 66 = 190字节(对于OAEP/SHA-256),另请参见here以表格形式概述不同密钥大小和摘要的最大消息长度。
对于较大的消息,加密通常不使用RSA,而是使用hybrid encryption(也可参见here)。为此,数据使用对称算法(例如AES)加密,而对称密钥使用非对称算法(例如RSA)加密。

相关问题