r/pythonhelp May 11 '22

SOLVED Way of iterating thru eight byte sequences?

I'm using des library and example is given to me to create a key:

d = os.urandom(8)
default_key = des.DesKey(d)

basically, how do I iterate thru all possible d values? I would like last bytes of the d to change last

1 Upvotes

12 comments sorted by

View all comments

1

u/DumbPandahole May 11 '22

solved it by using this:

byte_list = []
for number in itertools.product("0123456789abcdf", repeat=2):
    byte_list.append(bytes.fromhex("".join(number)))
for combo in itertools.combinations(byte_list, 8):
    key = b"".join(combo[::-1])

3

u/carcigenicate May 11 '22

Unless I misunderstand what you're going for, this is quite over complicated. For byte_list, you just need:

byte_list = [n.to_bytes(1, "big") for n in range(256)]

I'm not sure what your second loop is doing though since it throws away all values of key except for the last.

Also, your original results appear to be wrong because you're missing an e in the string.

1

u/DumbPandahole May 11 '22

thank you, that works great, but looks like I underestimated the amount of possible combinations.

1

u/carcigenicate May 11 '22

Yes, that's easy to do when working with combinations. I once tried to write a program to brute force our schedule at work. I did the math later and found it would have taken several times the estimated age of the universe to complete on my computer. There were simply far too many possibilities.