PeculiarVentures/webcrypto

Cannot import private and public key

stan-peryt opened this issue · 2 comments

`import { Crypto } from "@peculiar/webcrypto";

// GENERATE AND EXPORT KEYS
export async function generateKeysTest() {
const WebCrypto = new Crypto();
const { publicKey, privateKey } = await WebCrypto.subtle.generateKey(
{
name: "RSA-PSS",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
true,
["sign", "verify"]
);
const publicExport = await WebCrypto.subtle.exportKey("spki", publicKey);
const privateExport = await WebCrypto.subtle.exportKey("pkcs8", privateKey);

const pubExportedAsString = ab2str(publicExport);
const pubExportedAsBase64 = stringToBase64(pubExportedAsString);
const publicKeyPem = ${pubExportedAsBase64};

const privExportedAsString = ab2str(privateExport);
const privExportedAsBase64 = stringToBase64(privExportedAsString);
const privateKeyPem = ${privExportedAsBase64};

// IMPORT KEYS
const pubKeyImportedAsString = base64ToString(publicKeyPem);
const pubKeyImportedAsArrayBuffer = str2ab(pubKeyImportedAsString);
const publicKeyImport = await WebCrypto.subtle.importKey(
"spki",
pubKeyImportedAsArrayBuffer,
{ name: "RSA-PSS", hash: "SHA-256" },
true,
["verify"]
);

const privateKeyImportedAsString = base64ToString(privateKeyPem);
const privateKeyImportedAsArrayBuffer = str2ab(privateKeyImportedAsString);

const privateKeyImport = await WebCrypto.subtle.importKey(
"pkcs8",
privateKeyImportedAsArrayBuffer,
{ name: "RSA-PSS", hash: "SHA-256" },
true,
["sign"]
);
}

// HELPERS
const ab2str = (buffer: ArrayBuffer) => String.fromCharCode.apply(null, Array.from(new Uint8Array(buffer)));

const str2ab = (str: string): ArrayBuffer => {
const buffer = new ArrayBuffer(str.length * 2);
const bufferInterface = new Uint8Array(buffer);
Array.from(str).forEach((_, index: number) => (bufferInterface[index] = str.charCodeAt(index)));
return buffer;
};

function stringToBase64(value: string) {
return Buffer.from(value).toString("base64");
}

function base64ToString(encryptedString: string) {
return Buffer.from(encryptedString, "base64").toString("binary");
}
`
I'm trying to generate the key, export it and import back, but on key import i get "Too big integer error".

image

Maybe the problem is with your converters.

Here is my example. It uses PEM converter from @peculiar/x509 and script works without any errors

import { Crypto } from "@peculiar/webcrypto";
import { PemConverter } from "@peculiar/x509";

async function main() {
  // GENERATE AND EXPORT KEYS
  const WebCrypto = new Crypto();
  const { publicKey, privateKey } = await WebCrypto.subtle.generateKey(
    {
      name: "RSA-PSS",
      modulusLength: 2048,
      publicExponent: new Uint8Array([1, 0, 1]),
      hash: "SHA-256"
    },
    true,
    ["sign", "verify"]
  );
  const publicExport = await WebCrypto.subtle.exportKey("spki", publicKey);
  const privateExport = await WebCrypto.subtle.exportKey("pkcs8", privateKey);

  const publicKeyPem = PemConverter.encode(publicExport, "PUBLIC KEY");
  console.log(publicKeyPem);

  const privateKeyPem = PemConverter.encode(privateExport, "PRIVATE KEY");
  console.log(privateKeyPem);

  // IMPORT KEYS
  const publicKeyImport = await WebCrypto.subtle.importKey(
    "spki",
    PemConverter.decode(publicKeyPem)[0],
    { name: "RSA-PSS", hash: "SHA-256" },
    true,
    ["verify"]
  );
  console.log(publicKeyImport);

  const privateKeyImport = await WebCrypto.subtle.importKey(
    "pkcs8",
    PemConverter.decode(privateKeyPem)[0],
    { name: "RSA-PSS", hash: "SHA-256" },
    true,
    ["sign"]
  );
  console.log(privateKeyImport);
}

main().catch((e) => console.error(e));

https://codesandbox.io/s/webcrypto-reexport-keys-w7ir18

Perfect, this works! Thank you for super quick response :)