英文:
Convert jwk to pem in nest.js
问题
我有这个jwk密钥,我想要转换成pem格式:
const jwkey = {
kty: 'RSA',
kid: 'eecb0ced-4d49-4100-9547-841e4100b756',
n: '...非常长的字符串',
e: 'AQAB',
alg: 'RS256',
use: 'sig',
};
我尝试了像 **jose, node-rsa 和 jwk-To-Pem** 这样的库,但不知何故它们都失败了,并且在配置时会出现各种错误。
我真的不知道如何配置它。
以下是使用 node-rsa 进行转换的示例:
const key = new NodeRSA();
key.importKey(
{
n: Buffer.from(jwkey.n, 'base64'),
e: Buffer.from(jwkey.e, 'base64'),
},
'components'
);
const pem = key.exportKey('pkcs1-private-pem');
英文:
I have this jwk key that I wanto convert into pem:
const jwkey = {
kty: 'RSA',
kid: 'eecb0ced-4d49-4100-9547-841e4100b756',
n: '...very long string',
e: 'AQAB',
alg: 'RS256',
use: 'sig',
};
I have tried the libraries like jose, node-rsa and jwk-To-Pem but somehow all of them are failing and throws error here and there while configuring.
I literally have no idea how to configure it.
below is the example of node-rsa conversion:
const key = new NodeRSA();
key.importKey(
{
n: Buffer.from(jwkey.n, 'base64'),
e: Buffer.from(jwkey.e, 'base64'),
},
'components'
);
const pem = key.exportKey('pkcs1-private-pem');
答案1
得分: 0
You could use the 'jwk-to-pem' module.
const jwkToPem = require("jwk-to-pem")
const jwkey = {
kty: "RSA",
alg: "RS512",
kid: "26887d3ee3293c526c0e6dd05f122df53aa3f13d7dad06d25e266fa6f51db79fb52422aaf79f121476237e98dcd6640350fee47fec70e783544ec9a36e4605bc",
use: "sig",
n: "14m79mVwIE0JxQdKrgXVf7dVcBS90U0TvG7Yf7dG4NJocz1PNUrKrzGhe_FryOe0JahL_sjA2_rKw7NBCpuVx_zSPFRw6kqjewGicjXGus5Fmlf3zDuqwV4BWIFHyQexMPOly0agFfcM0M0MgBULXjINgBs9MwnRv7JVfRoGqXHsNM45djFDd3o4liu4LPlge_DquZUFLNu-BYAyAlWkz0H2TepZhGrN9VEPmxzQkNzXc1R4MpZvbxrRRgaAA2z094ik3hk86JhfyFq-LDcueZhtshmrYZ95LWgMlQ7PixkeK1HkeEYMt20lmNzR8B8KabimYmibxA4Ay9gpRwfp-Q",
e: "AQAB",
}
console.log("jwkey : ", jwkey)
const pem = jwkToPem(jwkey)
console.log("create pem : ", pem)
英文:
You could use 'jwk-to-pem' module.
const jwkToPem = require("jwk-to-pem")
const jwkey = {
kty: "RSA",
alg: "RS512",
kid: "26887d3ee3293c526c0e6dd05f122df53aa3f13d7dad06d25e266fa6f51db79fb52422aaf79f121476237e98dcd6640350fee47fec70e783544ec9a36e4605bc",
use: "sig",
n: "14m79mVwIE0JxQdKrgXVf7dVcBS90U0TvG7Yf7dG4NJocz1PNUrKrzGhe_FryOe0JahL_sjA2_rKw7NBCpuVx_zSPFRw6kqjewGicjXGus5Fmlf3zDuqwV4BWIFHyQexMPOly0agFfcM0M0MgBULXjINgBs9MwnRv7JVfRoGqXHsNM45djFDd3o4liu4LPlge_DquZUFLNu-BYAyAlWkz0H2TepZhGrN9VEPmxzQkNzXc1R4MpZvbxrRRgaAA2z094ik3hk86JhfyFq-LDcueZhtshmrYZ95LWgMlQ7PixkeK1HkeEYMt20lmNzR8B8KabimYmibxA4Ay9gpRwfp-Q",
e: "AQAB",
}
console.log("jwkey : ", jwkey)
const pem = jwkToPem(jwkey)
console.log("create pem : ", pem)
答案2
得分: 0
我已经尝试过像 jose 这样的库。
以下代码段将始终适用于 RSA 公钥,使用 jose
。
const jwk = {
kty: 'RSA',
n: 'pksialyL7vdNKFhL9Nu1uVs49L8vKqtRB3O_hSA_bICHKvNLX9_5NtoN1oDV7Mjz_rBOxM3AcnPV97OJPBrHKbQxhX3P5PN0LTqVLh0_Pcn5s7KHRpT1-jhHad5FaeJ2WdBRUhr8cEbVnYvAEmGfB3kduyPGNqSAbJC0PtEEJZlNralFLyJV0wEC1t7a9hRAXXkL52D_UbKnF05Gjn1XioHOKxEjlZirzrj0z8SUHXAD8UPJMAGvzakBRb_K46hcdl8G9_7t3RLjVyGwVwnZOWBZosUU4jOtQx0OZtDEO4rbw4yDMA4cHJs1FCDDsXi_r1tlZ5EKSzU51YDVx3txNQ',
e: 'AQAB'
}
const key = await jose.importJWK({
...jwk,
ext: true,
}, 'RS256')
const pem = await jose.exportSPKI(key)
console.log(pem)
// -----BEGIN PUBLIC KEY-----
// MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApksialyL7vdNKFhL9Nu1
// uVs49L8vKqtRB3O/hSA/bICHKvNLX9/5NtoN1oDV7Mjz/rBOxM3AcnPV97OJPBrH
// KbQxhX3P5PN0LTqVLh0/Pcn5s7KHRpT1+jhHad5FaeJ2WdBRUhr8cEbVnYvAEmGf
// B3kduyPGNqSAbJC0PtEEJZlNralFLyJV0wEC1t7a9hRAXXkL52D/UbKnF05Gjn1X
// ioHOKxEjlZirzrj0z8SUHXAD8UPJMAGvzakBRb/K46hcdl8G9/7t3RLjVyGwVwnZ
// OWBZosUU4jOtQx0OZtDEO4rbw4yDMA4cHJs1FCDDsXi/r1tlZ5EKSzU51YDVx3tx
// NQIDAQAB
// -----END PUBLIC KEY-----
英文:
> I have tried the libraries like jose
The following will always work for an RSA public key, using jose
.
const jwk = {
kty: 'RSA',
n: 'pksialyL7vdNKFhL9Nu1uVs49L8vKqtRB3O_hSA_bICHKvNLX9_5NtoN1oDV7Mjz_rBOxM3AcnPV97OJPBrHKbQxhX3P5PN0LTqVLh0_Pcn5s7KHRpT1-jhHad5FaeJ2WdBRUhr8cEbVnYvAEmGfB3kduyPGNqSAbJC0PtEEJZlNralFLyJV0wEC1t7a9hRAXXkL52D_UbKnF05Gjn1XioHOKxEjlZirzrj0z8SUHXAD8UPJMAGvzakBRb_K46hcdl8G9_7t3RLjVyGwVwnZOWBZosUU4jOtQx0OZtDEO4rbw4yDMA4cHJs1FCDDsXi_r1tlZ5EKSzU51YDVx3txNQ',
e: 'AQAB'
}
const key = await jose.importJWK({
...jwk,
ext: true,
}, 'RS256')
const pem = await jose.exportSPKI(key)
console.log(pem)
// -----BEGIN PUBLIC KEY-----
// MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApksialyL7vdNKFhL9Nu1
// uVs49L8vKqtRB3O/hSA/bICHKvNLX9/5NtoN1oDV7Mjz/rBOxM3AcnPV97OJPBrH
// KbQxhX3P5PN0LTqVLh0/Pcn5s7KHRpT1+jhHad5FaeJ2WdBRUhr8cEbVnYvAEmGf
// B3kduyPGNqSAbJC0PtEEJZlNralFLyJV0wEC1t7a9hRAXXkL52D/UbKnF05Gjn1X
// ioHOKxEjlZirzrj0z8SUHXAD8UPJMAGvzakBRb/K46hcdl8G9/7t3RLjVyGwVwnZ
// OWBZosUU4jOtQx0OZtDEO4rbw4yDMA4cHJs1FCDDsXi/r1tlZ5EKSzU51YDVx3tx
// NQIDAQAB
// -----END PUBLIC KEY-----
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论