如何在 NodeJS 中提取密钥大小
How to extract key size in NodeJS
我正在尝试从 PEM 编码的证书中获取密钥大小。
我尝试使用 node-forge (https://www.npmjs.com/package/node-forge) 提取它,首先将其转换为伪造证书,然后尝试从中获取密钥大小。
但是,我在伪造的证书中找不到正确的信息。
public密钥下的伪造证书中有此信息,我想我应该可以从模数'n'
中获取信息
{ n: BigInteger { data: [Array], t: 74, s: 0 },
e: BigInteger { data: [Array], t: 1, s: 0 },
encrypt: [Function],
verify: [Function] },
这里,'n'的数组是
BigInteger {
data:
[ 108702707,
223366147,
1633698,
222104385,
2057385,
196952745,
204102614,
1342314,
215447298,
167299729,
234087419,
218888278,
143261467,
196197892,
83562517,
50733325,
114027487,
90758946,
9956532,
60800276,
8677133,
7005374,
254551822,
214728639,
42558032,
110792918,
136202203,
78922972,
40753235,
245284543,
194070574,
248422593,
5163396,
151359098,
77422943,
72471134,
181405400,
207346591,
185707006,
185418315,
263158064,
111864582,
186113288,
54738616,
138771291,
249640899,
232181943,
117496275,
231520296,
184509360,
179085501,
215072100,
85449772,
136664237,
71259060,
139830485,
264798471,
266417322,
142764588,
177236257,
17830318,
9879037,
168589759,
121974085,
54883138,
87144585,
7724711,
192243183,
194739694,
159581652,
122617175,
91020203,
117134207,
13 ],
t: 74,
s: 0 },
我可以从这个数组中获取密钥大小吗?或者是否有另一种更简单的方法从 PEM 编码的证书中获取 keySize?
我认为下面的方法应该可行,因为 n 被用作 public 和私钥的模数,它的位长度也是密钥长度.
因此,如果(如您所说)我们可以得到 n 的位大小,我们就有了密钥长度。
public 键存储为 big-integer, so we can call the bitLength() 函数。
下面的代码对我有用:
const forge = require('node-forge');
const fs = require('fs');
function getCertificatePublicKeyBitLength(pemFile) {
const certificate = forge.pki.certificateFromPem(fs.readFileSync(pemFile));
return certificate.publicKey.n.bitLength();
}
console.log("Bit length: ", getCertificatePublicKeyBitLength("cert.pem"));
我正在尝试从 PEM 编码的证书中获取密钥大小。
我尝试使用 node-forge (https://www.npmjs.com/package/node-forge) 提取它,首先将其转换为伪造证书,然后尝试从中获取密钥大小。
但是,我在伪造的证书中找不到正确的信息。
public密钥下的伪造证书中有此信息,我想我应该可以从模数'n'
中获取信息 { n: BigInteger { data: [Array], t: 74, s: 0 },
e: BigInteger { data: [Array], t: 1, s: 0 },
encrypt: [Function],
verify: [Function] },
这里,'n'的数组是
BigInteger {
data:
[ 108702707,
223366147,
1633698,
222104385,
2057385,
196952745,
204102614,
1342314,
215447298,
167299729,
234087419,
218888278,
143261467,
196197892,
83562517,
50733325,
114027487,
90758946,
9956532,
60800276,
8677133,
7005374,
254551822,
214728639,
42558032,
110792918,
136202203,
78922972,
40753235,
245284543,
194070574,
248422593,
5163396,
151359098,
77422943,
72471134,
181405400,
207346591,
185707006,
185418315,
263158064,
111864582,
186113288,
54738616,
138771291,
249640899,
232181943,
117496275,
231520296,
184509360,
179085501,
215072100,
85449772,
136664237,
71259060,
139830485,
264798471,
266417322,
142764588,
177236257,
17830318,
9879037,
168589759,
121974085,
54883138,
87144585,
7724711,
192243183,
194739694,
159581652,
122617175,
91020203,
117134207,
13 ],
t: 74,
s: 0 },
我可以从这个数组中获取密钥大小吗?或者是否有另一种更简单的方法从 PEM 编码的证书中获取 keySize?
我认为下面的方法应该可行,因为 n 被用作 public 和私钥的模数,它的位长度也是密钥长度.
因此,如果(如您所说)我们可以得到 n 的位大小,我们就有了密钥长度。
public 键存储为 big-integer, so we can call the bitLength() 函数。
下面的代码对我有用:
const forge = require('node-forge');
const fs = require('fs');
function getCertificatePublicKeyBitLength(pemFile) {
const certificate = forge.pki.certificateFromPem(fs.readFileSync(pemFile));
return certificate.publicKey.n.bitLength();
}
console.log("Bit length: ", getCertificatePublicKeyBitLength("cert.pem"));