Пример #1
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
}
Пример #2
0
// Break repeating-key XOR
func c6() (actual, expected Result) {
	input, _ := ioutil.ReadFile("input/6.txt")
	output, _ := utils.ReadAndStripFile("output/6.txt")
	expected = string(output)

	inputStr := string(input)
	ciphertext, err := base64.StdEncoding.DecodeString(inputStr)
	if err != nil {
		log.Fatal(err)
	}

	// Find the 3 most likely keysizes in the range 2-40
	keysizes, err := crypto.FindKeysizes(ciphertext, 3, 2, 40)
	if err != nil {
		log.Fatal(err)
	}

	plaintext, err := crypto.BreakXorRepeating(ciphertext, keysizes)
	if err != nil {
		log.Fatal(err)
	}
	return string(utils.Strip(plaintext)), expected
}