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

Cannot create RSA PublicKey from PEM format #174

Open
ZeCarlosCoutinho opened this issue Jul 10, 2018 · 2 comments
Open

Cannot create RSA PublicKey from PEM format #174

ZeCarlosCoutinho opened this issue Jul 10, 2018 · 2 comments

Comments

@ZeCarlosCoutinho
Copy link

When I try to create an RSA PublicKey from the PEM format (PKCS1, I believe), it throws the following error:
Error: Not a public key.
at Object.createPublicKey

My code is the following:

var publicKey = ursa.createPublicKey(Buffer.from("-----BEGIN RSA PUBLIC KEY-----\n"+
        "MIIBCgKCAQEAt6j2fSP9MFbqninmlzSG1Wf7veRDN4ttyYWredHorXhRYkEGk+HW\n"+
        "rlLGMP7uLq9wirxuUct70tlFwO0kBpS/9Wj9MlC3PutYHr5k5/c0HAUcOI/sIl+8\n"+
        "uLcZS7zs+IHNOSt+FxoaQdgvfvxY1LkN90ehd+7DAFO23HPspoAaeHf85W8x+2az\n"+
        "mUQ17WdFsParHSMeYdur6+h6NKTava/vLSKlN5/K6s7KvF1IU4eqGAesvRhil3sR\n"+
        "AnDwvDucavdZWAfW747n5iUbuS5o3PpBquMwVSXHU0QQqHSjkQPKoWtrxphgM3HP\n"+
        "erLrRPiya0O8kphxEbro7wVaFUa0VjP36wIDAQAB\n"+
        "-----END RSA PUBLIC KEY-----"
    ));

When doing the same for PrivateKey, using the same format and createPrivateKey(Buffer.from("<PRIVATEKEY>")) method, it works.
(I also tried using another key from somewhere else, and there was no error, but the key was in a different format (PKCS8, I believe))

Why does the Private Key work, and the Public key doesn't?

@bartlewis
Copy link

Did you ever solve this @ZeCarlosCoutinho ?

@hugo-marques-m3
Copy link

You probably found this out already sinde this is very old but you need to use \r in the middle lines and \r\n before the last line.
As in:

var publicKey = ursa.createPublicKey(Buffer.from("-----BEGIN RSA PUBLIC KEY-----\n"+
        "MIIBCgKCAQEAt6j2fSP9MFbqninmlzSG1Wf7veRDN4ttyYWredHorXhRYkEGk+HW\r"+
        "rlLGMP7uLq9wirxuUct70tlFwO0kBpS/9Wj9MlC3PutYHr5k5/c0HAUcOI/sIl+8\r"+
        "uLcZS7zs+IHNOSt+FxoaQdgvfvxY1LkN90ehd+7DAFO23HPspoAaeHf85W8x+2az\r"+
        "mUQ17WdFsParHSMeYdur6+h6NKTava/vLSKlN5/K6s7KvF1IU4eqGAesvRhil3sR\r"+
        "AnDwvDucavdZWAfW747n5iUbuS5o3PpBquMwVSXHU0QQqHSjkQPKoWtrxphgM3HP\r"+
        "erLrRPiya0O8kphxEbro7wVaFUa0VjP36wIDAQAB\r\n"+
        "-----END RSA PUBLIC KEY-----"
    ));

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