Exemple #1
0
// Implement AES in CTR mode
func c18() (actual, expected Result) {
	plaintext := []byte("A-B-C. A-always, B-be, C-counting. Always be counting!")
	key := []byte("YELLOW SUBMARINE")

	ciphertext := make([]byte, len(plaintext))
	plaintext2 := make([]byte, len(plaintext))

	nonce := 0
	// Encrypt...
	stream, err := crypto.Ctr(uint64(nonce), key)
	if err != nil {
		log.Fatal(err)
	}
	stream.XORKeyStream(ciphertext, plaintext)

	// Decrypt with a new key stream
	stream, err = crypto.Ctr(uint64(nonce), key)
	if err != nil {
		log.Fatal(err)
	}
	stream.XORKeyStream(plaintext2, ciphertext)

	return string(plaintext2), string(plaintext)
}
Exemple #2
0
/* Encrypt a set of strings in AES CTR mode using the same nonce
 * Decrypt the resulting ciphertexts without the key or stream
 */
func c20() (actual, expected Result) {
	input, _ := ioutil.ReadFile("input/20.txt")
	output, _ := utils.ReadAndStripFile("output/20.txt")
	expected = string(output)

	strs := strings.Split(string(input), "\n")
	n := len(strs)
	strs = strs[:n-2]
	key := crypto.NewAesKey()
	nonce := uint64(0)

	// Decode strings and find length of the shortest string
	length := 1000
	plaintexts := make([][]byte, len(strs))
	for i, str := range strs {
		decoded, _ := base64.StdEncoding.DecodeString(str)
		if len(decoded) < length {
			length = len(decoded)
		}
		plaintexts[i] = decoded
	}

	var concat []byte
	for i, str := range plaintexts {
		// Truncate each string to common length
		plaintexts[i] = str[:length]

		stream, err := crypto.Ctr(nonce, key)
		if err != nil {
			log.Println(err)
		}

		// Concatenate the ciphertexts
		ciphertext := make([]byte, length)
		stream.XORKeyStream(ciphertext, plaintexts[i])
		concat = append(concat, ciphertext...)
	}

	l := []int{length}

	plaintext, err := crypto.BreakXorRepeating(concat, l)
	if err != nil {
		log.Println(err)
	}

	return string(utils.Strip(plaintext)), expected
}