Created
September 30, 2021 19:56
-
-
Save tychobrailleur/efdb8a17fe4a19eb37a611f94ab47f37 to your computer and use it in GitHub Desktop.
Weak key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
○ → 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