fork download
  1. # Table of Position of 64 bits at initial level: Initial Permutation Table
  2. initial_perm = [58, 50, 42, 34, 26, 18, 10, 2,
  3. 60, 52, 44, 36, 28, 20, 12, 4,
  4. 62, 54, 46, 38, 30, 22, 14, 6,
  5. 64, 56, 48, 40, 32, 24, 16, 8,
  6. 57, 49, 41, 33, 25, 17, 9, 1,
  7. 59, 51, 43, 35, 27, 19, 11, 3,
  8. 61, 53, 45, 37, 29, 21, 13, 5,
  9. 63, 55, 47, 39, 31, 23, 15, 7]
  10.  
  11. # Expansion D-box Table
  12. exp_d = [32, 1, 2, 3, 4, 5, 4, 5,
  13. 6, 7, 8, 9, 8, 9, 10, 11,
  14. 12, 13, 12, 13, 14, 15, 16, 17,
  15. 16, 17, 18, 19, 20, 21, 20, 21,
  16. 22, 23, 24, 25, 24, 25, 26, 27,
  17. 28, 29, 28, 29, 30, 31, 32, 1]
  18.  
  19. # Straight Permutation Table
  20. per = [16, 7, 20, 21,
  21. 29, 12, 28, 17,
  22. 1, 15, 23, 26,
  23. 5, 18, 31, 10,
  24. 2, 8, 24, 14,
  25. 32, 27, 3, 9,
  26. 19, 13, 30, 6,
  27. 22, 11, 4, 25]
  28.  
  29. # S-box Table
  30. sbox = [[[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
  31. [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
  32. [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
  33. [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]],
  34.  
  35. [[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
  36. [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
  37. [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
  38. [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]],
  39.  
  40. [[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
  41. [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
  42. [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
  43. [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]],
  44.  
  45. [[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
  46. [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
  47. [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
  48. [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]],
  49.  
  50. [[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
  51. [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
  52. [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
  53. [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]],
  54.  
  55. [[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
  56. [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
  57. [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
  58. [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]],
  59.  
  60. [[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
  61. [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
  62. [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
  63. [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]],
  64.  
  65. [[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
  66. [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
  67. [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
  68. [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]]
  69.  
  70. # Final Permutation Table
  71. final_perm = [40, 8, 48, 16, 56, 24, 64, 32,
  72. 39, 7, 47, 15, 55, 23, 63, 31,
  73. 38, 6, 46, 14, 54, 22, 62, 30,
  74. 37, 5, 45, 13, 53, 21, 61, 29,
  75. 36, 4, 44, 12, 52, 20, 60, 28,
  76. 35, 3, 43, 11, 51, 19, 59, 27,
  77. 34, 2, 42, 10, 50, 18, 58, 26,
  78. 33, 1, 41, 9, 49, 17, 57, 25]
  79.  
  80. # Hexadecimal to binary conversion
  81. def hex2bin(s):
  82. conversion = {'0': "0000",
  83. '1': "0001",
  84. '2': "0010",
  85. '3': "0011",
  86. '4': "0100",
  87. '5': "0101",
  88. '6': "0110",
  89. '7': "0111",
  90. '8': "1000",
  91. '9': "1001",
  92. 'A': "1010",
  93. 'B': "1011",
  94. 'C': "1100",
  95. 'D': "1101",
  96. 'E': "1110",
  97. 'F': "1111"}
  98. binary = ""
  99. for i in range(len(s)):
  100. binary = binary + conversion[s[i]]
  101. return binary
  102.  
  103. # Binary to hexadecimal conversion
  104. def bin2hex(s):
  105. conversion = {"0000": '0',
  106. "0001": '1',
  107. "0010": '2',
  108. "0011": '3',
  109. "0100": '4',
  110. "0101": '5',
  111. "0110": '6',
  112. "0111": '7',
  113. "1000": '8',
  114. "1001": '9',
  115. "1010": 'A',
  116. "1011": 'B',
  117. "1100": 'C',
  118. "1101": 'D',
  119. "1110": 'E',
  120. "1111": 'F'}
  121. hexa = ""
  122. for i in range(0, len(s), 4):
  123. ch = ""
  124. ch = ch + s[i]
  125. ch = ch + s[i + 1]
  126. ch = ch + s[i + 2]
  127. ch = ch + s[i + 3]
  128. hexa = hexa + conversion[ch]
  129.  
  130. return hexa
  131.  
  132.  
  133.  
  134. # Binary to decimal conversion
  135. def bin2dec(binary):
  136.  
  137. binary1 = binary
  138. decimal, i, n = 0, 0, 0
  139. while(binary != 0):
  140. dec = binary % 10
  141. decimal = decimal + dec * pow(2, i)
  142. binary = binary//10
  143. i += 1
  144. return decimal
  145.  
  146. # Decimal to binary conversion
  147.  
  148.  
  149. def dec2bin(num):
  150. res = bin(num).replace("0b", "")
  151. if(len(res) % 4 != 0):
  152. div = len(res) / 4
  153. div = int(div)
  154. counter = (4 * (div + 1)) - len(res)
  155. for i in range(0, counter):
  156. res = '0' + res
  157. return res
  158.  
  159. # Permute function to rearrange the bits
  160.  
  161.  
  162. def permute(k, arr, n):
  163. permutation = ""
  164. for i in range(0, n):
  165. permutation = permutation + k[arr[i] - 1]
  166. return permutation
  167.  
  168. # shifting the bits towards left by nth shifts
  169.  
  170.  
  171. def shift_left(k, nth_shifts):
  172. s = ""
  173. for i in range(nth_shifts):
  174. for j in range(1, len(k)):
  175. s = s + k[j]
  176. s = s + k[0]
  177. k = s
  178. s = ""
  179. return k
  180.  
  181.  
  182.  
  183. # calculating xow of two strings of binary number a and b
  184. def xor(a, b):
  185. ans = ""
  186. for i in range(len(a)):
  187. if a[i] == b[i]:
  188. ans = ans + "0"
  189. else:
  190. ans = ans + "1"
  191. return ans
  192.  
  193.  
  194.  
  195.  
  196.  
  197. def encrypt(pt, rounded_key_binary, rk):
  198. pt = hex2bin(pt)
  199.  
  200. # Initial Permutation
  201. pt = permute(pt, initial_perm, 64)
  202. print("After initial permutation", bin2hex(pt))
  203.  
  204. # Splitting
  205. left = pt[0:32]
  206. right = pt[32:64]
  207. for i in range(0, 16):
  208. # Expansion D-box: Expanding the 32 bits data into 48 bits
  209. right_expanded = permute(right, exp_d, 48)
  210.  
  211. # XOR RoundKey[i] and right_expanded
  212. xor_x = xor(right_expanded, rounded_key_binary[i])
  213.  
  214. # S-boxex: substituting the value from s-box table by calculating row and column
  215. sbox_str = ""
  216. for j in range(0, 8):
  217. row = bin2dec(int(xor_x[j * 6] + xor_x[j * 6 + 5]))
  218. col = bin2dec(
  219. int(xor_x[j * 6 + 1] + xor_x[j * 6 + 2] + xor_x[j * 6 + 3] + xor_x[j * 6 + 4]))
  220. val = sbox[j][row][col]
  221. sbox_str = sbox_str + dec2bin(val)
  222.  
  223. # Straight D-box: After substituting rearranging the bits
  224. sbox_str = permute(sbox_str, per, 32)
  225.  
  226. # XOR left and sbox_str
  227. result = xor(left, sbox_str)
  228. left = result
  229.  
  230. # Swapper
  231. if(i != 15):
  232. left, right = right, left
  233. print("Round ", i + 1, " ", bin2hex(left),
  234. " ", bin2hex(right), " ", rk[i])
  235.  
  236. # Combination
  237. combine = left + right
  238.  
  239. # Final permutation: final rearranging of bits to get cipher text
  240. cipher_text = permute(combine, final_perm, 64)
  241. return cipher_text
  242.  
  243.  
  244. pt = "123456ABCD132536"
  245. key = "AABB09182736CCDD"
  246.  
  247. # Key generation
  248. # --hex to binary
  249. key = hex2bin(key)
  250. print(key)
  251. print(len(key))
  252. # --parity bit drop table
  253. keyp = [57, 49, 41, 33, 25, 17, 9,
  254. 1, 58, 50, 42, 34, 26, 18,
  255. 10, 2, 59, 51, 43, 35, 27,
  256. 19, 11, 3, 60, 52, 44, 36,
  257. 63, 55, 47, 39, 31, 23, 15,
  258. 7, 62, 54, 46, 38, 30, 22,
  259. 14, 6, 61, 53, 45, 37, 29,
  260. 21, 13, 5, 28, 20, 12, 4]
  261.  
  262. # getting 56 bit key from 64 bit using the parity bits
  263. key = permute(key, keyp, 56)
  264. print("After permutations: ",key)
  265. print(len(key))
  266. # Number of bit shifts
  267. shift_table = [1, 1, 2, 2,
  268. 2, 2, 2, 2,
  269. 1, 2, 2, 2,
  270. 2, 2, 2, 1]
  271.  
  272. # Key- Coconversionression Table : Coconversionression of key from 56 bits to 48 bits
  273. key_coconversion = [14, 17, 11, 24, 1, 5,
  274. 3, 28, 15, 6, 21, 10,
  275. 23, 19, 12, 4, 26, 8,
  276. 16, 7, 27, 20, 13, 2,
  277. 41, 52, 31, 37, 47, 55,
  278. 30, 40, 51, 45, 33, 48,
  279. 44, 49, 39, 56, 34, 53,
  280. 46, 42, 50, 36, 29, 32]
  281.  
  282. # Splitting
  283. left = key[0:28] # rounded_key_binary for RoundKeys in binary
  284. right = key[28:56] # rk for RoundKeys in hexadecimal
  285.  
  286. rounded_key_binary = []
  287. rk = []
  288. for i in range(0, 16):
  289. # Shifting the bits by nth shifts by checking from shift table
  290. left = shift_left(left, shift_table[i])
  291. right = shift_left(right, shift_table[i])
  292.  
  293. # Combination of left and right string
  294. combine_str = left + right
  295.  
  296. # conversionression of key from 56 to 48 bits
  297. round_key = permute(combine_str, key_coconversion, 48)
  298.  
  299. rounded_key_binary.append(round_key)
  300. rk.append(bin2hex(round_key))
  301.  
  302. print("Encryption")
  303. cipher_text = bin2hex(encrypt(pt, rounded_key_binary, rk))
  304. print("Cipher Text : ", cipher_text)
  305.  
  306. print("Decryption")
  307. rounded_key_binary_rev = rounded_key_binary[::-1]
  308. rk_rev = rk[::-1]
  309. text = bin2hex(encrypt(cipher_text, rounded_key_binary_rev, rk_rev))
  310. print("Plain Text : ", text)
  311.  
  312.  
  313.  
Success #stdin #stdout 0.04s 9960KB
stdin
Standard input is empty
stdout
1010101010111011000010010001100000100111001101101100110011011101
64
After permutations:  11000011110000000011001110100011001111110000110011111010
56
Encryption
After initial permutation 14A7D67818CA18AD
Round  1   18CA18AD   5A78E394   194CD072DE8C
Round  2   5A78E394   4A1210F6   4568581ABCCE
Round  3   4A1210F6   B8089591   06EDA4ACF5B5
Round  4   B8089591   236779C2   DA2D032B6EE3
Round  5   236779C2   A15A4B87   69A629FEC913
Round  6   A15A4B87   2E8F9C65   C1948E87475E
Round  7   2E8F9C65   A9FC20A3   708AD2DDB3C0
Round  8   A9FC20A3   308BEE97   34F822F0C66D
Round  9   308BEE97   10AF9D37   84BB4473DCCC
Round  10   10AF9D37   6CA6CB20   02765708B5BF
Round  11   6CA6CB20   FF3C485F   6D5560AF7CA5
Round  12   FF3C485F   22A5963B   C2C1E96A4BF3
Round  13   22A5963B   387CCDAA   99C31397C91F
Round  14   387CCDAA   BD2DD2AB   251B8BC717D0
Round  15   BD2DD2AB   CF26B472   3330C5D9A36D
Round  16   19BA9212   CF26B472   181C5D75C66D
Cipher Text :  C0B7A8D05F3A829C
Decryption
After initial permutation 19BA9212CF26B472
Round  1   CF26B472   BD2DD2AB   181C5D75C66D
Round  2   BD2DD2AB   387CCDAA   3330C5D9A36D
Round  3   387CCDAA   22A5963B   251B8BC717D0
Round  4   22A5963B   FF3C485F   99C31397C91F
Round  5   FF3C485F   6CA6CB20   C2C1E96A4BF3
Round  6   6CA6CB20   10AF9D37   6D5560AF7CA5
Round  7   10AF9D37   308BEE97   02765708B5BF
Round  8   308BEE97   A9FC20A3   84BB4473DCCC
Round  9   A9FC20A3   2E8F9C65   34F822F0C66D
Round  10   2E8F9C65   A15A4B87   708AD2DDB3C0
Round  11   A15A4B87   236779C2   C1948E87475E
Round  12   236779C2   B8089591   69A629FEC913
Round  13   B8089591   4A1210F6   DA2D032B6EE3
Round  14   4A1210F6   5A78E394   06EDA4ACF5B5
Round  15   5A78E394   18CA18AD   4568581ABCCE
Round  16   14A7D678   18CA18AD   194CD072DE8C
Plain Text :  123456ABCD132536