Skip to content

Instantly share code, notes, and snippets.

@therealmik
Created May 12, 2015 05:00
Show Gist options
  • Save therealmik/2adbd9eb848d34886c48 to your computer and use it in GitHub Desktop.
Save therealmik/2adbd9eb848d34886c48 to your computer and use it in GitHub Desktop.
DES(p,k) == ~DES(~p,~k)
#!/usr/bin/python3
from Crypto.Cipher import DES
import os
from binascii import hexlify
def bitwise_not(buf):
return bytes([ (~b) & 0xff for b in buf ])
def print_hex(name, value):
value = str(hexlify(value), "utf-8")
print("{0:s}\t{1:s}".format(name, value))
key = os.urandom(8)
plain = os.urandom(8)
notkey = bitwise_not(key)
notplain = bitwise_not(plain)
cipher = DES.new(key, DES.MODE_ECB).encrypt(plain)
notcipher = DES.new(notkey, DES.MODE_ECB).encrypt(notplain)
print_hex(" Ciphertext", cipher)
print_hex("~Ciphertext", notcipher)
print_hex("|Ciphertext", bytes([ cipher[i] | notcipher[i] for i in range(8)]))
print_hex("&Ciphertext", bytes([ cipher[i] & notcipher[i] for i in range(8)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment