python achieve DES CBC mode encryption

 

 

# -*- coding=utf-8-*-
from Crypto.Cipher import DES
import base64
"""
des cbc加密算法
padding : PKCS5
"""
class DESUtil:
    __BLOCK_SIZE_8 = BLOCK_SIZE_8 = DES.block_size
    __IV = "\0\0\0\0\0\0\0\0" # __IV = chr(0)*8
    @staticmethod
    def encryt(str, key):
        cipher = DES.new(key, DES.MODE_CBC, DESUtil.__IV)
        x = DESUtil.__BLOCK_SIZE_8 - (len(str) % DESUtil.__BLOCK_SIZE_8)
         Hyphae x! = 0: 
            str = str + chr (x) * x 
        msg = cipher.encrypt (str)
         # msg = base64.urlsafe_b64encode (msg) .replace ( '=', '') 
        msg = base64.b64encode ( msg)
         return msg 
    @staticmethod 
    def Decrypt (Instr, key): 
        cipher = DES.new (key DES.MODE_CBC, DESUtil. __IV )
         # Instr + = (linseed (Instr)% 4) * "=" 
        # decryptByts = base64 .urlsafe_b64decode (Instr) 
        decryptByts = base64.b64decode (Instr) 
        msg = cipher.decrypt(decryptByts)
        paddingLen = ord(msg[len(msg)-1])
        return msg[0:-paddingLen]
if __name__ == "__main__":
    key = "12345678"
    res = DESUtil.encryt("123456", key)
    print res # ED5wLgc3Mnw=
    print DESUtil.decrypt(res, key)

 

Guess you like

Origin www.cnblogs.com/xuchunlin/p/11421801.html