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

admin 2017年7月25日14:59:32Python评论1,703 次浏览阅读模式

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 ""

相关文章
weinxin
我的微信
微信扫一扫
admin
  • 本文由 发表于 2017年7月25日14:59:32
  • 转载请务必保留本文链接:https://anyun.org/a/jishuguanzhu/bianchengchalou/python/2017/0725/8714.html

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: