Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with public key loading #85

Open
misko-frame opened this issue Mar 19, 2022 · 3 comments
Open

Problem with public key loading #85

misko-frame opened this issue Mar 19, 2022 · 3 comments

Comments

@misko-frame
Copy link

This is the code I am trying to run:

import eccrypto from 'eccrypto';

function removeLines(str: string): string {
  return str.replace('\n', '');
}

function base64ToArrayBuffer(b64: string): Uint8Array {
  const byteString = window.atob(b64);
  const byteArray = new Uint8Array(byteString.length);
  for (let i = 0; i < byteString.length; i++) {
    byteArray[i] = byteString.charCodeAt(i);
  }

  return byteArray;
}

function pemToArrayBuffer(pem: string): Uint8Array {
  const b64Lines = removeLines(pem);
  const b64Prefix = b64Lines.replace('-----BEGIN PUBLIC KEY-----', '');
  const b64Final = b64Prefix.replace('-----END PUBLIC KEY-----', '');

  return base64ToArrayBuffer(b64Final);
}

describe('test', () => {
  it('', async () => {
    const pk = '-----BEGIN PUBLIC KEY-----\n' +
      'MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEW3yGReZI3dI37WZipbNcNunkHSX3gx5n\n' +
      'NFC8V5HyWtHktU17mDH+0zWDVodNazTqqQFIUIuXz/e++o1JFEYh2A==\n' +
      '-----END PUBLIC KEY-----';
    const publicKeyAsArrayBuffer = pemToArrayBuffer(pk);
    console.log('publicKeyAsArrayBuffer', publicKeyAsArrayBuffer);
    try {
      await eccrypto.encrypt(publicKeyAsArrayBuffer, Buffer.from('test', 'utf8'));
    } catch (e) {
      console.warn(e);
    }
  });
});

This code snippet is written as a unit test for sake of simplicity, the execution was also in the browser.
Basically, I am trying to transform pk pem to ArrayBuffer and encrypt a message, but getting Error: Bad public key
( btw public key in the example is legit)
Screenshot 2022-03-19 at 22 47 43

Could someone suggest what needs to be done in order to be able to encrypt properly?

@willpiam
Copy link

did you get this to work?

@misko-frame
Copy link
Author

misko-frame commented Jul 14, 2022

nope... we have abandoned the approach with this lib

@jcbdev
Copy link

jcbdev commented Aug 13, 2022

@misko-frame I don't support this library but I was just reading in passing whilst working on another project. If I convert the base64 to bytes manually from the example you posted above I get a buffer of 88 bytes.

ecdsa public keys are 64 bytes long. I think the issue might actually be the initial input?

I think the first 23 bytes is ASN.1 stuff as is mentioned here on this thread - https://security.stackexchange.com/questions/255644/how-do-i-turn-an-88-byte-ecdsa-public-key-into-a-65-byte-uncompressed-or-33-byte

i think you need to remove the first 23 bytes something like this:

let byteString = window.atob(b64);
byteString = byteString.substring(23, byteString.length);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants