@@ -76,6 +76,8 @@ public abstract class BLDevice implements Closeable {
76
76
public static final byte [] INITIAL_IV = { 0x56 , 0x2e , 0x17 , (byte ) 0x99 , 0x6d , 0x09 , 0x3d , 0x28 , (byte ) 0xdd ,
77
77
(byte ) 0xb3 , (byte ) 0xba , 0x69 , 0x5a , 0x2e , 0x6f , 0x58 }; // 16-short
78
78
79
+ public static final int DEFAULT_BYTES_SIZE = 0x38 ; // 56-bytes
80
+
79
81
// Devices type HEX
80
82
81
83
public static final short DEV_SP1 = 0x0 ;
@@ -255,7 +257,12 @@ public abstract class BLDevice implements Closeable {
255
257
* implementation to handle MAC addresses
256
258
*/
257
259
private Mac mac ;
258
-
260
+
261
+ /**
262
+ * Global AES decrytpor
263
+ */
264
+ private static AES aes = null ;
265
+
259
266
/**
260
267
* Constructs a <code>BLDevice</code>, with a device type (constants),
261
268
* hostname and MAC address
@@ -276,9 +283,7 @@ protected BLDevice(short deviceType, String deviceDesc, String host, Mac mac) th
276
283
iv = INITIAL_IV ;
277
284
id = new byte [] { 0 , 0 , 0 , 0 };
278
285
279
- pktCount = 50000 ;
280
- // pktCount = new Random().nextInt(0xffff);
281
- // pktCount = 0;
286
+ pktCount = new Random ().nextInt (0xffff );
282
287
283
288
this .deviceType = deviceType ;
284
289
this .deviceDesc = deviceDesc ;
@@ -289,6 +294,7 @@ protected BLDevice(short deviceType, String deviceDesc, String host, Mac mac) th
289
294
sock = new DatagramSocket ();
290
295
sock .setReuseAddress (true );
291
296
sock .setBroadcast (true );
297
+ aes = new AES (iv , key );
292
298
}
293
299
294
300
/**
@@ -326,25 +332,11 @@ public Mac getMac() {
326
332
return mac ;
327
333
}
328
334
329
- /**
330
- * Returns the encryption IV of this client
331
- *
332
- * @return a byte array containing the IV
333
- */
334
- public byte [] getIv () {
335
- return iv ;
336
- }
337
-
338
- /**
339
- * Returns the encryption key of this client
340
- *
341
- * @return a byte array containing the key
342
- */
343
- public byte [] getKey () {
344
- return key ;
345
- }
335
+ public static AES getAes () {
336
+ return aes ;
337
+ }
346
338
347
- /**
339
+ /**
348
340
* Returns a friendly description of this BLDevice
349
341
* @return a String
350
342
*/
@@ -381,39 +373,13 @@ public boolean auth() throws IOException {
381
373
return false ;
382
374
}
383
375
384
- log .debug ("auth recv data bytes (" + data .length +") after initial req: {}" , DatatypeConverter .printHexBinary (data ));
385
-
386
- log .debug ("auth Getting encrypted data from 0x38 to the end" );
387
-
388
- byte [] encData = subbytes (data , 0x38 , data .length );
389
-
390
- log .debug ("auth encDataLen=" + encData .length );
391
-
392
- byte [] newBytes = null ;
393
- if (encData .length > 0 ) {
394
- int numpad = encData .length % 16 ;
395
- if (numpad == 0 )
396
- numpad = 16 ;
397
- newBytes = new byte [encData .length +numpad ];
398
- for (int i = 0 ; i < newBytes .length ; i ++) {
399
- if (i < encData .length )
400
- newBytes [i ] = encData [i ];
401
- else
402
- newBytes [i ] = 0x00 ;
403
- }
404
- }
405
-
406
- log .debug ("auth padded encoded bytes from initial req: {}" , DatatypeConverter .printHexBinary (newBytes ));
407
-
408
- log .debug ("auth Creating AES instance with initial iv, key" );
409
-
410
- AES aes = new AES (INITIAL_IV , INITIAL_KEY );
376
+ log .debug ("auth recv encrypted data bytes (" + data .length +") after initial req: {}" , DatatypeConverter .printHexBinary (data ));
411
377
412
378
byte [] payload = null ;
413
379
try {
414
380
log .debug ("auth Decrypting encrypted data" );
415
381
416
- payload = aes . decrypt ( newBytes );
382
+ payload = decryptFromDeviceMessage ( data );
417
383
418
384
log .debug ("auth Decrypted. len=" + payload .length );
419
385
@@ -435,6 +401,8 @@ public boolean auth() throws IOException {
435
401
return false ;
436
402
}
437
403
404
+ aes = new AES (iv , key );
405
+
438
406
log .debug ("auth Getting ID from 0x00 to 0x04" );
439
407
440
408
id = subbytes (payload , 0x00 , 0x04 );
@@ -886,6 +854,36 @@ public static byte[] subbytesTillNull(byte[] data, int offset) {
886
854
return out ;
887
855
}
888
856
857
+ /**
858
+ * Get Payload without header and padded for decryption.
859
+ *
860
+ * @param data the encrypted data message from the device and includes the header
861
+ * @return Payload bytes without the header and padded to modulo 16
862
+ */
863
+ public static byte [] getRawPayloadBytesPadded (byte [] data ) {
864
+ byte [] encData = subbytes (data , BLDevice .DEFAULT_BYTES_SIZE , data .length );
865
+ byte [] newBytes = null ;
866
+ if (encData .length > 0 ) {
867
+ int numpad = encData .length % 16 ;
868
+ if (numpad == 0 )
869
+ numpad = 16 ;
870
+ newBytes = new byte [encData .length +numpad ];
871
+ for (int i = 0 ; i < newBytes .length ; i ++) {
872
+ if (i < encData .length )
873
+ newBytes [i ] = encData [i ];
874
+ else
875
+ newBytes [i ] = 0x00 ;
876
+ }
877
+ }
878
+ return newBytes ;
879
+ }
880
+
881
+ protected static byte [] decryptFromDeviceMessage (byte [] encData ) throws Exception {
882
+ byte [] encPL = getRawPayloadBytesPadded (encData );
883
+ byte [] pl = aes .decrypt (encPL );
884
+
885
+ return pl ;
886
+ }
889
887
/**
890
888
* Picks bytes from start-set to the end-set in a bytes array
891
889
*
0 commit comments