node.js - 如何在 NodeJS 中提取密钥大小
问题描述
我正在尝试从 PEM 编码的证书中获取密钥大小。
我尝试使用 node-forge ( https://www.npmjs.com/package/node-forge ) 提取它,首先将其转换为伪造证书,然后尝试从中获取密钥大小。
但是,我在伪造证书中找不到正确的信息。
公钥下的伪造证书中有此信息,我想我应该能够从模数'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用作公钥和私钥的模数,它的位长度也是密钥长度。
因此,如果(如您所说)我们可以得到n的大小(以位为单位),我们就有了密钥长度。
公钥存储为一个大整数,因此我们可以调用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"));
推荐阅读
- javascript - 我正在使用 jQuery 进行图像预览。但是我无法发布预览后单击的数据。我怎样才能让它成为现实?
- javascript - ReactJS 中的样式化组件自定义 CSS
- flutter - 对颤动的东西设置时间限制
- html - 选择单选按钮时可以更改背景吗?
- firebase - 如何使用颤振从 Firebase 存储中查看我上传的 pdf?
- google-apps-script - 将 GET 请求发送到 Trello API 并将数据导入 Google 表格的 Google 脚本
- python - XGBRegressor 训练和测试得分高,但预测不好
- reactjs - 内容未在生产中显示在开发中效果很好材料 ui 定价示例
- excel - VBA错误处理转到下一个循环而不是恢复
- react-native - 如何使用 DrawerNavigation 在 React Native 中实现注销