z-hao-wang / react-native-rsa

React native rsa crypto
MIT License
68 stars 19 forks source link

How can I set the public key form a string which is the 'pkcs1-public-der'? #9

Open hugomosh opened 7 years ago

hugomosh commented 7 years ago

I have the public key from a redbear/Duo an it is in pkcs1-public-der The Duo Documentation says:

Response string (e.g.) : {"b":"ascii hex-encoded data","r":0} // 0 ok, non zero problem with index/data The device's public key is in DER format.

And I need to do with react-native-rsa this, instead the node-rsa

var RSA = require('node-rsa');
...
SoftAP.prototype.publicKey = function publicKey(cb) {
    is(cb);
    this.__sendCommand({ name: 'public-key' }, function response(err, dat) {
        checkResponse(err, dat, cb);
        var buff = new Buffer(dat.b, 'hex');
        this.__publicKey = new RSA(buff.slice(22), 'pkcs1-public-der', {
            encryptionScheme: 'pkcs1'
        });
        cb(null, this.__publicKey.exportKey('pkcs8-public'));
    }.bind(this));
};

It is my first time using keys, I will appreciate any help. Thanks

z-hao-wang commented 7 years ago

looks like you need to find some way to extra raw rsa components from der format.

This lib doesn't do key format import or export. It mainly support encrypt & decrypt using raw keys.

Hao Wang

On Jul 20, 2017, at 11:57 AM, Hugo Mosh Cardoza notifications@github.com<mailto:notifications@github.com> wrote:

I have the public key from a redbear/Duo an it is in pkcs1-public-der The Duo Documentation says:

Response string (e.g.) : {"b":"ascii hex-encoded data","r":0} // 0 ok, non zero problem with index/data The device's public key is in DER format.

And I need to do with react-native-rsa this, instead the node-rsa

var RSA = require('node-rsa'); ... SoftAP.prototype.publicKey = function publicKey(cb) { is(cb); this.sendCommand({ name: 'public-key' }, function response(err, dat) { checkResponse(err, dat, cb); var buff = new Buffer(dat.b, 'hex'); this.publicKey = new RSA(buff.slice(22), 'pkcs1-public-der', { encryptionScheme: 'pkcs1' }); cb(null, this.__publicKey.exportKey('pkcs8-public')); }.bind(this)); };

It is my first time using keys, I will appreciate any help. Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/z-hao-wang/react-native-rsa/issues/9, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABjVgwJaLQqw8Y5-zYUMbkHsOe_G-xelks5sP6MegaJpZM4Oeh7a.

hugomosh commented 7 years ago

Thanks Yes we ended up using ASN1 js library to obtain the sequence of n and exp El El jue, 20 de julio de 2017 a la(s) 15:52, hao-wang < notifications@github.com> escribió:

looks like you need to find some way to extra raw rsa components from der format.

This lib doesn't do key format import or export. It mainly support encrypt & decrypt using raw keys.

Hao Wang

On Jul 20, 2017, at 11:57 AM, Hugo Mosh Cardoza <notifications@github.com mailto:notifications@github.com> wrote:

I have the public key from a redbear/Duo an it is in pkcs1-public-der The Duo Documentation says:

Response string (e.g.) : {"b":"ascii hex-encoded data","r":0} // 0 ok, non zero problem with index/data The device's public key is in DER format.

And I need to do with react-native-rsa this, instead the node-rsa

var RSA = require('node-rsa'); ... SoftAP.prototype.publicKey = function publicKey(cb) { is(cb); this.sendCommand({ name: 'public-key' }, function response(err, dat) { checkResponse(err, dat, cb); var buff = new Buffer(dat.b, 'hex'); this.publicKey = new RSA(buff.slice(22), 'pkcs1-public-der', { encryptionScheme: 'pkcs1' }); cb(null, this.__publicKey.exportKey('pkcs8-public')); }.bind(this)); };

It is my first time using keys, I will appreciate any help. Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub< https://github.com/z-hao-wang/react-native-rsa/issues/9>, or mute the thread< https://github.com/notifications/unsubscribe-auth/ABjVgwJaLQqw8Y5-zYUMbkHsOe_G-xelks5sP6MegaJpZM4Oeh7a>.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/z-hao-wang/react-native-rsa/issues/9#issuecomment-316826525, or mute the thread https://github.com/notifications/unsubscribe-auth/AA21evwdyzyMpMA-Hpyr5UptX-XLzboEks5sP74egaJpZM4Oeh7a .

lukasklein commented 6 years ago

Hey @hugomosh I'm currently faced with the exact same problem. Would you mind sharing some information how you got it to work for you?

wilcoxmd commented 5 years ago

@hugomosh @lukasklein hey guys, I'm a bit stuck on this too. Any help would be appreciated. I just keep getting Invalid Key messages from this library and need to figure out how to extract the raw keys. I'm also getting some errors about 'assert' not existing in the Haste module map when using the asn1 package.

Did you find a specific package that worked?

wilcoxmd commented 5 years ago

@hugomosh I finally came across the asn1js package (NOT just asn1) and realized this is the one you probably used? It seems to deal with BER any way.

Do you mind sharing any samples of how you used it in conjunction with this library?

wilcoxmd commented 5 years ago

circling back here in case someone finds this later...

I was able to figure out the location of the modulus and exponent using an online decoder, and then wrote the following function to grab those values and pass them to this module:

encryptPassword(derKey, password) {
    // derKey is the device public key, which is in DER format
    // react-native-rsa needs to take in a public key object with
    // with members n = modulus, and e = exponent
    // DER key modulus is encoded at hex bytes 28 - 156. We're consuming a hex number string here, so we have to double to go to proper spot.
    // standard exponent is 0x10001, actual exponnet located starting at index 159 and ending at 162
    // both modulus and exponent need to be passed in as hex strings
    // for more info on decoding DER keys - see this post:
    // https://crypto.stackexchange.com/a/35105
    const keyModString = derKey.slice(28 * 2, 157 * 2);
    const keyExpString = derKey.slice(159 * 2, 162 * 2);
    let rsa = new RSAKey();
    const publicKey = {
      n: keyModString,
      e: keyExpString
    };
    publicKeyString = JSON.stringify(publicKey);
    rsa.setPublicString(publicKeyString); //expects JSON string object with modulus field (n) and exponent field (e)
    let encryptedPassword = rsa.encrypt(password);
    return encryptedPassword;
}