这个要借助Crypto.Cipher这个插件来实现的,引用后只需要写如下代码即可
1 from Crypto.Cipher import DES 2 3 class MyDESCrypt: 4 5 key = chr(11)+chr(11)+chr(11)+chr(11)+chr(11)+chr(11)+chr(11)+chr(11) 6 iv = chr(22)+chr(22)+chr(22)+chr(22)+chr(22)+chr(22)+chr(22)+chr(22) 7 8 def __init__(self,key='',iv=''): 9 if len(key)> 0: 10 self.key = key 11 if len(iv)>0 : 12 self.iv = iv 13 14 def ecrypt(self,ecryptText): 15 try: 16 cipherX = DES.new(self.key, DES.MODE_CBC, self.iv) 17 pad = 8 - len(ecryptText) % 8 18 padStr = "" 19 for i in range(pad): 20 padStr = padStr + chr(pad) 21 ecryptText = ecryptText + padStr 22 x = cipherX.encrypt(ecryptText) 23 return x.encode('hex_codec').upper() 24 except: 25 return "" 26 27 28 def decrypt(self,decryptText): 29 try: 30 31 cipherX = DES.new(self.key, DES.MODE_CBC, self.iv) 32 str = decryptText.decode('hex_codec') 33 y = cipherX.decrypt(str) 34 return y[0:ord(y[len(y)-1])*-1] 35 except: 36 return ""