Very Easy - Ancient Encodings
Challenge Description
Your initialization sequence requires loading various programs to gain the necessary knowledge and skills for your journey. Your first task is to learn the ancient encodings used by the aliens in their communication.
Code
from Crypto.Util.number import bytes_to_long
from base64 import b64encode
from secret import FLAG
def encode(message):
return hex(bytes_to_long(b64encode(message)))
def main():
encoded_flag = encode(FLAG)
with open("output.txt", "w") as f:
f.write(encoded_flag)
if __name__ == "__main__":
main()
Imports
bytes_to_long
: A function from theCrypto.Util.number
library that converts a sequence of bytes to a long integer.b64encode
: A function from thebase64
library that encodes data into a base64 string.FLAG
: A variable imported from thesecret
module that contains the secret message we want to encode.
encode
Function
This function takes a message in byte format and processes it in two steps:
b64encode(message)
: Encodes the message into base64, returning a bytes object.bytes_to_long(b64encode(message))
: Converts this base64 byte string into a long integer.hex(...)
: Converts that long integer into a hexadecimal string.
For example, if message
is b'Hello'
:
b64encode(b'Hello')
returnsb'SGVsbG8='
.bytes_to_long(b'SGVsbG8=')
convertsb'SGVsbG8='
into a long integer.hex(...)
converts that long integer into a hexadecimal string.
Main Function
This function does the following:
- Calls the
encode
function withFLAG
as the argument and stores the result inencoded_flag
. - Opens (or creates if it doesn't exist) a file named
output.txt
in write mode ("w"
). - Writes the contents of
encoded_flag
to theoutput.txt
file.
Script Execution
This block ensures that main()
runs only if the script is executed directly (not if it's imported as a module in another script).
Summary
The code takes a secret message (FLAG
), first encodes it in base64, then converts that base64 representation into a long integer, and finally converts that long integer into a hexadecimal string. This hexadecimal string is then saved to a file named output.txt
. This process provides a way to encode and store the message in a form that is not easily recognizable in its original format.
This multi-step encoding approach can be useful in situations where you want to transform data so that it is not easily recognizable, although it does not provide strong cryptographic security on its own.
Decrypt
The Bash One-Liner
Assuming your bash one-liner reads the output.txt
file and decodes it back to the original message, it would likely look something like this:
Here's what each part does:
cat output.txt
: Reads the contents ofoutput.txt
.echo $(cat output.txt)
: Prints the contents ofoutput.txt
as a string.xxd -r -p
: Converts the hexadecimal string back to binary data.base64 -d
: Decodes the binary data from base64 back to the original message.
Decrypting in Python
To perform the decryption (or rather decoding) in Python, we need to reverse the encoding steps:
- Convert the hexadecimal string back to a long integer.
- Convert the long integer back to a base64-encoded byte string.
- Decode the base64 byte string back to the original message.
Here’s how you can achieve this:
from Crypto.Util.number import long_to_bytes
from base64 import b64decode
def decode(encoded_message):
# Remove the '0x' prefix and convert the hex string to an integer
long_value = int(encoded_message, 16)
# Convert the long integer back to bytes
base64_encoded_bytes = long_to_bytes(long_value)
# Decode the base64-encoded bytes to get the original message
message = b64decode(base64_encoded_bytes)
return message
def read_encoded_message():
with open("output.txt", "r") as f:
encoded_message = f.read().strip()
return encoded_message
if __name__ == "__main__":
encoded_message = read_encoded_message()
original_message = decode(encoded_message)
print("Original Message:", original_message.decode())
Explanation
Convert Hex String to Long Integer
This converts the hexadecimal string back into a long integer.
Convert Long Integer to Bytes
This converts the long integer back into a byte string, which is still base64 encoded.
Decode Base64-Encoded Bytes
This decodes the base64-encoded byte string back into the original message in bytes.
Read the Encoded Message from File
def read_encoded_message():
with open("output.txt", "r") as f:
encoded_message = f.read().strip()
return encoded_message
This reads the hexadecimal string from the output.txt
file.
Main Execution
if __name__ == "__main__":
encoded_message = read_encoded_message()
original_message = decode(encoded_message)
print("Original Message:", original_message.decode())
This reads the encoded message, decodes it, and prints the original message.
This Python script effectively reverses the encoding process performed by the original script, restoring the original message.