0

I try to generate a RSA key with Web Crypto and use it to sign a string with jsrsasign (Firefox does not support RSA-PSS). Therefore, I export the Web Crypto key and convert it to PKCS8-PEM, but when I call KEYUTIL.getKeyFromPlainPrivatePKCS8PEM to import the key to jsrsasign an error is thrown: malformed plain PKCS8 private key(code:001)

What did I wrong? JSBin

window.crypto.subtle.generateKey(
  {
    name: "RSA-OAEP",
    modulusLength: 2048,
    publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
    hash: {name: "SHA-256"},
  },
  true,
  ["encrypt", "decrypt"]
)
.then(keyPair => window.crypto.subtle.exportKey("pkcs8", keyPair.privateKey))
.then(arrayBufferToBase64String)
.then(toPem)
.then(pem => {
  var rsa = KEYUTIL.getKeyFromPlainPrivatePKCS8PEM(pem); // throws: malformed plain PKCS8 private key(code:001)
  var sig = rsa.signStringPSS('text', 'sha256', 32);
  console.log('signature', sig);
})
.catch(::console.error)

function arrayBufferToString(arrayBuffer) {
  var byteArray = new Uint8Array(arrayBuffer)
  var byteString = '';
  for (var i=0; i<byteArray.byteLength; i++) {
    byteString += String.fromCharCode(byteArray[i]);
  }
  return byteString;
}

function arrayBufferToBase64String(arrayBuffer) {
  return btoa(arrayBufferToString(arrayBuffer));
}

function toPem(key) {
  return `
-----BEGIN RSA PRIVATE KEY-----
${key}
-----END RSA PRIVATE KEY-----
`;
}

Edit:

I just realized that jsrsasign can handle jwk:

window.crypto.subtle.generateKey(
  {
    name: "RSA-OAEP",
    modulusLength: 2048,
    publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
    hash: {name: "SHA-256"},
  },
  true,
  ["encrypt", "decrypt"]
)
.then(keyPair => window.crypto.subtle.exportKey("jwk", keyPair.privateKey))
.then(jwk => {
  var rsa = KEYUTIL.getKey(jwk);
  var sig = rsa.signStringPSS('text', 'sha256', 32);
  console.log('signature', sig);
})
.catch(::console.error)

I prefer this solution, but I still like to know why my pkcs8 solution does not work.

4

2 回答 2

3

由于内容不同,因此将 PEM 标头从"----BEGIN RSA PRIVATE KEY-----"更改为不起作用。"-----BEGIN PRIVATE KEY-----"我认为使用JWK进行文件导出是最好的方法,因为它在任何浏览器之间都具有很大的互操作性。我之前尝试过同样的事情,但是PKCS#8在某些浏览器上不起作用。

如 Felix 所述,您的RSA密钥是具有PE ​​M标头的PKCS#1密钥。"-----BEGIN RSA PRIVATE KEY-----"

如果您需要将PKCS#1转换为PKCS#8密钥KEYUTIL类可能很有用,如下例所示:

keyobj = KEYUTIL.getKey("-----BEGIN RSA PRIVATE KEY-----..."); // your key
pkcs8pem = KEYUTIL.getPEM(keyobj, "PKCS8PRV");
于 2016-09-25T15:09:25.310 回答
2

由于“RSA”部分,您的 PEM 编码密钥实际上是 PKCS#1 而不是 PKCS#8。这只是 DER 中的 RSA 密钥对象,没有包含在序列中的密钥标识符。

jsrsasign 正在寻找 PKCS#8 标头“BEGIN PRIVATE KEY”而不是“BEGIN RSA PRIVATE KEY”。更改标题使其工作。

于 2015-11-21T15:38:55.140 回答