Cryptographic attacks and utilities for XOR-based algorithms.

Redefined classes

redef class RepeatingKeyXorCipher

crapto :: xor $ RepeatingKeyXorCipher

XOR cipher where the key is repeated to match the length of the message.
redef class SingleByteXorCipher

crapto :: xor $ SingleByteXorCipher

Simple XOR cipher where the whole plaintext is XORed with a single byte.

All class definitions

redef class RepeatingKeyXorCipher

crapto :: xor $ RepeatingKeyXorCipher

XOR cipher where the key is repeated to match the length of the message.
redef class SingleByteXorCipher

crapto :: xor $ SingleByteXorCipher

Simple XOR cipher where the whole plaintext is XORed with a single byte.
package_diagram crapto::xor xor combinations combinations crapto::xor->combinations crypto crypto crapto::xor->crypto crapto::english_utils english_utils crapto::xor->crapto::english_utils core core combinations->core crypto->core crapto::english_utils->core ...core ... ...core->core crapto::crapto crapto crapto::crapto->crapto::xor crapto::repeating_key_xor_solve repeating_key_xor_solve crapto::repeating_key_xor_solve->crapto::crapto crapto::repeating_key_xor_solve... ... crapto::repeating_key_xor_solve...->crapto::repeating_key_xor_solve

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module basic_ciphers

crypto :: basic_ciphers

Basic cryptographic ciphers and utilities.
module bitset

core :: bitset

Services to handle BitSet
module bytes

crypto :: bytes

Mix of utilities and services related to bytes
module bytes

core :: bytes

Services for byte streams and arrays
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O
module xor_ciphers

crypto :: xor_ciphers

XOR oriented cryptographic ciphers and utilities.

Parents

module combinations

combinations :: combinations

Memory-efficient Cartesian products, combinations and permutation on collections.
module crypto

crypto :: crypto

Mix of all things cryptography-related
module english_utils

crapto :: english_utils

English language utilities for cryptographic purposes.

Children

module crapto

crapto :: crapto

Cryptographic attacks and utilities.

Descendants

# Cryptographic attacks and utilities for XOR-based algorithms.
module xor

import combinations
import crypto
import english_utils

redef class SingleByteXorCipher
	# Tries to find key using frequency analysis on all possible plaintexts.
	# Populates `self.key`
	fun find_key do

		# Accumulate best result
		var max = 0.0
		var best = 0

		# Iterate on possible values for a byte
		var xor_b = new Bytes.with_capacity(1)
		for b in [0 .. 255] do
			# Need `Bytes` to pass to xor
			xor_b[0] = b

			# Xor and evaluate result
			var xored = ciphertext.xorcipher(xor_b)
			var result = xored.to_s.english_scoring
			if result > max then
				max = result
				best = b
			end
		end

		self.key = best

	end
end

redef class RepeatingKeyXorCipher
	# Attempts to find the key by using frequency analysis on the resulting plaintexts.
	# Best key lengths are estimated using the hamming distance of blocks of keylength bytes.
	# Ciphertext is then transposed in such a way that it can be solved by sequences of
	# SingleByteXor (one for each offset in the key).
	#
	# `min_keylength` and `max_keylength` are limits as to what key lengths are tested.
	# `considered_keylength_count` is the number of best key lengths that are kept to be
	# analysed by the SingleByteXor frequency analysis.
	fun find_key(min_keylength, max_keylength, considered_keylength_count: nullable Int)  do

		# Set default values
		if min_keylength == null then min_keylength = 2
		if max_keylength == null then max_keylength = 40
		if considered_keylength_count == null then considered_keylength_count = 3

		# Find the best candidate key lengths based on the normalized hamming distances
		var best_sizes = get_normalized_hamming_distances(min_keylength, max_keylength, considered_keylength_count)

		var best = 0.0
		var best_key: nullable Bytes = null
		for ks in best_sizes do

			# Rearrange ciphertext to be in SingleByteXORable blocks
			var transposed = transpose_ciphertext(ks)

			var key = new Bytes.empty
			for slot in transposed do
				var sbx = new SingleByteXorCipher
				sbx.ciphertext = slot
				sbx.find_key
				key.add sbx.key
			end

			# Evaluate the resulting plaintext based on english frequency analysis
			var eng_score = ciphertext.xorcipher(key).to_s.english_scoring
			if eng_score > best then
				best_key = key
				best = eng_score
			end

			assert best_key != null
			self.key = best_key

		end
	end

	# Computes the normalized hamming distances between blocks of ciphertext of length between `min_length` and `max_length`.
	# The `considered_keylength_count` smallest results are returned
	private fun get_normalized_hamming_distances(min_keylength, max_keylength, considered_keylength_count: Int): Array[Int] do

		var normalized_distances = new HashMap[Float, Int]

		# Iterate over all given key lengths
		for ks in [min_keylength .. max_keylength[ do

			# Initialize the blocks of size `ks`
			var blocks = new Array[Bytes]
			while (blocks.length + 1) * ks < ciphertext.length do
				blocks.add(ciphertext.slice(blocks.length * ks, ks))
			end

			# Compute the normalized hamming distance with all block combinations
			var pairs = new CombinationCollection[Bytes](blocks, 2)
			var hamming_dists = new Array[Float]
			for p in pairs do
				hamming_dists.add(p[0].hamming_distance(p[1]).to_f / ks.to_f)
			end

			# Normalize the results based on the number of blocks
			var normalized = 0.0
			for dist in hamming_dists do normalized += dist
			normalized /= hamming_dists.length.to_f
			normalized_distances[normalized] = ks

		end

		# Collect the best candidates
		var distances = normalized_distances.keys.to_a
		default_comparator.sort(distances)
		var best_distances = distances.subarray(0, considered_keylength_count)
		var best_sizes = [for d in best_distances do normalized_distances[d]]

		return best_sizes
	end

	# Returns a rearranged format of the ciphertext where every byte of a slot will be XORed with the same offset of a key of length `keylength`.
	private fun transpose_ciphertext(keylength: Int): Array[Bytes] do
		var transposed = new Array[Bytes]
		for i in [0 .. keylength[ do
			transposed.add(new Bytes.empty)
		end

		for byte_idx in [0 .. ciphertext.length[ do
			transposed[byte_idx % keylength].add(ciphertext[byte_idx])
		end

		return transposed
	end
end
lib/crapto/xor.nit:15,1--150,3