Skip to content

Instantly share code, notes, and snippets.

@ak64th
Last active September 10, 2019 10:22
Show Gist options
  • Save ak64th/547f5576bf3e54a0b3187ad900cb7527 to your computer and use it in GitHub Desktop.
Save ak64th/547f5576bf3e54a0b3187ad900cb7527 to your computer and use it in GitHub Desktop.
Use Triple DES with python. Package `cryptography` or `pyDes` is required.
import base64
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.padding import PKCS7
backend = default_backend()
ct = b'iWdEgY1YmHSjoW13UIQ52w==' # base64 encoded ciphertext
secret = b'olQFK9LzdoGnPcwBcBhvy8rG'
iv = b'20190910'
block_size = len(iv) * 8
padding = PKCS7(block_size)
cipher = Cipher(algorithms.TripleDES(secret), modes.CBC(iv), backend=backend)
decryptor = cipher.decryptor()
decrypted = decryptor.update(base64.b64decode(ct)) + decryptor.finalize()
unpadder = padding.unpadder()
unpadded = unpadder.update(decrypted) + unpadder.finalize()
print(unpadded.decode())
import base64
import pyDes
ct = b'iWdEgY1YmHSjoW13UIQ52w==' # base64 encoded ciphertext
secret = b'olQFK9LzdoGnPcwBcBhvy8rG'
iv = b'20190910'
k_cbc = pyDes.triple_des(secret, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5)
data = k_cbc.decrypt(base64.b64decode(ct)).decode()
print(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment