VP 110: Bytes and Files (25 pts)

What You Need

Any computer with Python 3.

Purpose

Learn about the Python 3 data types: byte, int, and string.

Integers

Execute this command to open Python 3 in interactive mode:
python3
Now enter these commands, which are immediately executed.
i = 1
print(i, type(i))
j = i + 1
print(j, type(j))
As shown below, i and j are 'int' objects: integers, so addition works.

Bytes

Execute these commands:
a = b'\x01'
print(a, type(a))
b = a + 1
As shown below, a is a "bytes" object, and addition is not allowed.
One way to convert bytes to integers is to select a specific byte.

Execute these commands:

b = a[0]
print(b, type(b))
c = b + 1
print(c, type(c))
As shown below, b is a "int" object, and addition works.
So, to add one to a byte object, execute these commands:
a = b'\x01'
print(a, type(a))
b = a[0]
print(b, type(b))
c = b + 1
print(c, type(c))
d = bytes([c])
print(d, type(d))
As shown below, converting the "bytes" object to an "int" object, performing the addition, and converting it back to "bytes" works.
To exit interactive mode, execute this command:
exit()

Files

In a text editor, create a file named infile containing this text:
HELLO
In a text editor, make this program:
with open("infile", "rb") as f:
    byte = f.read(1)
    while byte != b"":
        print(byte)
        byte = f.read(1)
As shown below, this program reads the file one byte at a time.
In a text editor, make this program:
with open("infile", "rb") as f:
    with open("outfile", "wb") as g:
        byte = f.read(1)
        while byte != b"":
            print(byte)
            g.write(byte)
            byte = f.read(1)
As shown below, this program copies infile to outfile.

Flag VP 110.1: Eights (5)

Download this file:
https://samsclass.info/124/proj14/VP110-1
To download that file on Debian, execute these commands:
sudo apt update
sudo apt install wget -y
wget https://samsclass.info/124/proj14/VP110-1
Count the number of '\x08' bytes. That's the flag.

Flag VP 110.2: Adding Files (10)

Download these two files:
https://samsclass.info/124/proj14/VP110-2a
https://samsclass.info/124/proj14/VP110-2b
Add the bytes in the two files together to construct the flag. For example, if the first file contains b'\x40' and the second contains b'\x01', the result is b'\x41', or "A".

Flag VP 110.3: Adding Files (10)

Download these two files:
https://samsclass.info/124/proj14/VP110-3a
https://samsclass.info/124/proj14/VP110-3b
Add the bytes in the two files together to construct a new file named VP110-3-flag.png. Open that file in an image viewer to see the flag.

References

How to Convert Bytes to Int in Python 2.7 and 3.x


Upgraded to Python 3 5-29-2020
wget commands added 8-2-2020