# Proj X2: XOR Encryption in Python (Up to 40 pts. extra credit)

## What You Need

A Kali Linux machine, real or virtual. You could also use OS X, or Windows with Python installed.

## Purpose

Encrypt and decrypt files using XOR in Python.

## Understanding XOR

Exclusive OR (XOR) is a fundamental mathematical operation used in many encryption algorithms.

XOR operates on one bit at a time, with these results:

``` 0 XOR 0 = 0 0 XOR 1 = 1 1 XOR 0 = 1 1 XOR 1 = 0 ```
For our purposes, we'll use the Python ^ operator, which acts on a whole byte at a time.

Characters are ASCII-encoded, like this:

``` A is 01000001 B is 01000010 C is 01000011 ... ```
A whole table of ASCII values is here:

Consider A^B:

``` A is 01000001 B is 01000010 A^B= 00000011 ```
That is character 3, an unprintable end-of-text mark.

However, A^s is printable:

``` A is 01000001 s is 01110011 A^B= 00110010 ```
The result is the hexadecimal value 0x32, or the numeral 2.

## XOR in Python

In Kali Linux, in a Terminal window, execute this command:
```nano xor1 ```
In nano, enter the code shown below:
```#!/usr/bin/python import sys if len(sys.argv) != 4: print "Usage: ./xor1 infile outfile k" print "k is a one-character XOR key" print "For hexadecimal keys, use \$'\\x01'" exit() f = open(str(sys.argv[1]), "rb") g = open(str(sys.argv[2]), "a") k = ord(sys.argv[3]) try: byte = f.read(1) while byte != "": xbyte = ord(byte) ^ k g.write(chr(xbyte)) byte = f.read(1) finally: f.close() g.close() ```

Save the file with Ctrl+X, Y, Enter. Next, we need to make the file executable.

In a Terminal window, execute this command:

```chmod a+x xor1 ```

## Encrypting a Single Character

In a Terminal window, execute this command:
```./xor1 ```
You see the help message, explaining how to use the program, as shown below.

To create a file named plain1 with the letter A in it, execute these commands :

```echo -n A > plain1 cat plain1 ```
The "echo -n" command created a file named plain1 which contains a single letter A, without a carriage return at the end of the file.

The "cat plain1" command printed out the file, which appeared as a single A at the start of the next line, as shown below:

To encode the plain1 file with a key of s, execute these commands:

```./xor1 plain1 cipher1 s cat cipher1 ```
The result is 2, as shown below:

## Encrypting a Text File

In Kali Linux, in a Terminal window, execute this command:
```nano plain2 ```
In nano, enter the code shown below, replacing "YOUR NAME" with your own name:
```Normal English text; written by YOUR NAME ```

Save the file with Ctrl+X, Y, Enter. To encrypt the file using a key of x, execute these commands:

```./xor1 plain2 cipher2 x cat cipher2 ```
The result is strange unreadable characters, as shown below:

## Decrypting a Text File

To decrypt a file, simply repeat the XOR operation with the same key. It will reverse itself.

Execute these commands:

```./xor1 cipher2 plain2r x cat plain2r ```
The file is restored to readable text, as shown below:

## Challenge A: Unknown Single-Digit Key (5 pts. extra credit)

Download this file and decrypt it. It's XOR-encrypted, and the key is a single ASCII number, between 0 and 9.

Use the form below to put your name on the WINNERS PAGE.

 Your Name (without spaces): The complete decrypted text:
Save a whole-screen image of the winners page showing your name with the filename "YOUR NAME Proj X2a".

## Challenge B: Unknown Single-Byte Key (10 pts. extra credit)

Download this file and decrypt it. It's XOR-encrypted, and the key is a single byte, between '\x00' and '\xff'.

## Hint

This script shows how to loop through bytes:

Use the form below to put your name on the WINNERS PAGE.
 Your Name (without spaces): The complete decrypted text:
Save a whole-screen image of the winners page showing your name with the filename "YOUR NAME Proj X2b".

## Challenge C: Unknown Two-Byte Key (10 pts. extra credit)

Download this file and decrypt it. It's XOR-encrypted, and the key is a two bytes, between '\x0000' and '\xffff'.

This means you XOR it two bytes at a time, as shown below for a key of '\xf00d' and plaintext of 'AB':

```Plaintext: 0xf00d = 0b1111000000001101 Key: 'AB" = 0b0010000100010010 XOR: 0b1101000100011111 = 0xd11f ```

## Hint

The decrypted text begins with the word `Congratulations`
Use the form below to put your name on the WINNERS PAGE.
 Your Name (without spaces): The complete decrypted text:
Save a whole-screen image of the winners page showing your name with the filename "YOUR NAME Proj X2c".

## Challenge D: Asynchronous XOR With Single-Byte Key (15 pts. extra credit)

Download this file and decrypt it. It's XOR-encrypted in an asynchronous manner, and the key is a single byte, between '\x00' and '\xff'.

Here is an example of the algorithm for a key of 'A' and plaintext of 'ABC':

```Plaintext: 'ABC' = 0b01000001 0b01000010 0b01000011 Key: 'A' = 0b01000001 XOR first byte with key: 0b00000000 Add result to key (mod 256): 0b01000001 Use result for next key: 0b01000001 XOR next byte: 0b00000011 Add result to key (mod 256): 0b01000100 Use result for next key: 0b01000100 XOR next byte: 0b00000111 Final ciphertext: 0b00000000 0b00000011 0b00000111 = 0x000307 ```

## Hint

The decrypted text contains the word `Congratulations`
Use the form below to put your name on the WINNERS PAGE.
 Your Name (without spaces): The complete decrypted text:
Save a whole-screen image of the winners page showing your name with the filename "YOUR NAME Proj X2b".