我是靠谱客的博主 甜甜芹菜,这篇文章主要介绍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密集型,所以采用多进程方式加快速度

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- 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)
复制代码
1
2
3
4
5
6
r9d+NZNW48poelbNtY/hQA== I LOVE U! {'我爱你': 'YU8GX81bLakEK6H1dHRCYA==', '他爱你': '7QO7vzKHSroEK6H1dHRCYA==', '谁是你的最爱呢': 'vhp8USlnUPG4HJ2zv3sxtYirbZSeim3W', '?': 'rxqx654m5h8='} ['我爱你', '他爱你', '谁是你的最爱呢', '?'] Process finished with exit code 0

最后

以上就是甜甜芹菜最近收集整理的关于python3.10下采用多进程DES+ECB+PKCS5的全部内容,更多相关python3内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部