blob: f21bf376e0b058ced93558f07b98ba129aed18bf [file] [log] [blame]
Cheng Tangc92b3ea2012-07-02 17:29:24 +08001#! /usr/bin/env python
2# vim: tabstop=4
3
4import pyDes, codecs
5
6
7
8def 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
12def 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
16def 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
20def 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
25def 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
30def 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
35def DataNot(data):
36 r = ''
37 for a in data:
38 r = r + chr((~ord(a)) & 0xFF)
39 return r
40
41def 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
53def PadCardPhyNoHex(phyno):
54 return codecs.encode(PadCardPhyNo(codecs.decode(phyno, 'hex')), 'hex')
55
56def 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
75def 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
82def 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
101def 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
108def 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
123def 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')
129
130
131