import base64 import hashlib from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 from Crypto.Cipher import DES3 from Crypto.Util.Padding import pad, unpad def md5(orig: bytes) -> bytes: m = hashlib.md5() m.update(orig) return bytes.fromhex(m.hexdigest()) def base64_encode(orig: bytes) -> str: return base64.b64encode(orig).decode() def base64_decode(b64: str) -> bytes: return base64.b64decode(b64) def rsa_decrypt(cipher_text: bytes, private_key: str) -> str: cipher = PKCS1_v1_5.new(RSA.importKey(private_key)) decrypt_text = cipher.decrypt(cipher_text, b"rsa") return decrypt_text.decode("utf-8") def des3_decrypt(cipher_text: bytes, key: bytes) -> bytes: cipher = DES3.new(key, DES3.MODE_ECB) return unpad(cipher.decrypt(cipher_text), 16) def des3_encrypt(origin_text: bytes, key: bytes) -> bytes: cipher = DES3.new(key, DES3.MODE_ECB) return cipher.encrypt(pad(origin_text, 16))