Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Typiqally/55c208f4168c8e624171ebdc8f750b68 to your computer and use it in GitHub Desktop.
Save Typiqally/55c208f4168c8e624171ebdc8f750b68 to your computer and use it in GitHub Desktop.
@ExperimentalUnsignedTypes
fun main() {
val aes = AES()
val bondingKey = "83 69 6c e4 44 fa b0 f1 0b f6 17 af 58 2e 3f e4 54 34 16 36 4c 76 de e7 2c 43 dd 8a 63 ac 0f 49".decodeHex()
println("Bonding key: ${bondingKey.contentToString()}")
val bondingKeyIv = "16 64 ef 48 03 58 ca 84 3c 82 e2 54 6f ff 49 28".decodeHex()
val secretKey = DeviceKey().create("B8:8E:82:3A:17:CB")
println("Secret key (${secretKey.size}): ${secretKey.contentToString()}")
val key = aes.decrypt(bondingKey, secretKey, bondingKeyIv)
println("Key (${key.size}): ${key.contentToString()}")
val messageIv = "97 80 c8 e4 17 05 51 cf 06 06 18 3b 00 00 00 02".decodeHex()
val message = "39 60 cf 6d cd 2d 6f 4c 3e 08 66 ad 66 dd b4 cd b4 a2 7b ab a1 fb b1 e8 78 6d 18 32 d6 30 f0 a4 2c 5f 1a ca 8e 6f ec c8 3e b2 2d 98 ff 43 56 27".decodeHex()
println("Message (${message.size}): ${message.contentToString()}")
val decryptedMessage = aes.decrypt(message, key, messageIv)
val bytes = decryptedMessage
println(bytes.contentToString())
println(bytes.toHexString())
}
fun ByteArray.toHexString(): String = joinToString(separator = "") { eachByte -> "%02x ".format(eachByte) }
fun String.decodeHex(): ByteArray {
val trimmed = this.replace(" ", "")
check(trimmed.length % 2 == 0) { "Must have an even length" }
return trimmed.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment