Cheng Tang | c92b3ea | 2012-07-02 17:29:24 +0800 | [diff] [blame] | 1 | #! /usr/bin/env python
|
| 2 | # vim: tabstop=4
|
| 3 |
|
| 4 | import pyDes, codecs
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 | def desencrypt(key, data, m=pyDes.ECB):
|
| 9 | k = pyDes.des(key, pad=None, padmode=pyDes.PAD_NORMAL, mode=m)
|
| 10 | return k.encrypt(data)
|
| 11 |
|
| 12 | def desdecrypt(key, data, m=pyDes.ECB):
|
| 13 | k = pyDes.des(key, pad=None, padmode=pyDes.PAD_NORMAL, mode=m)
|
| 14 | return k.decrypt(data)
|
| 15 |
|
| 16 | def tripledesencrypt(key, data, m=pyDes.ECB):
|
| 17 | k = pyDes.triple_des(key, pad=None, padmode=pyDes.PAD_NORMAL, mode=m)
|
| 18 | return k.encrypt(data)
|
| 19 |
|
| 20 | def tripledesdecrypt(key, data, m=pyDes.ECB):
|
| 21 | k = pyDes.triple_des(key, pad=None, padmode=pyDes.PAD_NORMAL, mode=m)
|
| 22 | return k.decrypt(data)
|
| 23 |
|
| 24 |
|
| 25 | def desencrypthex(key, data):
|
| 26 | k = pyDes.des(codecs.decode(key, 'hex'), pad=None, padmode=pyDes.PAD_NORMAL)
|
| 27 | e = k.encrypt(codecs.decode(data, 'hex'))
|
| 28 | return codecs.encode(e, 'hex')
|
| 29 |
|
| 30 | def tripledesencrypthex(key, data):
|
| 31 | k = pyDes.triple_des(codecs.decode(key, 'hex'), pad=None, padmode=pyDes.PAD_NORMAL)
|
| 32 | e1 = k.encrypt(codecs.decode(data, 'hex'))
|
| 33 | return codecs.encode(e1, 'hex')
|
| 34 |
|
| 35 | def DataNot(data):
|
| 36 | r = ''
|
| 37 | for a in data:
|
| 38 | r = r + chr((~ord(a)) & 0xFF)
|
| 39 | return r
|
| 40 |
|
| 41 | def PadCardPhyNo(phyno):
|
| 42 | r = ''
|
| 43 | if len(phyno) < 8:
|
| 44 | pad = "\x80\x00\x00\x00\x00\x00\x00\x00"
|
| 45 | l = 8 - len(phyno)
|
| 46 | r = phyno + pad[:l]
|
| 47 | elif len(phyno) == 8:
|
| 48 | r = phyno
|
| 49 | else:
|
| 50 | r = phyno[:8]
|
| 51 | return r
|
| 52 |
|
| 53 | def PadCardPhyNoHex(phyno):
|
| 54 | return codecs.encode(PadCardPhyNo(codecs.decode(phyno, 'hex')), 'hex')
|
| 55 |
|
| 56 | def PbocDeliveryKey(factor, key):
|
| 57 | cipherdatanot = ''
|
| 58 | cipherdata = PadCardPhyNo(factor)
|
| 59 |
|
| 60 | print "factor is [%s]" % codecs.encode(cipherdata, 'hex')
|
| 61 |
|
| 62 | if len(key) == 8: # singledes delivery
|
| 63 | k1 = desencrypt(key, cipherdata)
|
| 64 | return k1
|
| 65 | elif len(key) == 16:
|
| 66 | cipherdatanot = DataNot(cipherdata)
|
| 67 |
|
| 68 | k1 = tripledesencrypt(key, cipherdata)
|
| 69 | k2 = tripledesencrypt(key, cipherdatanot)
|
| 70 |
|
| 71 | return k1 + k2
|
| 72 | else:
|
| 73 | raise ValueError('key length error')
|
| 74 |
|
| 75 | def PbocDeliveryKeyHex(factor, key):
|
| 76 | f = codecs.decode(factor, 'hex')
|
| 77 | k = codecs.decode(key, 'hex')
|
| 78 | k1 = PbocDeliveryKey(f, k)
|
| 79 | return codecs.encode(k1, 'hex')
|
| 80 |
|
| 81 |
|
| 82 | def CalcMac3DES(data, initdata, key):
|
| 83 | datalen = len(data)
|
| 84 | k = pyDes.des(key[:8], pad=None, padmode=pyDes.PAD_NORMAL)
|
| 85 |
|
| 86 | for i in range(datalen / 8):
|
| 87 | m = ""
|
| 88 | for j in range(len(initdata)):
|
| 89 | m = m + chr(ord(initdata[j]) ^ ord(data[i * 8 + j]))
|
| 90 |
|
| 91 | initdata = m
|
| 92 | x = k.encrypt(initdata)
|
| 93 | initdata = x
|
| 94 |
|
| 95 |
|
| 96 | k1 = pyDes.des(key[8:], pad=None, padmode=pyDes.PAD_NORMAL)
|
| 97 | n = k1.decrypt(initdata)
|
| 98 | initdata = k.encrypt(n)
|
| 99 | return initdata
|
| 100 |
|
| 101 | def CalcMac3DESHex(data, initdata, key):
|
| 102 | d = codecs.decode(data, 'hex')
|
| 103 | id = codecs.decode(initdata, 'hex')
|
| 104 | k = codecs.decode(key, 'hex')
|
| 105 | k1 = CalcMac3DES(d, id, k)
|
| 106 | return codecs.encode(k1, 'hex')
|
| 107 |
|
| 108 | def CalcMacDES(data, initdata, key):
|
| 109 | datalen = len(data)
|
| 110 | k = pyDes.des(key, pad=None, padmode=pyDes.PAD_NORMAL)
|
| 111 |
|
| 112 | for i in range(datalen / 8):
|
| 113 | m = ""
|
| 114 | for j in range(len(initdata)):
|
| 115 | m = m + chr(ord(initdata[j]) ^ ord(data[i * 8 + j]))
|
| 116 |
|
| 117 | initdata = m
|
| 118 | x = k.encrypt(initdata)
|
| 119 | initdata = x
|
| 120 |
|
| 121 | return initdata
|
| 122 |
|
| 123 | def CalcMacDESHex(data, initdata, key):
|
| 124 | d = codecs.decode(data, 'hex')
|
| 125 | id = codecs.decode(initdata, 'hex')
|
| 126 | k = codecs.decode(key, 'hex')
|
| 127 | k1 = CalcMacDES(d, id, k)
|
| 128 | return codecs.encode(k1, 'hex')
|
Cheng Tang | c92b3ea | 2012-07-02 17:29:24 +0800 | [diff] [blame] | 129 |
|
Tang Cheng | 1aa15ee | 2012-09-06 10:12:23 +0800 | [diff] [blame^] | 130 |
|
| 131 | def encrypt_m1key(inputkey):
|
| 132 | statickey = codecs.decode('3230303530313331', 'hex')
|
| 133 | key = codecs.decode(inputkey[:16], 'hex')
|
| 134 | outkey = []
|
| 135 | for i in range(8):
|
| 136 | t1 = ord(key[i])
|
| 137 | t2 = ord(statickey[i])
|
| 138 | t = ((~t1) ^ t2) & 0xFF
|
| 139 | outkey.append(chr(t))
|
| 140 | return codecs.encode(''.join(outkey), 'hex')
|