python 校验 BLE resolvable private address

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @author: david
# pip install pycrypto‎demo

from Crypto.Cipher import AES

def bytesToHexString(bs):
# hex_str = ''
# for item in bs:
# hex_str += str(hex(item))[2:].zfill(2).upper() + " "
# return hex_str
return ''.join(['%02X ' % b for b in bs])

def hexStringTobytes(str):
str = str.replace(" ", "")
return bytes.fromhex(str)
# return a2b_hex(str)

def stringTobytes(str):
return bytes(str,encoding='utf8')

def bytesToString(bs):
return bytes.decode(bs,encoding='utf8')

if __name__ == '__main__':

           #uint8_t  irk[16]={

           #        0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x12

           #};
           #注意key的顺序,高位在前

    mac = '69C9A7FCA4D0'
key = '12111111111111111111111111111111'
plaintext = '00000000000000000000000000'+ mac[0:6]
prand = mac[0:6]
hash = mac[6:]
hexstr_key = hexStringTobytes(key)
hexstr_plaintext = hexStringTobytes(plaintext)
cryptor = AES.new(hexstr_key,AES.MODE_ECB)
ciphertext = cryptor.encrypt(hexstr_plaintext)
# print(bytesToHexString(ciphertext))
cryptor_prand = bytesToHexString(ciphertext)[39:].replace(" ", "")
print('MAC:'+mac)
print('prand = ' + prand)
print('hash = ' + hash)
print('encrypt = ' + cryptor_prand)
if( hash == cryptor_prand):
print('mac pass')

posted @ 2019-01-04 19:29  天道酬勤DW  阅读(414)  评论(0编辑  收藏  举报