def calc_pinblock(**kw): """ 计算PinBlock """ tmk = kw.get("TMK", "159D86C7C1F779EA29F77A6858E0DA2A") pik = kw.get("PIK", "75CAD854C2E59A5EEDD7CA7410C2C215") pan = kw.get("PAN", "6212142000000000012") passwd = kw.get("passwd", "123456") des3 = pydes.triple_des(unhexlify(tmk)) pinkey = des3.decrypt(unhexlify(pik)) des3 = pydes.triple_des(pinkey) pinblock = unhexlify(('06' + passwd).ljust(16, 'F')) customer_data = unhexlify('0000' + pan[-13:-1]) plain_pin = list(map(lambda x, y: x ^ y, pinblock, customer_data)) return hexlify(des3.encrypt(plain_pin)).decode('latin1').upper()
def encrypt(key, nonce): def derive_key(key, magic): hash1 = hmac.new(key, magic, sha1).digest() hash2 = hmac.new(key, hash1 + magic, sha1).digest() hash3 = hmac.new(key, hash1, sha1).digest() hash4 = hmac.new(key, hash3 + magic, sha1).digest() return hash2 + hash4[0:4] #hm = lambda k, m: hmac.new(k, m, sha1).digest() #lambda k, m: hm(k, hm(k, m) + m) + hm(k, hm(k, hm(k, m)) + m)[0:4] key1 = base64.standard_b64decode(key) key2 = derive_key(key1, "WS-SecureConversationSESSION KEY HASH") key3 = derive_key(key1, "WS-SecureConversationSESSION KEY ENCRYPTION") hash = hmac.new(key2, nonce, sha1).digest() iv = struct.pack("Q", random.getrandbits(8 * 8)) # 8 bytes ciph = pydes.triple_des(key3, pydes.CBC, iv).encrypt(nonce + \ "\x08\x08\x08\x08\x08\x08\x08\x08") blob = struct.pack("<LLLLLLL", 28, pydes.CBC, CALC_3DES, CALG_SHA1, len(iv), len(hash), len(ciph)) + iv + hash + ciph return base64.standard_b64encode(blob)
def encrypt(key, nonce): '''Return a base64 hash for mbi auth policy''' def derive_key(key, magic): hash1 = hmac.new(key, magic, sha1).digest() hash2 = hmac.new(key, hash1 + magic, sha1).digest() hash3 = hmac.new(key, hash1, sha1).digest() hash4 = hmac.new(key, hash3 + magic, sha1).digest() return hash2 + hash4[0:4] #hm = lambda k, m: hmac.new(k, m, sha1).digest() #lambda k, m: hm(k, hm(k, m) + m) + hm(k, hm(k, hm(k, m)) + m)[0:4] key1 = base64.standard_b64decode(key) key2 = derive_key(key1, "WS-SecureConversationSESSION KEY HASH") key3 = derive_key(key1, "WS-SecureConversationSESSION KEY ENCRYPTION") hash_ = hmac.new(key2, nonce, sha1).digest() iv = struct.pack("Q", random.getrandbits(8 * 8)) # 8 bytes ciph = pydes.triple_des(key3, pydes.CBC, iv).encrypt(nonce + \ "\x08\x08\x08\x08\x08\x08\x08\x08") blob = struct.pack("<LLLLLLL", 28, pydes.CBC, CALC_3DES, CALG_SHA1, len(iv), len(hash_), len(ciph)) + iv + hash_ + ciph return base64.standard_b64encode(blob)
def __init__(self): self.three_des = pydes.triple_des(key, mode=pydes.CBC, IV="\0\1\2\3\4\5\6\7", pad=None, padmode=pydes.PAD_PKCS5) saved_file_dir = os.path.expanduser("~") self.saved_file = os.path.join(saved_file_dir, ".dnspod.db") if not os.path.exists(self.saved_file): fp = open(self.saved_file, "w") fp.close()
def calc_mac_cbc(**kw): """ 计算报文MAC值(CBC算法) """ tmk = kw.get("TMK", "159D86C7C1F779EA29F77A6858E0DA2A") mak = kw.get("MAK", "E6218EF29513B143") vec = kw.get("IV", "\x00\x00\x00\x00\x00\x00\x00\x00") mab = kw.get("MAB", None) vec = vec.encode('latin1') mab += ''.rjust(8 - len(mab) % 8, '\x00').encode('latin1') des3 = pydes.triple_des(unhexlify(tmk)) mackey = des3.decrypt(unhexlify(mak)) des = pydes.des(mackey) for i in range(0, len(mab), 8): vec = bytes(list(map(lambda a, b: a ^ b, vec, mab[i:i + 8]))) vec = des.encrypt(vec) return hexlify(vec).upper().decode('latin1')
def calc_mac_ecb(**kw): """ 计算报文MAC值(ECB算法) """ tmk = kw.get("TMK", "159D86C7C1F779EA29F77A6858E0DA2A") mak = kw.get("MAK", "E6218EF29513B143") mab = kw.get("MAB", None) mab += ''.rjust(8 - len(mab) % 8, '\x00').encode('latin1') mab = reduce(lambda x, y: bytes(list(map(lambda a, b: a ^ b, x, y))), [mab[i:i + 8] for i in range(0, len(mab), 8)]) des3 = pydes.triple_des(unhexlify(tmk)) mackey = des3.decrypt(unhexlify(mak)) des = pydes.des(mackey) ret = des.encrypt(hexlify(mab[:4]).upper()) ret = bytes(list(map(lambda x, y: x ^ y, ret, hexlify(mab[4:]).upper()))) ret = des.encrypt(ret) return hexlify(hexlify(ret[:4]).upper()).decode('latin1')