The code performs two encryption functions 'encrypt1' and 'encrypt2'. In 'encrypt1', the flag is split into blocks of size 16, and each block has its elements swapped with another element that is 7 positions away from it (wrapping around if necessary). However, this function is flawed as it doesn't return anything, so it doesn't actually modify the flag.

In 'encrypt2', the flag is transformed using a randomly generated substitution box 'S_BOX', which is a permutation of the numbers from 0 to 47. The flag is transformed by replacing each element with the element at the corresponding position in 'S_BOX'. The resulting flag is the encrypted flag.

To decrypt the flag, we need to undo 'encrypt2' and 'encrypt1' in reverse order. First, we can apply the inverse permutation of 'S_BOX' to the encrypted flag. Then, we can undo the swaps made by 'encrypt1' by swapping each element with the element that is 7 positions away from it (wrapping around if necessary). Finally, we need to unpad the decrypted flag to obtain the original flag.

Here's the Python code to do this:

from Crypto.Util.Padding import unpad

# Inverse permutation of S_BOX
INV_S_BOX = [0] * 48
for i, x in enumerate(S_BOX):
    INV_S_BOX[x] = i

# Apply inverse permutation of S_BOX
m = [c[i] for i in INV_S_BOX]

# Undo swaps made by encrypt1
for i in range(0, len(m), BLOCK):
    for j in range(BLOCK):
        aa = j * 7 % BLOCK
        m[i+j], m[i+aa] = m[i+aa], m[i+j]

# Remove padding
flag = unpad(m, BLOCK)

print(flag.decode())

This should output the original flag.

Crypto Challenge: Decrypting the Flag with Substitution and Swapping

原文地址: http://www.cveoy.top/t/topic/myA6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录