# very simple DES like program (toy version)
# real DES has 16 rounds, 64 bit block, permutation tables and s-boxes
# here only the feistel structure is shown : 4 rounds, 16 bit block

def f(right, key):
    # round function (simple), real DES uses expansion + s-box
    return (right * 3 + key) % 256

def des(block, keys):
    left = block >> 8
    right = block & 255
    for k in keys:
        left, right = right, left ^ f(right, k)
    # swap at the end
    return (right << 8) | left

key = [11, 22, 33, 44]
plain = 0x4865        # 'He'

cipher = des(plain, key)
# decryption = same function with keys in reverse order
back = des(cipher, key[::-1])

print("plain  :", hex(plain))
print("cipher :", hex(cipher))
print("decrypt:", hex(back))
