Skip to content

Instantly share code, notes, and snippets.

@tychobrailleur
Created September 30, 2021 19:56
Show Gist options
  • Save tychobrailleur/efdb8a17fe4a19eb37a611f94ab47f37 to your computer and use it in GitHub Desktop.
Save tychobrailleur/efdb8a17fe4a19eb37a611f94ab47f37 to your computer and use it in GitHub Desktop.
Weak key
from Crypto.Cipher import DES
from bitarray import *
def int_to_nibble(i):
bnr = bin(i).replace('0b','')
x = bnr[::-1]
while len(x) < 4:
x += '0'
bnr = x[::-1]
return bnr
def str_to_nibbles(s):
arr = list(map(int_to_nibble, list(map(int, list(s)))))
return arr
def nibble_to_bytes(n):
return int(n).to_bytes(2, 'big')
# As described by https://academic.csuohio.edu/yuc/security/Chapter_06_Data_Encription_Standard.pdf
key = bitarray(''.join(str_to_nibbles('0101010101010101'))).tobytes()
plaintext = bitarray(''.join(str_to_nibbles('1234567887654321'))).tobytes()
cipher = DES.new(key, DES.MODE_ECB)
msg = cipher.encrypt(plaintext)
print(plaintext)
print(msg)
print(cipher.encrypt(msg))
○ → python des.py
b'\x124Vx\x87eC!'
b'\x81O\xe98X\x91T\xf7'
b'\x124Vx\x87eC!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment