Export webcrypto key to PEM format

我与影子孤独终老i 提交于 2020-12-10 08:11:11

问题


I am using WebCrypto with RSASSA-PKCS1-v1_5 (https://github.com/diafygi/webcrypto-examples#rsassa-pkcs1-v1_5---sign) and I need to export the public key to PEM format using javascript code.

The documentation says that is possible to export the key in this way: https://github.com/diafygi/webcrypto-examples#rsassa-pkcs1-v1_5---exportkey but I need a different format.

Any idea?

Thanks in advance.

Regards


回答1:


Export the public key to spki

window.crypto.subtle.exportKey("spki",keys.publicKey);

And convert the resulting array buffer to base64 adding the PEM headers -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY-----. Below I provide the function spkiToPEM with a full example

crypto.subtle.generateKey(
    {
        name: "RSASSA-PKCS1-v1_5",
        modulusLength: 2048, 
        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
        hash: {name: "SHA-256"}, 
    },
    false, 
    ["sign", "verify"] 
).then(function(keys){     
    return window.crypto.subtle.exportKey("spki",keys.publicKey);
}).then (function(keydata){
    var pem = spkiToPEM(keydata);
    console.log(pem);
}).catch(function(err){
    console.error(err);
});

function spkiToPEM(keydata){
    var keydataS = arrayBufferToString(keydata);
    var keydataB64 = window.btoa(keydataS);
    var keydataB64Pem = formatAsPem(keydataB64);
    return keydataB64Pem;
}

function arrayBufferToString( buffer ) {
    var binary = '';
    var bytes = new Uint8Array( buffer );
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return binary;
}


function formatAsPem(str) {
    var finalString = '-----BEGIN PUBLIC KEY-----\n';

    while(str.length > 0) {
        finalString += str.substring(0, 64) + '\n';
        str = str.substring(64);
    }

    finalString = finalString + "-----END PUBLIC KEY-----";

    return finalString;
}



回答2:


In case anyone is looking for a more modern solution, here is @pedrofb's solution in es-next:

const getPublicKey = async () => {
  const options = {
    name: 'RSASSA-PKCS1-v1_5',
    modulusLength: 2048, 
    publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
    hash: { name: 'SHA-256' }, 
  };

  const keys = await window.crypto.subtle.generateKey(
    options,
    false, // non-exportable (public key still exportable)
    ['sign', 'verify'],
  );

  const publicKey = await window.crypto.subtle.exportKey('spki', keys.publicKey);

  let body = window.btoa(String.fromCharCode(...new Uint8Array(publicKey)));
  body = body.match(/.{1,64}/g).join('\n');

  return `-----BEGIN PUBLIC KEY-----\n${body}\n-----END PUBLIC KEY-----`;
};

Use:

getPublicKey().then(value => console.log(value));

Output:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2NYY4J3mY6DjmhwxRRK9
UMdTx7RnoteFAAlaqkV3jff3o+O8h/NtAi1jnsrMp6SOewdO9Ae8htV5CK7WZ3yX
cJ5hR5yGCcgKcDYSP1PKb9aqp4vGOjrbAhTcJyIs/qjBqtxcYY/oICKMV3Lmmf/E
WJKtXaKJBk5v97XuBuX1ccaNaU7WxW5QayR0kR0oyJh21WJjHaVQEoLABIao+8fy
d/p2nu/BLvPrtIy76M+VrfK0V45ODC0dolx0XtWRhI9odrBBayOvuIwa4nrLYI0W
Y2QW5aQM5R7JddA6KxiVsQr3JsWncEdw/wOkMtKXMEQcmqLldDQgGCOVNxy4saRl
VwIDAQAB
-----END PUBLIC KEY-----



回答3:


The spki format is not well supported by browsers, you basically must use jwt to be interoperable across browsers.

You can see an example going to spki here - https://github.com/PeculiarVentures/PKI.js/blob/5b9c35c154c48b232b45cc2a908c88e2f56a8447/src/CryptoEngine.js#L55



来源:https://stackoverflow.com/questions/40314257/export-webcrypto-key-to-pem-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!