您的位置:首页 > 编程语言 > Python开发

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

2015-05-07 18:15 555 查看
这个要借助Crypto.Cipher这个插件来实现的,引用后只需要写如下代码即可

from Crypto.Cipher import DES

class MyDESCrypt:

key = chr(11)+chr(11)+chr(11)+chr(11)+chr(11)+chr(11)+chr(11)+chr(11)
iv = chr(22)+chr(22)+chr(22)+chr(22)+chr(22)+chr(22)+chr(22)+chr(22)

def __init__(self,key='',iv=''):
if len(key)> 0:
self.key = key
if len(iv)>0 :
self.iv = iv

def ecrypt(self,ecryptText):
try:
cipherX = DES.new(self.key, DES.MODE_CBC, self.iv)
pad = 8 - len(ecryptText) % 8
padStr = ""
for i in range(pad):
padStr = padStr + chr(pad)
ecryptText = ecryptText + padStr
x = cipherX.encrypt(ecryptText)
return x.encode('hex_codec').upper()
except:
return ""

def decrypt(self,decryptText):
try:

cipherX = DES.new(self.key, DES.MODE_CBC, self.iv)
str = decryptText.decode('hex_codec')
y = cipherX.decrypt(str)
return y[0:ord(y[len(y)-1])*-1]
except:
return ""
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: