Skip to content

Files

Latest commit

 

History

History
145 lines (118 loc) · 3.59 KB

fixme2-explain.md

File metadata and controls

145 lines (118 loc) · 3.59 KB

Code Breakdown and Issues


1. Importing the random module

import random

Issue:

  • The random module is not used anywhere in the script.

Fix:

  • Remove the unnecessary import:
# import random  # Remove this line

2. Function to XOR a string with a key

def str_xor(secret, key):

No issue here.


3. Key Extension Logic

new_key = key
i = 0
while len(new_key) < len(secret):
    # new_key = new_key + key[i]  # Incorrect string concatenation
    new_key.append(key[i])  # Incorrect use of append
    i = (i + 1) % len(key)

Issues:

  1. new_key is initialized as a string (key), but .append() is only valid for lists.
  2. The previous implementation (new_key = new_key + key[i]) was inefficient but worked with strings.

Fix:

  • Convert new_key to a list, then join it back into a string:
new_key = list(key)  # Convert key to a list
i = 0
while len(new_key) < len(secret):
    new_key.append(key[i])  # Append to the list
    i = (i + 1) % len(key)
new_key = "".join(new_key)  # Convert back to string

4. XOR Operation

return "".join([chr(ord(secret_c) ^ ord(new_key_c)) for (secret_c, new_key_c) in zip(secret, new_key)])

Correct logic for XOR encryption/decryption.
Potential issue:

  • If the XOR operation results in non-printable characters, the output might be unreadable.

Fix (optional improvement):

  • If handling binary data, return a byte string instead:
return bytes([ord(a) ^ ord(b) for a, b in zip(secret, new_key)])

5. Encrypted Flag Definition

flag_enc = chr(0x15) + chr(0x07) + chr(0x08) + chr(0x06) + ...

No issue if these values are correct.


6. Decrypting the Flag

flag = str_xor(flag_enc, 'enkidu')

Correct logic.


7. Checking If flag Is Empty

if flag = "":

Syntax Error:

  • = is an assignment operator, but an equality check requires ==.

Fix:

if flag == "":

8. Printing the Flag

else:
    print('That is correct! Here\'s your flag: ' + flag)

No issue here.


Fixed and Optimized Code

def str_xor(secret, key):
    # Extend key to match secret length efficiently
    new_key = list(key)  # Convert key to a list
    i = 0
    while len(new_key) < len(secret):
        new_key.append(key[i])  # Append to the list
        i = (i + 1) % len(key)
    new_key = "".join(new_key)  # Convert back to string

    # Perform XOR and return the result
    return "".join(chr(ord(a) ^ ord(b)) for a, b in zip(secret, new_key))


flag_enc = "".join(chr(x) for x in [
    0x15, 0x07, 0x08, 0x06, 0x27, 0x21, 0x23, 0x15, 0x58, 0x18, 0x11, 0x41,
    0x09, 0x5f, 0x1f, 0x10, 0x3b, 0x1b, 0x55, 0x1a, 0x34, 0x5d, 0x51, 0x40,
    0x54, 0x09, 0x05, 0x04, 0x57, 0x1b, 0x11, 0x31, 0x5f, 0x51, 0x52, 0x46,
    0x00, 0x5f, 0x5a, 0x0b, 0x19
])

flag = str_xor(flag_enc, "enkidu")

# Check that flag is not empty
if flag == "":
    print("String XOR encountered a problem, quitting.")
else:
    print("That is correct! Here's your flag:", flag)

Summary of Issues and Fixes

Issue Fix
random module is imported but not used Removed import
.append() used on a string (new_key.append(key[i])) Converted new_key to a list
if flag = "" uses = instead of == Changed to if flag == ""
Inefficient string concatenation (new_key = new_key + key[i]) Used a list for efficient appending