Skip to content

Instantly share code, notes, and snippets.

@lrks

lrks/main.java Secret

Created April 14, 2019 17:51
Show Gist options
  • Save lrks/a4096dc3eb3bcfe45c99bcf099b1318a to your computer and use it in GitHub Desktop.
Save lrks/a4096dc3eb3bcfe45c99bcf099b1318a to your computer and use it in GitHub Desktop.
import java.util.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.nio.ByteOrder;
import java.nio.ByteBuffer;
public class Main {
static class Barista {
public byte[] encryptedMachinePassword = "0000000000000000".getBytes();
public byte[] encryptedToken = "0000000000000000".getBytes();
public byte[] encryptionKey = "0000000000000000".getBytes();
private byte[] encdec(byte[] data, int param) {
Cipher instance = Cipher.getInstance(data.length == 16 ? "AES/ECB/NOPADDING" : "AES/ECB/PKCS5Padding");
instance.init(param, new SecretKeySpec(this.encryptionKey, "AES"));
return instance.doFinal(data);
}
public byte[] decrypt(byte[] data) {
return encdec(data, 2);
}
public byte[] encrypt(byte[] data) {
return encdec(data, 1);
}
public byte[] getHibun() {
byte[] machinePassword = decrypt(this.encryptedMachinePassword);
byte[] decryptedToken = decrypt(this.encryptedToken);
byte[] haisyo = new byte[16];
for (int i = 0; i < 16; i++) {
haisyo[i] = (byte) ((machinePassword[i] ^ decryptedToken[i]) & 0xFF);
}
return haisyo;
}
public byte[] str2hex(String str) {
return DatatypeConverter.parseHexBinary(str.replaceAll(" ",""));
}
public String hex2str(byte[] data) {
return DatatypeConverter.printHexBinary(data);
}
public byte[] getTime() {
byte[] rand = new byte[4];
new Random().nextBytes(rand);
int time = (int)(new Date().getTime() / 1000);
byte[] data = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(time).array();
return (encrypt(new byte[]{data[0], data[1], data[2], data[3], rand[0], rand[1], rand[2], rand[3]}));
}
public byte[] getButtonCmd(int button) {
byte[] rand = new byte[4];
new Random().nextBytes(rand);
return encrypt(new byte[]{(byte)((button >> 8) & 0xFF), (byte)(button & 0xFF), rand[0], rand[1], rand[2], rand[3]});
}
}
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
Barista barista = new Barista();
// lightpairing
System.out.println("char-read-hnd 0x0028");
System.out.println("char-read-hnd 0x0030");
System.out.print("Return Value> ");
barista.encryptedToken = barista.str2hex(scan.nextLine());
System.out.println("char-write-req 0x0030 " + barista.hex2str(barista.encrypt(barista.getHibun())));
System.out.println("char-write-req 0x0030 " + barista.hex2str("WE START PAIRING".getBytes("US-ASCII")));
System.out.println("char-read-hnd 0x0030");
System.out.println("touch LED");
System.out.print("Return Value> ");
barista.encryptedMachinePassword = barista.str2hex(scan.nextLine());
System.out.println("char-read-hnd 0x0030");
System.out.print("Return Value> ");
barista.encryptionKey = barista.str2hex(scan.nextLine());
System.out.println("char-write-req 0x0030 " + barista.hex2str(barista.encrypt(barista.getHibun())));
// payload
while (true) {
System.out.println("---");
int[] buttons = {1, 2, 4, 8, 16, 32, 64, 256};
for (int button: buttons) {
byte[] payload = barista.getButtonCmd(button);
System.out.println("button " + button + ": char-write-req 0x002e " + barista.hex2str(payload));
}
System.out.print("re-Generate?");
scan.nextLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment