SoftAP Java Example Help

Hello! I’m trying to create a simple app on Android that allows me to configure my Photon’s WiFi connection. I’m able to get pretty far but I’m stuck on the password encryption. I’ve tried to translate the CLI Node example I’ve seen into Java but with no luck (https://github.com/spark/softap-setup-js/blob/master/softap.js). I know the public key being returned is in PKCS#1 format. So, for example, if I query the Photon using this URL: http://192.168.0.1/public-key, I get the following hex encoded public key:

30819F300D06092A864886F70D010101050003818D003081890281810090F90CE05D6FD39D889850E3E17129F7269A79D3847F9E252F35429BA06DCF7EDF86FB3DA06E30603A32158A25B8719859DB3C72DAED263B85AD36BE70B7FA8B280E26A79A93C34AEEE455074B6A64368D00E0190498A6E9B9E74A643E04690BE52581DA93B1ABF1DFC8AF82D0B07D7599483D10307C82ACB2780090DA26AA3F0203010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

The Node code slices out the first 22 bytes (I’m assuming it’s trying to get to the modulus). Then it passes that into some built-in Node methods to create the RSA public key. I think the exponent in the above string is 0x010001 (65537 decimal). After the exponent there are a ton of zeros (I guess padding for some reason?).

Does anyone have a Java example that can take the hex encoded public key (above) and create an RSAPublicKeySpec. I’d love to see the following Node code translated into Java. Any help is greatly appreciated.

SoftAP.prototype.publicKey = function publicKey(cb) {
	is(cb);
	var sock = this.__sendCommand('public-key', response.bind(this));
	function response(err, dat) {

		if(err) { return cb(err); }
		if(!dat) { return cb(new Error('No data received')); }
		if(dat.r !== 0) {
			return cb(new Error('Received non-zero response code'));
		}
		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'));
	};
	return sock;
};

Hi @jdarden

I am sorry that I cannot answer your Java question directly but I can tell you a bit about the key format you have there. It is easy to check the output by putting the binary data into a file and then base64 encoding that and add the beginning and ending headers like this:

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQ+QzgXW/TnYiYUOPhcSn3Jpp504R/niUvNUKb
oG3Pft+G+z2gbjBgOjIViiW4cZhZ2zxy2u0mO4WtNr5wt/qLKA4mp5qTw0ru5FUHS2pkNo0A4BkE
mKbpuedKZD4EaQvlJYHak7Gr8d/Ir4LQsH11mUg9EDB8gqyyeACQ2iaqPwIDAQAB
-----END PUBLIC KEY-----

Notice that I have deleted all the zeros after the exponent. If your key was signed, that is where the signature would be.

When I run this through openssl, it looks OK to me:

% openssl rsa -pubin -inform PEM -in foo.tmp -noout -text
Public-Key: (1024 bit)
Modulus:
    00:90:f9:0c:e0:5d:6f:d3:9d:88:98:50:e3:e1:71:
    29:f7:26:9a:79:d3:84:7f:9e:25:2f:35:42:9b:a0:
    6d:cf:7e:df:86:fb:3d:a0:6e:30:60:3a:32:15:8a:
    25:b8:71:98:59:db:3c:72:da:ed:26:3b:85:ad:36:
    be:70:b7:fa:8b:28:0e:26:a7:9a:93:c3:4a:ee:e4:
    55:07:4b:6a:64:36:8d:00:e0:19:04:98:a6:e9:b9:
    e7:4a:64:3e:04:69:0b:e5:25:81:da:93:b1:ab:f1:
    df:c8:af:82:d0:b0:7d:75:99:48:3d:10:30:7c:82:
    ac:b2:78:00:90:da:26:aa:3f
Exponent: 65537 (0x10001)

so the modulus starts at 00 90 f9 0c… and runs like you thought to …26 aa 3f. And the exponent is the common 2^16+1.

1 Like

Thanks so much! That gives me a little more information to work with. I can fiddle with the Java. If I get a working example, I’ll post the code for others.

Just so I’ve mentioned it, are you aware that there’s an android SDK which incorporates the set-up module? That might be helpful, depending on what you’re trying to do :slight_smile:

@Moors7 Thank you! I just realized that shortly after I wrote the question. Sheesh, the folks at Particle have done everything for us! Where’s the fun in that? :smile: - l’ll dig into the SDK. I’m sure it has what I’m looking for (and definitely more than I need for what I’m doing). I may still post some code examples for anyone like me who wants to zero in on this specific issue.

Can you please share the source code. I am trying to make a simple android java app to change the wifi credentials to my photon and have got no leads yet…