A 54: Caesar Cipher in Go (55 pts)

What You Need

A Linux machine with Go installed, which you prepared in a previous project.

Purpose

Encrypt and decrypt files using XOR in Go.

Strings and Bytes

To do cryptographic operations, we need to work with individual bytes, so we need to understand how data is stored in Go.

On your Linux server, in a Terminal window, execute these commands:

mkdir -p work/src/my_project/caesar1
nano ~/work/src/my_project/caesar1/caesar1.go
In nano, enter this code, as shown below.
package main
import "fmt"

func main() {
    plaintext := "ABC"
    key := byte(3)
	ciphertext := ""
	n := 0

	for i, c := range plaintext {
		nc := byte(c)
		ciphertext += string( nc + key )
		n = i
	}
    fmt.Printf("Key: %d\n", key)
    fmt.Printf("Input: String: %s Hex: %x\n", plaintext, plaintext)
    fmt.Printf("Output: %d characters: String: %s Hex: %x\n", n+1, ciphertext, ciphertext)
}

Save the file with Ctrl+X, Y, Enter.

Execute these commands to compile the program and run it:

go install my_project/caesar1
caesar1
The program runs, printing out the output as a string and in hexadecimal form, as shown below.


A 54.1: Encrypting Ciphertext (5 pts)

Encrypt this plaintext with a key of 5:
ALICE
With a key of a

Hint: to put raw hex into a string, use a statement like this:

ciphertext = "\x35\x09\x04\x41"
The flag is the ciphertext as a five-letter string, like ABCDE

A 54.2: Decrypting Ciphertext With the Key (5 pts)

Decrypt this ciphertext to reveal the flag:
UKORNG
It was encrypted with a key of 2.

The flag is the plaintext as a six-letter string, like ABCDEF


A 54.3: Decrypting a Text File Without the Key (10 pts)

Decrypt this ciphertext to reveal the flag:
ZNK&LRGM&OY&IXGIQKX
Note that this is a Caesar cipher based on ASCII codes, so shifting Z by one does not result in A, but in [

A 54.3: Decrypting a Text File Without the Key (10 pts)

Decrypt this ciphertext to reveal the flag:
0f333e7b3d373a3c7b32287b696e6d04101e0208
The key is a byte, between 0 and 255.

Reading and Writing Files in Go

On your Linux server, in a Terminal window, execute these commands to create a file named "infile" and prepare to enter a Go program:
echo -n ABC >infile
mkdir -p work/src/my_project/file1
nano ~/work/src/my_project/file1/file1.go
In nano, enter this code, as shown below.
package main
import ( "fmt"; "io/ioutil" )

func main() {
	dat, err := ioutil.ReadFile("infile")
	if err != nil { fmt.Println("Read Error: ", err) }

	size := len(dat)
	fmt.Println("Read in: ", size, " bytes")

	err = ioutil.WriteFile("outfile", dat, 0644)
	if err != nil { fmt.Println("Write Error: ", err) }
}

Save the file with Ctrl+X, Y, Enter.

Execute these commands to compile the program, run it, and examine the results:

go install my_project/file1
file1
cat infile
cat outfile
The program runs, creating "outfile" which contains the same contents as "infile", as shown below.

Encrypting a File with the Caesar Cipher

On your Linux server, in a Terminal window, execute these commands:
mkdir -p work/src/my_project/file2
nano ~/work/src/my_project/file2/file2.go
In nano, enter this code, as shown below.
package main
import ( "fmt"; "io/ioutil" )

func main() {
	dat, err := ioutil.ReadFile("infile")
	if err != nil { fmt.Println("Read Error: ", err) }

	size := len(dat)
	fmt.Println("Read in: ", size, " bytes")

	out := make([]byte, size)
	key := byte(3)
	b := byte(0)

	for i, c := range dat {
		b = byte(c)
        out[i] = b + key
        }

	err = ioutil.WriteFile("outfile2", out, 0644)
	if err != nil { fmt.Println("Write Error: ", err) }
}
Save the file with Ctrl+X, Y, Enter.

Execute these commands to compile the program, run it, and examine the results:

go install my_project/file2
file2
cat infile
cat outfile2
The program runs, creating "outfile2" which contains the contents of "infile" shifted forward by 2 in the alphabet, as shown below.


A 54.4: Decrypting a Text File Without the Key (15 pts)

To get the ciphertext, execute this command:
wget https://bowneconsultingcontent.com/pub/Attack/proj/A54.4_infile
This file contains a few paragraphs of text. When decrypted, the flag can be found within the text.

A 54.5: Decrypting an Image File Without the Key (20 pts)

To get the ciphertext, execute this command:
wget https://bowneconsultingcontent.com/pub/Attack/proj/A54.5_infile
This is an image file. When decrypted, it starts with a PNG file header, as shown below:

Notice that the first 4 bytes are 89, 50, 4E, 47; the 2nd through 4th byte spell out PNG in ASCII.

Decrypt the file. When you get it, change its filename extension to PNG and open it in an image viewer or Web browser.

The flag is visible in the image.

To download a file from a Google cloud server, click the gear at the top right, as shown below.


Posted: 11-4-19
Quotes changed to double quotes for raw hex 11-21-19