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

How to import a public key which is made with ECDSA? #49

Open
zcqzcg opened this issue Jan 3, 2020 · 2 comments
Open

How to import a public key which is made with ECDSA? #49

zcqzcg opened this issue Jan 3, 2020 · 2 comments
Labels

Comments

@zcqzcg
Copy link

zcqzcg commented Jan 3, 2020

Hi there,
First thanks for the amazing effort in this pakcage, it's really saving my day.

Well here seems to be some problems with my code.

what I can do:

  • import a privateKey
  • use it to generate a new publicKey, and de/encrypt works like a charming.

what I want:

  • import the publicKey and the privateKey at the same time
  • use them to de/encrypt, don't have to generate a new one

The code under blow is as far as I can get. It's a bit of ugly, please don't laugh at me for me being a rookie.

var keyUtils = require('js-crypto-key-utils');
var eccrypto = require("eccrypto");


var privKeyA = `-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgYYZNfKK0D3KJUE45
m7mBYQsH+iOu3IuzwK2W21/3lQChRANCAAT0SdzISybvGSWhwokYCndUAYwSPIkE
LuvZ2y9CBGO6PGGRvWJTvklrMfaALSxFlcXpdSfnB5ieBrht+qspmGBC
-----END PRIVATE KEY-----`;
var pubKeyA = `-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE9EncyEsm7xklocKJGAp3VAGMEjyJ
BC7r2dsvQgRjujxhkb1iU75JazH2gC0sRZXF6XUn5weYnga4bfqrKZhgQg==
-----END PUBLIC KEY-----`;

const pubKeyObjFromPemA = new keyUtils.Key('pem', pubKeyA);
// console.log(pubKeyObjFromPemA);
const prvKeyObjFromPemA = new keyUtils.Key('pem', privKeyA);
// console.log(prvKeyObjFromPemA);



async function foo() {

    let prvKeyJWKA = await prvKeyObjFromPemA.export('oct');
    let pubKeyJWKA = await pubKeyObjFromPemA.export('oct');

    // console.log(prvKeyJWKA);
    // console.log(pubKeyJWKA);

    // let pubAHex = Array.prototype.map.call(pubKeyJWKA, x => ('00' + x.toString(16)).slice(-2)).join('');

    var privateKeyA = Buffer.from(prvKeyJWKA);

    var publicKeyA = Buffer.from(pubKeyJWKA);
    console.log(publicKeyA.toString('hex'));

    let pubAHex = Array.prototype.map.call(publicKeyA, x => ('00' + x.toString(16)).slice(-2)).join('');
    console.log(pubAHex);

    publicKeyA = eccrypto.getPublic(privateKeyA);
    console.log(publicKeyA.toString('hex'));

    // Encrypting the message for A.
    let encrypted = await eccrypto.encrypt(publicKeyA, Buffer.from("msg to a asdasdasdasd"));

    console.log(encrypted.ciphertext.toString());

    let plaintext = await eccrypto.decrypt(privateKeyA, encrypted);

    console.log(plaintext.toString())

}


foo();

and here is what I got from the last run :

04f449dcc84b26ef1925a1c289180a7754018c123c89042eebd9db2f420463ba3c6191bd6253be496b31f6802d2c4595c5e97527e707989e06b86dfaab29986042
04f449dcc84b26ef1925a1c289180a7754018c123c89042eebd9db2f420463ba3c6191bd6253be496b31f6802d2c4595c5e97527e707989e06b86dfaab29986042
04f8d2d188ba947c54af0607e689258f2bdd7ab99027752975915b1baf56a9bf8c35c23fe2b0dc53999c60a963b317eabcd0bbdf612359906737d1173a1d386e0c
������ӆ�s������>�w�$���v�_�O)�d
msg to a asdasdasdasd

I really hope you may help me with this issue. Also if it's not happening by some reason, that's ok, I can just save another publicKey for the encryption using.

Thanks again for your time,
David

@JBaczuk
Copy link
Contributor

JBaczuk commented Jun 17, 2020

I'm not sure I understand the problem, if you already have the private key, why not just generate the public key? It's deterministic. Are you sure that pem public key is correct?

@zcqzcg
Copy link
Author

zcqzcg commented Jun 24, 2020

Now I have a pair of ecdsa keys;

and if I want to use the ecc encrypt/decrypt, I will have to generate a new ecc-publicKey from the privateKey , which I already have a ecdsa-publicKey in hand.

So now I have ecdsaPrivateKey, ecdsaPublicKey and eccPublicKey.

Can I just use the ecdsaPublicKey in ecc crypto instead of generating a new eccPub?

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

No branches or pull requests

2 participants