How can I import an RSA private key in PEM format for use with WebCrypto?

空扰寡人 提交于 2019-12-04 05:26:07

问题


I'm trying to use WebCrypto to sign a token with RSA-PSS, but I keep getting the error:

DataError: Data provided to an operation does not meet requirements

at crypto.subtle.importKey.

This is my JavaScript code:

function signToken(token, key) {
    crypto.subtle.importKey(
        'pkcs8',
        PEM2Binary(key),
        {
            name: 'RSA-PSS',
            hash: { name: 'SHA-256' },
        },
        false,
        ['sign']
    ).then(function(privKey){
        crypto.subtle.sign(
            'RSA-PSS',
            privKey,
            new TextEncoder().encode(token)
        ).then(function(signedToken){
            msg = JSON.stringify({ intent: 'authenticate-token', signedToken: signedToken });
            socket.send(msg);
        })
    }).catch(function(error){
        console.error(error);
    })
}

function PEM2Binary(pem) {
    var encoded = '';
    var lines = pem.split('\n');
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].indexOf('-----') < 0) {
            encoded += lines[i];
        }
    }
    var byteStr = atob(encoded);
    var bytes = new Uint8Array(byteStr.length);
    for (var i = 0; i < byteStr.length; i++) {
        bytes[i] = byteStr.charCodeAt(i);
    }
    return bytes.buffer;
}

And the sample private key I'm using:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA3d+Y/kWq4nNrJd0dgktQ7lfDUtEIxcs10I8+xPEUhpvacotB
o787dDP4ZFYhRQf0WMjQYJwEna/NsrPuY2fgFWIBlUdpkj1SR00FBxHvnRlzP1Sg
cYnJlqh7/A26tM51nVkMtf/4NtYr+jFvRmB5759U1GRBFwZYsoWQRQlQKh5MAk89
KRc/TvrXNzam39GcLvSYz9uN0p2p2Mrofjgv42m53AqC9LOrQbwDX7Dxl0n4z3EI
Z2Ycx33QZVYiLPH1GHTJ8+JuKjqY0ovu6lghGN0y2xR+sqWNvgyW7xlDWZ6Vwyxs
K5SCIc48VwiiAyGZGhcCZloMlO5S/dutycgHhQIDAQABAoIBAQDXfef2bmu+bSNQ
LyYN+mCsXQkUUnoWwXuPCNGKLiwlYRIV1jL2ezGfdyp1KU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EGZZ9wBffnhv1rjfNh6PbrcSdSKOznPEMpvw/pOB
wBnu6RWZ9mtWWIdDmYEJDqzrV7qp2w2x2foLc4oyCUKbUbQ/DvTYFQ==
-----END RSA PRIVATE KEY-----

Thanks in advance for any help.


回答1:


The header -----BEGIN RSA PRIVATE KEY----- in the PEM file is reserved to PKCS#1 keys, but WebCryptoApi does not support pkcs1, so you need to convert the key from pkcs1 to pcks8 using a tool like openssl

# openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in pkcs1.key -out pkcs8.key


来源:https://stackoverflow.com/questions/51033786/how-can-i-import-an-rsa-private-key-in-pem-format-for-use-with-webcrypto

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