简体   繁体   中英

Bruteforce python shuffle function

Hello I was wondering how to revert a string that was shuffled with the random.shuffle() function from python. I know that the original string was shuffled with this code:

import random

file_to_shuffle = list(open("file_tot_shuffle.txt", "rb").read().strip()) #rile containing a string
random.seed(random.randint(0, 256))
random.shuffle(file_to_shuffle)
print(bytes(file_to_shuffle).decode())

Thanks to anyone that can help

Let me know if this is what you're looking for:

import random

file_contents = list("Something")

# Get seed for the code
seed = random.randint(0, 256)
random.seed(seed)
random.shuffle(file_contents)

# Re-Inject Same seed
indices = list(range(len(file_contents)))
random.seed(seed)
random.shuffle(indices)

# To Revert
collat = sorted(list(zip(indices, file_contents)), key=lambda x: x[0])

print("".join(list(zip(*collat))[1]))

PS This works due to the psuedo random algorithm, which is not true randomness

The issue if the original seed is known or not. If we know it, you can reverse the shuffle by figuring how how that seed shuffles a list of that length.

import random

# file_to_shuffle = list(open("file_tot_shuffle.txt", "rb").read().strip()) #rile containing a string
file_to_shuffle = list(b"HI THIS IS SOME RANDOM TEXT".strip())

SEED = random.randint(0, 256)

random.seed(SEED)
random.shuffle(file_to_shuffle)
print(bytes(file_to_shuffle).decode())

order = list(range(len(file_to_shuffle)))
random.seed(SEED)
random.shuffle(order)

unshuffledList = [0]*len(order)
for i, orig_i in enumerate(order):
    unshuffledList[orig_i] = file_to_shuffle[i]

print(bytes(unshuffledList).decode())

Output:

MIR OION EH TST HSMEASITDX
HI THIS IS SOME RANDOM TEXT

If we don't know the seed, we at least know it's somewhere in the range of 0 to 256... so there may be a way to reverse it if something more is known about the original file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM