May-05-2018, 12:01 PM
(May-05-2018, 12:03 AM)Hairy_Ape Wrote: Sorry for any confusion but with this being aes 256 the password is 32 bits in length. So we need to figure out the last 8 bits of the password which is why I’m iterating from 00000000 to 99999999.
You are mixing bits and bytes.
The key in a AES256 is... 256 bits that are exactly 32 bytes.
I have done some tests and a script that encrypt a text of ~1500 bytes and after that tries to decode it changing the last byte of the key in the 256 options takes less than a second and almost all the time is lost printing to the stdout.
To help you this is the script that encodes a secret and returns 3 values: the coded text, the nonce and the truncated key (is missing the last byte)
It also prints in the stdout the right solution.
#!/usr/bin/env python3 import os from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend def prepare_secret(msg: bytes): key = os.urandom(32) algo = algorithms.AES(key) # Block size in bytes... I expect nobody will create an algorithm with a # block size not multiplo of 8 sz = algo.block_size // 8 # Create the nonce iv = os.urandom(sz) cipher = Cipher(algo, modes.CBC(iv), backend=default_backend()) # Pad the message with nulls up to the chipher block # This is essentially *WRONG* but for this demo... msg += bytes(sz - len(msg) % sz) encryptor = cipher.encryptor() ct = encryptor.update(msg) + encryptor.finalize() print(f"The answer is at {key[-1]}") return key[:-1], iv, ct def hack(hint: bytes, iv: bytes, ct: bytes): """ This is the answer to your homework... And I am a mean person and I do not give it to you :D """ pass hint, iv, ct = prepare_secret(b'This is the Plain Text message that you are trying to recover') hack(hint, iv, ct)You can use this to explore the problem and understand the different element sizes.
Also notice that the padding I am using to make the lenght of the plain text a multiplo of the cypher block size is WRONG... in reality you shall use something like PKCS7 or similar.