加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 实现 DES CBC模式加解密

发布时间:2020-12-20 10:51:22 所属栏目:Python 来源:网络整理
导读:? ? # -*- 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 = " " # __IV = chr(0)*8 @staticmethod def encr

?

?

# -*- 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 = "" # __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)
        if 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(enStr,DESUtil.__IV)
        # enStr += (len(enStr) % 4)*"="
        # decryptByts = base64.urlsafe_b64decode(enStr)
        decryptByts = base64.b64decode(enStr)
        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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读