我是靠谱客的博主 甜甜芹菜,最近开发中收集的这篇文章主要介绍python3.10下采用多进程DES+ECB+PKCS5,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

先安装Cryptodome,据说这个des比pydes快:
C:Program FilesPython310Scripts>pip install pycryptodomex
Collecting pycryptodomex
Downloading pycryptodomex-3.14.0-cp35-abi3-win_amd64.whl (1.8 MB)
|████████████████████████████████| 1.8 MB 60 kB/s
Installing collected packages: pycryptodomex
Successfully installed pycryptodomex-3.14.0
因为DES计算属于CPU密集型,所以采用多进程方式加快速度

# -*- coding: utf-8 -*-
# Cryptodome自带的工具包实现了pkcs7(default),iso7816和x923三种padding算法,pkcs5只需要这样调用:
# from Cryptodome.Util.Padding import pad, unpad
# bytes_padded = pad(bytes, 8, style='pkcs7')
# bytes = unpad(bytes_padded, 8, style='pkcs7')
# 或者参照工具自己写一个类似的函数,网上有的有问题
# 按照标准,pkcs5padding方式是在待加密的字节流后补齐成8bit字节(octets)串,而不是在字符串后直接补齐
from Cryptodome.Cipher import DES
from base64 import b64decode, b64encode
from concurrent.futures import ProcessPoolExecutor
KEY = b'12345678'[0:8]
DES_BLOCK_SIZE = 8
def cryptodome_des_encrypt(plain_text):
pad = lambda s: s + (DES_BLOCK_SIZE-len(s) % DES_BLOCK_SIZE) * bytes([DES_BLOCK_SIZE-len(s) % DES_BLOCK_SIZE])
cipher = DES.new(KEY, DES.MODE_ECB)
plain_bytes = plain_text.encode('utf8')
plain_bytes_padded = pad(plain_bytes)
cipher_bytes = cipher.encrypt(plain_bytes_padded)
# 按个人需求对密文字节进行base64编码
cipher_bytes = b64encode(cipher_bytes)
cipher_text = cipher_bytes.decode('utf8')
return cipher_text, {plain_text: cipher_text}
def cryptodome_des_decrypt(cipher_text):
unpad = lambda s: s[0:-(s[-1])]
cipher = DES.new(KEY, DES.MODE_ECB)
cipher_bytes = cipher_text.encode('utf8')
# 按个人需求对密文字节进行base64反向解码
cipher_bytes = b64decode(cipher_bytes)
plain_bytes_padded = cipher.decrypt(cipher_bytes)
plain_bytes = unpad(plain_bytes_padded)
plain_text = plain_bytes.decode('utf8')
return plain_text
if __name__ == "__main__":
plain_msg = "I LOVE U!"
x, y = cryptodome_des_encrypt(plain_msg)
print(x)
print(cryptodome_des_decrypt(x))
plain_msg = ['我爱你', '他爱你', '谁是你的最爱呢', '?']
cipher_msg_dict = {}
with ProcessPoolExecutor(max_workers=None, mp_context=None, initializer=None, initargs=()) as pool:
cipher_msgs = pool.map(cryptodome_des_encrypt, plain_msg)
for x, y in cipher_msgs:
cipher_msg_dict.update(y)
print(cipher_msg_dict)
plain_msg = [cryptodome_des_decrypt(v) for v in cipher_msg_dict.values()]
print(plain_msg)
r9d+NZNW48poelbNtY/hQA==
I LOVE U!
{'我爱你': 'YU8GX81bLakEK6H1dHRCYA==', '他爱你': '7QO7vzKHSroEK6H1dHRCYA==', '谁是你的最爱呢': 'vhp8USlnUPG4HJ2zv3sxtYirbZSeim3W', '?': 'rxqx654m5h8='}
['我爱你', '他爱你', '谁是你的最爱呢', '?']
Process finished with exit code 0

最后

以上就是甜甜芹菜为你收集整理的python3.10下采用多进程DES+ECB+PKCS5的全部内容,希望文章能够帮你解决python3.10下采用多进程DES+ECB+PKCS5所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部