Skip to content

Instantly share code, notes, and snippets.

@ficapy
Created February 18, 2021 12:11
Show Gist options
  • Select an option

  • Save ficapy/82daf7dccadaf8285bc4894d790d8f3b to your computer and use it in GitHub Desktop.

Select an option

Save ficapy/82daf7dccadaf8285bc4894d790d8f3b to your computer and use it in GitHub Desktop.
from cryptography.hazmat.primitives.ciphers import algorithms, modes, Cipher
from cryptography.hazmat.backends import default_backend
import binascii
# https://github.com/wangyiwy/oktools/blob/133bad6350/static/js/encrypt_des.js
# 参照这个前端网页的Crypto-JS加密结果
# 3DES,ECB,无填充,无iv
# https://www.crypto101.io/
# https://stackoverflow.com/questions/61717485/incorrect-decrypted-string-implemented-using-aes-ecb-nopadding-and-base-64-with
KEY = b'08CC301D'
text = b'1' * 65
cipher = Cipher(algorithms.TripleDES(KEY), modes.ECB(), backend=default_backend())
encryptor = cipher.encryptor()
block_size = algorithms.TripleDES(KEY).block_size
# 应该使用标准方式填充,这样直接使用0填充不科学
pad = lambda s: s + (block_size - len(s) % block_size) * chr(0).encode()
ct = encryptor.update(pad(text))
print(binascii.b2a_hex(ct[:len(text)]))
# b'59be45af24a3428959be45af24a3428959be45af24a3428959be45af24a3428959be45af24a3428959be45af24a3428959be45af24a3428959be45af24a3428953'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment