ss1111 ss1111 - 22 days ago 6
Python Question

Python - Generating all combinations of Hexadecimal values

I have an Encryption key which is 32-bits in Hexadecimal format. But I'm only given 22 bits. I have to find the Plaintext. My thought process is to do a brute-force attack and find the other 10 bits. I am also given the Ciphertext. The Encryption used is AES in 128-bit ECB Mode. I am using Python, but I just started learning it so I am not an expert yet.

My approach is to take the 22-bit key and concatenate the other 10 bits, feed that into AES along with the Ciphertext and Decrypt to check if one of the resulting phrases resembles a proper sentence. The only part that I am stuck on is generating the 10 bit Hexadecimal string.

This is the output that I want:

0000000000
0000000001
0000000002
...
000000000F
...
FFFFFFFFFF


What is an approach I could use to do this? I tried making a dictionary and assigning numerical values to all Hexadecimal values but I am stuck on how to write a loop that could give the sequence I want as an output.

Answer
def gen_all_hex():
    i = 0
    while i < 16**10:
        yield "{:010X}".format(i)
        i += 1

for s in gen_all_hex():
    print(s)

Result:

0000000000
0000000001
0000000002
0000000003
0000000004
0000000005
0000000006
0000000007
0000000008
0000000009
000000000A
000000000B
000000000C
000000000D
000000000E
000000000F
0000000010
0000000011
0000000012
0000000013
0000000014
0000000015
0000000016
0000000017
0000000018
0000000019
000000001A
...

Note that it's going to take a long time to iterate through all 1,099,511,627,776 values.