本帖最后由 龙卷风暴 于 2024-4-29 18:16 编辑
[Python] 纯文本查看 复制代码 from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
def E_数据加密_DES(要加密的数据,密码文本):
# e语言特殊的密钥转换
def getnewkey(key):
key = key.encode("utf-8")
newkey = bytearray(8)
for i in range(len(newkey)):
j = i % len(newkey)
newkey[j] = newkey[j] ^ key
for i in range(8):
k = 0
l = newkey
for index in range(8):
k = k << 1 | l & 1
l = l >> 1
newkey = k
return newkey
key = getnewkey(密码文本)
cipher = Cipher(algorithms.TripleDES(key), mode=modes.ECB(), backend=default_backend())
encryptor = cipher.encryptor()
# E语言特殊的数据处理
plaintext = 要加密的数据
plaintext = len(plaintext).to_bytes() + bytes(3) + bytes(plaintext.encode("utf-8")) # 由于python的int到bytes只有一个字节 所以自行填补三个{0,0,0}字节集在后面,但是如果加密文本长度超出255字节尚未处理
bloqueSize = 8
proveerLength = bloqueSize - (len(plaintext) - 1) % bloqueSize + 1
plaintext = plaintext + bytearray(proveerLength)
ciphertext = encryptor.update(plaintext)
return ciphertext
翻译自:
https://bbs.125.la/forum.php?mod=viewthread&tid=14818072
|