Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
140 Kevin 1
import math
2
 
3
substitution_map = {0:14,1:4,2:13,3:1,4:2,5:15,6:11,7:8,8:3,9:10,10:6,11:12,12:5,13:9,14:0,15:7}
4
 
5
key = [3,10,9,4,13,6,3,15]				# Key
6
# key = [8,9,10,12,3,6,11,5]
7
key_length = len(key) * 4				# Calculate length of the key in bits
8
bytes_per_key = key_length / 8			# Calculate the number of bytes for each round key (4 for a 32-bit key)
9
rounds = int(math.log(key_length,2))	# Calculate the number of rounds (5 for a 32-bit key)
10
 
11
# plaintext = [2,6,11,7]
12
# plaintext = [15,14,2,6]
13
# plaintext = [15,14,2,7]
14
plaintext = [2,13,0,0]
15
 
16
def get_key(key, round):
17
	'''Returns the round key for the specified key.
18
	The round key is bytes_per_key bytes starting at the specified offset determined by round.'''
19
	round -= 1
20
	ret = []
21
	for index in range(bytes_per_key):
22
		ret.append(key[index + round])
23
	return ret
24
 
25
def xor_array(array, key):
26
	'''Xor the two arrays and returns the result.'''
27
	ret = []
28
	for index in range(len(array)):
29
		ret.append(array[index] ^ key[index])
30
	return ret
31
 
32
def sub_array(array):
33
	'''Runs the given array through the substitution map and returns the result.'''
34
	ret = []
35
	for index in range(len(array)):
36
		ret.append(substitution_map[array[index]])
37
	return ret
38
 
39
def perm_array(array):
40
	'''Permutes the given array and returns the results.'''
41
	ret = []
42
	for index in range(bytes_per_key-1,-1,-1):
43
		value = 0
44
		for entry in array:
45
			value <<= 1
46
			value |= (entry >> index) & 1
47
		ret.append(value)
48
	return ret
49
 
50
if __name__ == '__main__':
51
	working_array = plaintext 	# Initial array is the plaintext
52
	print "Round 0 W (Plaintext):\t", plaintext
53
	print
54
 
55
	# Print out the results for each step as it is calculated
56
	for round in range(1,rounds-1):
57
		print "Round {0} Key:\t\t\t".format(round),get_key(key, round)
58
		working_array = xor_array(working_array, get_key(key, round))
59
		print "Round {0} U (xor'ed):\t\t".format(round),working_array
60
		working_array = sub_array(working_array)
61
		print "Round {0} V (sub'ed):\t\t".format(round),working_array
62
		working_array = perm_array(working_array)
63
		print "Round {0} W (perm'ed):\t".format(round),working_array
64
		print
65
 
66
	# Calculate and print out the final round without the permutation
67
	print "Round {0} Key:\t\t\t".format(rounds-1),get_key(key, rounds-1)
68
	working_array = xor_array(working_array, get_key(key, rounds-1))
69
	print "Round {0} U (xor'ed):\t\t".format(rounds-1),working_array
70
	working_array = sub_array(working_array)
71
	print "Round {0} V (sub'ed):\t\t".format(rounds-1),working_array
72
	print
73
	print "Round {0} Key:\t\t\t".format(rounds),get_key(key, rounds)
74
	working_array = xor_array(working_array, get_key(key,rounds))
75
	print("Ciphertext (xor'ed):\t"), working_array