We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

One Time Pad

A cipher is said to have perfect security if an attacker who has access to only the ciphertext can infer absolutely nothing of interest about the plaintext. Such perfect ciphers do exist, one such example is the "one-time pad".

Click to play video

The XOR Operator in Go

The ^ operator will XOR two bytes. This cheat-sheet may be helpful.

byte1 := 0b01110000
byte2 := 0b10101000
xorRes := byte1 ^ byte2
fmt.Printf("%b\n", xorRes)
// 11011000

Assignment

While the One Time Pad is a very powerful cipher, it's also very difficult to use in practice. That said, our product managers have found a use case in Passly that isn't overly inconvenient, so they've asked us to implement it.

Complete the crypt function. It accepts some data and a key as slices of bytes, and returns the result of an XOR operation on all the bits.

For example:

data       = "0110100001100101011011000110110001101111"
key        = "0111001101101010011001100111010101100100"

output     = "0001101100001111000010100001100100001011"

Note on Software Design

Because XOR encryption is the perfect inverse of XOR decryption, the encrypt() and decrypt() will be identical in functionality. That's why we're writing a single crypt function that's used directly by the encrypt and decrypt functions. It makes the code easier to understand and use.