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 can I set the public key form a string which is the 'pkcs1-public-der'? #9

Open
hugomosh opened this issue Jul 20, 2017 · 6 comments

Comments

@hugomosh
Copy link

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
Copy link
Owner

z-hao-wang commented Jul 20, 2017 via email

@hugomosh
Copy link
Author

hugomosh commented Jul 23, 2017 via email

@lukasklein
Copy link

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
Copy link

@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
Copy link

@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
Copy link

wilcoxmd commented Jan 3, 2019

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;
}

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

4 participants