title: SubtleCrypto importKey from PEM
body: <p>I am trying to use the SubtleCrypto Web API in Ionic to encrypt data using a public key. I am importing the key in PEM format and then passing it onto <code>window.crypto.subtle.importKey</code> and then using that result into <code>window.crypto.subtle.encrypt</code></p>
  
  <p>It would appear that there is a problem with the <code>window.crypto.subtle.importKey</code> - I am getting a <code>Uncaught (in promise): DataError</code> when I am trying to import the key.</p>
  
  <p>I am currently using the following methods to import the key:</p>
  
  <pre><code>//Get the public key in CryptoKey format
  let importedPublicKey = await window.crypto.subtle.importKey(
      "pkcs8",
      this.pemPublicToArrayBuffer(serverPublicKey),
      {
          name: "RSA-OAEP",
          hash: {name: "SHA-256"}
      },
      true,
      []
  );
  
  private pemPublicToArrayBuffer(pem) {
    var b64Lines = this.removeLines(pem);
    var b64Prefix = b64Lines.replace('-----BEGIN PUBLIC KEY-----', '');
    var b64Final = b64Prefix.replace('-----END PUBLIC KEY-----', '');
  
    return this.base64ToArrayBuffer(b64Final);
  }
  
  private base64ToArrayBuffer(b64) {
    var byteString = window.atob(b64);
    var byteArray = new Uint8Array(byteString.length);
    for (var i = 0; i &lt; byteString.length; i++) {
      byteArray[i] = byteString.charCodeAt(i);
    }
  
    return byteArray;
  }
  </code></pre>
  
  <p>Does anyone possibly know why the key import is failing with the PEM public key?</p>