IDEA modes: CBC, CFB, OFB, and ECB
[Hazmat Home][Home]
Phil Zimmerman used the IDEA (International Data Encryption Algorithm) cipher to build PGP v2.0, and it is still used as an option for the method. With 3DES, the method is not quite a legacy method, as it is still used in a range of applications (including with TLS 1.2).
|
Coding
The coding is:
import os from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives import padding import sys message="Hello" mode=0 iv = os.urandom(8) if (len(sys.argv)>1): message=str(sys.argv[1]) if (len(sys.argv)>2): mode=int(sys.argv[2]) keysize=16 key = os.urandom(keysize) padder = padding.PKCS7(128).padder() unpadder = padding.PKCS7(128).unpadder() try: cipher = Cipher(algorithms.IDEA(key), modes.CBC(iv)) if (mode==0): cipher = Cipher(algorithms.IDEA(key), modes.CBC(iv)) if (mode==1): cipher = Cipher(algorithms.IDEA(key), modes.OFB(iv)) if (mode==2): cipher = Cipher(algorithms.IDEA(key), modes.CFB(iv)) if (mode==3): cipher = Cipher(algorithms.IDEA(key), modes.ECB()) encryptor = cipher.encryptor() str=padder.update(message.encode())+padder.finalize() ciphertext = encryptor.update(str ) + encryptor.finalize() # Now decrypt decryptor = cipher.decryptor() rtn=unpadder.update(decryptor.update(ciphertext) + decryptor.finalize())+unpadder.finalize() print("Type:\t\t\t",cipher.algorithm.name) print("Mode:\t\t\t",cipher.mode.name) print("Message:\t\t",message) print("Message with padding:\t",str) print("\nKey:\t\t\t",key.hex()) print("IV:\t\t\t",iv.hex()) print("\nCipher:\t\t\t",ciphertext.hex()) print("Decrypt:\t\t",rtn.decode()) except Exception as msg: print(msg)
and a sample run:
Type: IDEA Mode: CBC Message: Hello Message with padding: b'Hello\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b' Key: 77e2bb493a8123b447510ae689f7eddd IV: 0154d1bf8050f8a2 Cipher: d7eb71977072eee3781d08f365576143 Decrypt: Hello