コード例 #1
0
ファイル: cryptopals.go プロジェクト: taravancil/cryptopals
// Implement AES CBC mode
func c10() (actual, expected Result) {
	input, _ := ioutil.ReadFile("input/10.txt")
	output, _ := utils.ReadAndStripFile("output/10.txt")
	expected = string(output)

	key := []byte("YELLOW SUBMARINE")
	iv := stdBytes.Repeat([]byte("\x00"), aes.BlockSize)
	ciphertext, _ := base64.StdEncoding.DecodeString(string(input))
	plaintext, _ := crypto.CbcDecrypt(ciphertext, key, iv)

	return string(utils.Strip(plaintext)), expected
}
コード例 #2
0
ファイル: cryptopals.go プロジェクト: taravancil/cryptopals
/* 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
}
コード例 #3
0
ファイル: cryptopals.go プロジェクト: taravancil/cryptopals
/* Implement AES in ECB mode, then decrypt a ciphertext encrypted under
* a known key.
 */
func c7() (actual, expected Result) {
	input, _ := ioutil.ReadFile("input/7.txt")
	output, _ := utils.ReadAndStripFile("output/7.txt")
	expected = string(output)

	key := []byte("YELLOW SUBMARINE")
	text := string(input)

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

	plaintext, err := crypto.EcbDecrypt(ciphertext, key)
	if err != nil {
		log.Fatal(err)
	}

	return string(utils.Strip(plaintext)), expected
}
コード例 #4
0
ファイル: cryptopals.go プロジェクト: taravancil/cryptopals
// 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
}