def __sendGA4(self, authToken): header = bytearray([ 0x00, 0x86, 0, 0, len(authToken) + 4, 0x7c, len(authToken) + 2, 0x85, len(authToken) ]) response = self.__transceiveAPDU(list(header + authToken) + [0]) tlv = TLV(['86', '87', '88']) # DO87 and DO88 are optional collection = tlv.parse(binascii.hexlify(response[2:])) if (collection.get('86') != None): DO86 = bytearray.fromhex(collection.get('86')) else: DO86 = None if (collection.get('87') != None): DO87 = bytearray.fromhex(collection.get('87')) else: DO87 = None if (collection.get('88') != None): DO88 = bytearray.fromhex(collection.get('88')) else: DO88 = None return DO86, DO87, DO88
def update_impi(self, impi=None): hex_str = "" if impi: hex_str = s2h(impi) # Build TLV tlv = TLV(['80']) content = tlv.build({'80': hex_str}) bin_size_bytes = self._scc.binary_size(EF_ISIM_ADF_map['IMPI']) data, sw = self._scc.update_binary(EF_ISIM_ADF_map['IMPI'], rpad(content, bin_size_bytes*2)) return sw
def update_impu(self, impu=None): hex_str = "" if impu: hex_str = s2h(impu) # Build TLV tlv = TLV(['80']) content = tlv.build({'80': hex_str}) rec_size_bytes = self._scc.record_size(EF_ISIM_ADF_map['IMPU']) impu_tlv = rpad(content, rec_size_bytes*2) data, sw = self._scc.update_record(EF_ISIM_ADF_map['IMPU'], 1, impu_tlv) return sw
def update_domain(self, domain=None, mcc=None, mnc=None): hex_str = "" if domain: hex_str = s2h(domain) elif mcc and mnc: # MCC and MNC always has 3 digits in domain form plmn_str = 'mnc' + lpad(mnc, 3, "0") + '.mcc' + lpad(mcc, 3, "0") hex_str = s2h('ims.' + plmn_str + '.3gppnetwork.org') # Build TLV tlv = TLV(['80']) content = tlv.build({'80': hex_str}) bin_size_bytes = self._scc.binary_size(EF_ISIM_ADF_map['DOMAIN']) data, sw = self._scc.update_binary(EF_ISIM_ADF_map['DOMAIN'], rpad(content, bin_size_bytes*2)) return sw
def __sendGA4(self, authToken): header = bytearray([ 0x00, 0x86, 0, 0, len(authToken) + 4, 0x7c, len(authToken) + 2, 0x85, len(authToken) ]) response = self.__transceiveAPDU(list(header + authToken) + [0]) tlv = TLV(['86', '87', '88']) collection = tlv.parse(binascii.hexlify(response[2:]).decode('ascii')) tpicc = collection.get('86') if collection.get('86') != None else "" car1 = collection.get('87') if collection.get('87') != None else "" car2 = collection.get('88') if collection.get('88') != None else "" return bytearray.fromhex(tpicc), bytearray.fromhex( car1), bytearray.fromhex(car2)
def server_target(s): try: # 采用循环不断地从socket中读取客户端发送过来的数据 while True: #line = input() data = {"username": "******", "age": 16} jsondata = json.dumps(data) if jsondata is None or jsondata == 'exit': break tlv = TLV(['89ABCDEF', '9F04']) test = '1234567890' print(len(test)) data = tlv.build({'89ABCDEF': test}) print(data) time.sleep(2) #s.send('hello world'.encode('utf-8')) s.send(data.encode('utf-8')) except Exception: print(Exception.with_traceback())
def decode_select_response(resp_hex): fixup_fcp_proprietary_tlv_map(FCP_Proprietary_TLV_MAP) resp_hex = resp_hex.upper() # outer layer fcp_base_tlv = TLV(['62']) fcp_base = fcp_base_tlv.parse(resp_hex) # actual FCP fcp_tlv = TLV(FCP_TLV_MAP) fcp = fcp_tlv.parse(fcp_base['62']) # further decode the proprietary information if fcp['A5']: prop_tlv = TLV(FCP_Proprietary_TLV_MAP) prop = prop_tlv.parse(fcp['A5']) fcp['A5'] = tlv_val_interpret(FCP_prorietary_interpreter_map, prop) fcp['A5'] = tlv_key_replace(FCP_Proprietary_TLV_MAP, fcp['A5']) # finally make sure we get human-readable keys in the output dict r = tlv_val_interpret(FCP_interpreter_map, fcp) return tlv_key_replace(FCP_TLV_MAP, r)
def _decode_record_hex(self, raw_hex_data): raw_hex_data = raw_hex_data.upper() atempl_base_tlv = TLV(['61']) atempl_base = atempl_base_tlv.parse(raw_hex_data) atempl_TLV_MAP = {'4F': 'aid_value', 50: 'label'} atempl_tlv = TLV(atempl_TLV_MAP) atempl = atempl_tlv.parse(atempl_base['61']) # FIXME: "All other Dos are according to ISO/IEC 7816-4" return tlv_key_replace(atempl_TLV_MAP, atempl)
class CRT(object): # Supported tags crt_tag = ['B4', 'B8'] key_usage_tag = '95' crypto_mech_tag = '80' key_tag = 'D1' iv_tag = '91' tlv = TLV(crt_tag + [key_usage_tag + key_tag + iv_tag]) def __init__(self, tag, key_use, key_len=16, iv_len=0): self.tag = hex(tag).split('x')[1] self.key_use = hex(key_use).split('x')[1] self.key = '' for _ in range(key_len): k = hex(random.getrandbits(8)).split('x')[1] if len(k) == 1: k += '0' self.key += k self.iv = '' for _ in range(iv_len): k = hex(random.getrandbits(8)).split('x')[1] if len(k) == 1: k += '0' self.iv += k def get_bytes(self): body = self.tlv.build({ self.key_usage_tag: self.key_use, self.key_tag: self.key, self.iv_tag: self.iv }) crt = self.tlv.build({self.tag: body}) return int_to_hex(int(crt, 16), len(crt) // 2) def get_key(self): if self.key == '': return [] return int_to_hex(int(self.key, 16), len(self.key) // 2) def get_iv(self): if self.iv == '': return [] return int_to_hex(int(self.iv, 16), len(self.iv) // 2)
from pytlv.TLV import * import json data = {"Username": "******", "age": 16} jsondata = json.dumps(data) print(jsondata) tlv = TLV(['89ABCDEF', '9F04']) test = '1234567890' + repr(jsondata) print(len(test)) print(test) data = tlv.build({'89ABCDEF': test}) print(data) text = tlv.parse(data) print(text) ''' test1 = 'Ab0000' print(len(test1)) data1 = tlv.build({'9f04': test1 + jsondata}) print(data1) text1 = tlv.parse(data1) print(text1) '''
import socket import threading import pickle import json from pytlv.TLV import * # 创建socket对象 s = socket.socket() # 连接远程主机 s.connect(('localhost', 2333)) tlv = TLV(['89ABCDEF', '9F04']) def read_from_server(s): try: data = s.recv(2048) # test text = tlv.parse(data.decode('utf-8')) print(text) #print(data) #text = json.loads(avp["value"]) #print(text["username"]) #print(text["age"]) return text #return data # 如果捕获到异常,则表明该socket对应的客户端已经关闭 except: print(Exception.with_traceback())
def create_tlv(): return TLV(TLV_TAGS)