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
"""
@navenchary
Copy link

navenchary commented Aug 16, 2019

Is 3des encryption and des3 encryption are one and the same?

@nautilor
Copy link

they are the same but 3DES/DES3 is considered no more safe, you can find more here

@Yilmaz4
Copy link

Yilmaz4 commented Jul 31, 2021

Wikipedia says a 3DES key can be 56 bits, 112 bits or 168 bits. But in this example, 3DES key can only be 128 bits or 192 bits. Why?

@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