Python DES 加密解密,就是大家所謂想要的那個非常快速的方法


這個要借助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 ""

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM