首页 > 编程语言 >python之DES3加密

python之DES3加密

时间:2023-03-18 11:22:05浏览次数:42  
标签:CBC 加密 python DES3 plaintext cipher MODE BS

安装第三方模块

pip install pycryptodome

代码实现

import base64
from Crypto.Cipher import DES3


def des3(data_string):
    BS = 8
    pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)

    # 3DES的MODE_CBC模式下只有前24位有意义
    key = b'xxxxxxxxxxxxxxxxxxxx'[0:24]
    iv = b'xxxxxxxx'

    plaintext = pad(data_string).encode("utf-8")

    # 使用MODE_CBC创建cipher
    cipher = DES3.new(key, DES3.MODE_CBC, iv)
    result = cipher.encrypt(plaintext)
    return base64.b64encode(result).decode('utf-8')

标签:CBC,加密,python,DES3,plaintext,cipher,MODE,BS
From: https://www.cnblogs.com/pdxt666/p/17229608.html

相关文章