def detect_possible_key(coded_text, key_len):
	# split text into groups based on founded key len and xor with ' ' (space)
	groups = [coded_data[i:i+key_len] for i in range(0, len(coded_data), key_len)]

	possible_key = ''
	for i in range(key_len):
		freq = {}
		scaned_chars = 0
		for item in groups:
			# check if text part not less that current index
			if i < len(item):
				char = sk.str_xor(item[i], ' ')
				if char in freq:
					freq[char] += 1
				else:
					freq[char] = 1
				scaned_chars += 1
		
		freq = {k: float(v)/scaned_chars for k, v in freq.items()}
		key_char = sorted(freq.items(), key = lambda item: item[1], reverse = True)[0]
		possible_key += key_char[0]
	return possible_key		
		for item in groups:
			# check if text part not less that current index
			if i < len(item):
				char = sk.str_xor(item[i], ' ')
				if char in freq:
					freq[char] += 1
				else:
					freq[char] = 1
				scaned_chars += 1
		
		freq = {k: float(v)/scaned_chars for k, v in freq.items()}
		key_char = sorted(freq.items(), key = lambda item: item[1], reverse = True)[0]
		possible_key += key_char[0]
	return possible_key		


if __name__ == '__main__':
	coded_data = ''

	with open("encryptedtext.txt", 'rb') as f:
		coded_data = f.read()

	# find key length
	key_len = detect_key_len(coded_data)
	print key_len
	
	possible_key = detect_possible_key(coded_data, key_len)
      
	print sk.str_xor(coded_data, possible_key)
	print "This text encoded by key: %s" % (possible_key)
		freq = {}
		scaned_chars = 0
		for item in groups:
			# check if text part not less that current index
			if i < len(item):
				char = sk.str_xor(item[i], ' ')
				if char in freq:
					freq[char] += 1
				else:
					freq[char] = 1
				scaned_chars += 1
		
		freq = {k: float(v)/scaned_chars for k, v in freq.items()}
		key_char = sorted(freq.items(), key = lambda item: item[1], reverse = True)[0]
		possible_key += key_char[0]
	return possible_key		


if __name__ == '__main__':
	coded_data = ''

	with open(argv[1], 'rb') as f:
		coded_data = f.read()

	# find key length
	key_len = detect_key_len(coded_data)
	possible_key = detect_possible_key(coded_data, key_len)

	print sk.str_xor(coded_data, possible_key)
	print "This text encoded by key: %s" % (possible_key)
def detect_possible_key(coded_text, key_len):
	# split text into groups based on founded key len and xor with ' ' (space)
	groups = [coded_data[i:i+key_len] for i in range(0, len(coded_data), key_len)]

	possible_key = ''
	for i in range(key_len):
		freq = {}
		scaned_chars = 0
		for item in groups:
			# check if text part not less that current index
			if i < len(item):
				char = sk.str_xor(item[i], ' ')
				if char in freq:
					freq[char] += 1
				else:
					freq[char] = 1
				scaned_chars += 1
		
		freq = {k: float(v)/scaned_chars for k, v in freq.items()}
		key_char = sorted(freq.items(), key = lambda item: item[1], reverse = True)[0]
		possible_key += key_char[0]
	return possible_key