標籤:
這個要藉助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 = key11 if len(iv)>0 :12 self.iv = iv13 14 def ecrypt(self,ecryptText):15 try:16 cipherX = DES.new(self.key, DES.MODE_CBC, self.iv)17 pad = 8 - len(ecryptText) % 818 padStr = ""19 for i in range(pad):20 padStr = padStr + chr(pad)21 ecryptText = ecryptText + padStr22 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 ""
Python DES 加密解密,就是大家所謂想要的那個非常快速的方法