Skip to content

Instantly share code, notes, and snippets.

@komuw
Last active March 28, 2023 06:35
Show Gist options
  • Save komuw/83ddf9b4ae8f995f15af to your computer and use it in GitHub Desktop.
Save komuw/83ddf9b4ae8f995f15af to your computer and use it in GitHub Desktop.
python DES3(triple DES encryption)
`pip install pycrypto`
from Crypto.Cipher import DES3
from Crypto import Random
key = 'Sixteen byte key'
iv = Random.new().read(DES3.block_size) #DES3.block_size==8
cipher_encrypt = DES3.new(key, DES3.MODE_OFB, iv)
plaintext = 'sona si latine loqueri ' #padded with spaces so than len(plaintext) is multiple of 8
encrypted_text = cipher_encrypt.encrypt(plaintext)
cipher_decrypt = DES3.new(key, DES3.MODE_OFB, iv) #you can't reuse an object for encrypting or decrypting other data with the same key.
cipher_decrypt.decrypt(encrypted_text)
cipher_decrypt.decrypt(encrypted_text) #you cant do it twice
"""
For MODE_ECB, MODE_CBC, and MODE_OFB, plaintext length (in bytes) must be a multiple of block_size.
For MODE_CFB, plaintext length (in bytes) must be a multiple of segment_size/8.
For MODE_CTR, plaintext can be of any length.
For MODE_OPENPGP, plaintext must be a multiple of block_size, unless it is the last chunk of the message.
key size (must be either 16 or 24 bytes long)
"""
"""
https://pythonhosted.org/pycrypto/Crypto.Cipher.DES3-module.html
https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation
"""
@aymenalitaleb
Copy link

@Yilmaz4 this should answer your question: https://crypto.stackexchange.com/a/24666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment