[text] AD

Viewer

  1. AES
  2.  
  3. REC
  4.  
  5.  
  6. import socket
  7. INV_S_BOX_STRING = '52 09 6a d5 30 36 a5 38 bf 40 a3 9e 81 f3 d7 fb' \
  8.                    '7c e3 39 82 9b 2f ff 87 34 8e 43 44 c4 de e9 cb' \
  9.                    '54 7b 94 32 a6 c2 23 3d ee 4c 95 0b 42 fa c3 4e' \
  10.                    '08 2e a1 66 28 d9 24 b2 76 5b a2 49 6d 8b d1 25' \
  11.                    '72 f8 f6 64 86 68 98 16 d4 a4 5c cc 5d 65 b6 92' \
  12.                    '6c 70 48 50 fd ed b9 da 5e 15 46 57 a7 8d 9d 84' \
  13.                    '90 d8 ab 00 8c bc d3 0a f7 e4 58 05 b8 b3 45 06' \
  14.                    'd0 2c 1e 8f ca 3f 0f 02 c1 af bd 03 01 13 8a 6b' \
  15.                    '3a 91 11 41 4f 67 dc ea 97 f2 cf ce f0 b4 e6 73' \
  16.                    '96 ac 74 22 e7 ad 35 85 e2 f9 37 e8 1c 75 df 6e' \
  17.                    '47 f1 1a 71 1d 29 c5 89 6f b7 62 0e aa 18 be 1b' \
  18.                    'fc 56 3e 4b c6 d2 79 20 9a db c0 fe 78 cd 5a f4' \
  19.                    '1f dd a8 33 88 07 c7 31 b1 12 10 59 27 80 ec 5f' \
  20.                    '60 51 7f a9 19 b5 4a 0d 2d e5 7a 9f 93 c9 9c ef' \
  21.                    'a0 e0 3b 4d ae 2a f5 b0 c8 eb bb 3c 83 53 99 61' \
  22.                    '17 2b 04 7e ba 77 d6 26 e1 69 14 63 55 21 0c 7d'.replace(" ", "")
  23.                    
  24. S_BOX_STRING = '63 7c 77 7b f2 6b 6f c5 30 01 67 2b fe d7 ab 76' \
  25.                'ca 82 c9 7d fa 59 47 f0 ad d4 a2 af 9c a4 72 c0' \
  26.                'b7 fd 93 26 36 3f f7 cc 34 a5 e5 f1 71 d8 31 15' \
  27.                '09 83 2c 1a 1b 6e 5a a0 52 3b d6 b3 29 e3 2f 84' \
  28.                '53 d1 00 ed 20 fc b1 5b 6a cb be 39 4a 4c 58 cf' \
  29.                'd0 ef aa fb 43 4d 33 85 45 f9 02 7f 50 3c 9f a8' \
  30.                '51 a3 40 8f 92 9d 38 f5 bc b6 da 21 10 ff f3 d2' \
  31.                'cd 0c 13 ec 5f 97 44 17 c4 a7 7e 3d 64 5d 19 73' \
  32.                '60 81 4f dc 22 2a 90 88 46 ee b8 14 de 5e 0b db' \
  33.                'e0 32 3a 0a 49 06 24 5c c2 d3 ac 62 91 95 e4 79' \
  34.                'e7 c8 37 6d 8d d5 4e a9 6c 56 f4 ea 65 7a ae 08' \
  35.                'ba 78 25 2e 1c a6 b4 c6 e8 dd 74 1f 4b bd 8b 8a' \
  36.                '70 3e b5 66 48 03 f6 0e 61 35 57 b9 86 c1 1d 9e' \
  37.                'e1 f8 98 11 69 d9 8e 94 9b 1e 87 e9 ce 55 28 df' \
  38.                '8c a1 89 0d bf e6 42 68 41 99 2d 0f b0 54 bb 16'.replace(" ", "")
  39.                
  40.                
  41. S_BOX = bytearray.fromhex(S_BOX_STRING)
  42.  
  43.  
  44. INV_S_BOX = bytearray.fromhex(INV_S_BOX_STRING)
  45.  
  46.  
  47. def inv_shift_rows(state: [[int]]) -> [[int]]:
  48.     state[1][1], state[2][1], state[3][1], state[0][1] = state[0][1], state[1][1], state[2][1], state[3][1]
  49.     state[2][2], state[3][2], state[0][2], state[1][2] = state[0][2], state[1][2], state[2][2], state[3][2]
  50.     state[3][3], state[0][3], state[1][3], state[2][3] = state[0][3], state[1][3], state[2][3], state[3][3]
  51.     return
  52.  
  53.  
  54. def inv_sub_bytes(state: [[int]]) -> [[int]]:
  55.     for r in range(len(state)):
  56.         state[r] = [INV_S_BOX[state[r][c]] for c in range(len(state[0]))]
  57.        
  58.  
  59.  
  60.        
  61. def xtime(a: int) -> int:
  62.     if a & 0x80:
  63.         return ((a << 1) ^ 0x1b) & 0xff
  64.     return a << 1
  65.  
  66.  
  67. def xtimes_0e(b):
  68.     # 0x0e = 14 = b1110 = ((x * 2 + x) * 2 + x) * 2
  69.     return xtime(xtime(xtime(b) ^ b) ^ b)
  70.  
  71.  
  72.  
  73.  
  74. def xtimes_0b(b):
  75.     # 0x0b = 11 = b1011 = ((x*2)*2+x)*2+x
  76.     return xtime(xtime(xtime(b)) ^ b) ^ b
  77.  
  78.  
  79.  
  80.  
  81. def xtimes_0d(b):
  82.     # 0x0d = 13 = b1101 = ((x*2+x)*2)*2+x
  83.     return xtime(xtime(xtime(b) ^ b)) ^ b
  84.  
  85.  
  86.  
  87.  
  88. def xtimes_09(b):
  89.     # 0x09 = 9  = b1001 = ((x*2)*2)*2+x
  90.     return xtime(xtime(xtime(b))) ^ b
  91.  
  92.  
  93. def mix_column(col: [int]):
  94.     c_0 = col[0]
  95.     all_xor = col[0] ^ col[1] ^ col[2] ^ col[3]
  96.     col[0] ^= all_xor ^ xtime(col[0] ^ col[1])
  97.     col[1] ^= all_xor ^ xtime(col[1] ^ col[2])
  98.     col[2] ^= all_xor ^ xtime(col[2] ^ col[3])
  99.     col[3] ^= all_xor ^ xtime(c_0 ^ col[3])
  100.  
  101.  
  102. def mix_columns(state: [[int]]):
  103.     for r in state:
  104.         mix_column(r)
  105.  
  106.  
  107. def state_from_bytes(data: bytes) -> [[int]]:
  108.     state = [data[i*4:(i+1)*4] for i in range(len(data) // 4)]
  109.     return state
  110.  
  111.  
  112.  
  113.  
  114. def bytes_from_state(state: [[int]]) -> bytes:
  115.     return bytes(state[0] + state[1] + state[2] + state[3])
  116.  
  117.  
  118. def add_round_key(state: [[int]], key_schedule: [[[int]]], round: int):
  119.     round_key = key_schedule[round]
  120.     for r in range(len(state)):
  121.         state[r] = [state[r][c] ^ round_key[r][c] for c in range(len(state[0]))]
  122.  
  123.  
  124. def inv_mix_column(col: [int]):
  125.     c_0, c_1, c_2, c_3 = col[0], col[1], col[2], col[3]
  126.     col[0] = xtimes_0e(c_0) ^ xtimes_0b(c_1) ^ xtimes_0d(c_2) ^ xtimes_09(c_3)
  127.     col[1] = xtimes_09(c_0) ^ xtimes_0e(c_1) ^ xtimes_0b(c_2) ^ xtimes_0d(c_3)
  128.     col[2] = xtimes_0d(c_0) ^ xtimes_09(c_1) ^ xtimes_0e(c_2) ^ xtimes_0b(c_3)
  129.     col[3] = xtimes_0b(c_0) ^ xtimes_0d(c_1) ^ xtimes_09(c_2) ^ xtimes_0e(c_3)
  130.  
  131.  
  132.  
  133.  
  134. def inv_mix_columns(state: [[int]]) -> [[int]]:
  135.     for g in state:
  136.         inv_mix_column(r)
  137.  
  138.  
  139.  
  140.  
  141. def inv_mix_column_optimized(col: [int]):
  142.     u = xtime(xtime(col[0] ^ col[2]))
  143.     v = xtime(xtime(col[1] ^ col[3]))
  144.     col[0] ^= u
  145.     col[1] ^= v
  146.     col[2] ^= u
  147.     col[3] ^= v
  148.  
  149.  
  150.  
  151.  
  152. def inv_mix_columns_optimized(state: [[int]]) -> [[int]]:
  153.     for r in state:
  154.         inv_mix_column_optimized(r)
  155.     mix_columns(state)
  156.    
  157. def xor_bytes(a: bytes, b: bytes) -> bytes:
  158.     return bytes([x ^ y for (x, y) in zip(a, b)])
  159.  
  160.  
  161.  
  162.  
  163. def rot_word(word: [int]) -> [int]:
  164.     return word[1:] + word[:1]
  165.  
  166.  
  167. def sub_word(word: [int]) -> bytes:
  168.     substituted_word = bytes(S_BOX[i] for i in word)
  169.     return substituted_word
  170.  
  171.  
  172.  
  173.  
  174. def rcon(i: int) -> bytes:
  175.     rcon_lookup = bytearray.fromhex('0102040810204081b36')
  176.     rcon_value = bytes([rcon_lookup[i-1], 0, 0, 0])
  177.     return rcon_value
  178.    
  179. def key_expansion(key: bytes, nb: int = 4) -> [[[int]]]:
  180.  
  181.  
  182.     nk = len(key) // 4
  183.  
  184.  
  185.     key_bit_length = len(key) * 8
  186.  
  187.  
  188.     if key_bit_length == 128:
  189.         nr = 10
  190.     elif key_bit_length == 192:
  191.         nr = 12
  192.     else:  # 256-bit keys
  193.         nr = 14
  194.  
  195.  
  196.     w = state_from_bytes(key)
  197.  
  198.  
  199.     for i in range(nk, nb * (nr + 1)):
  200.         temp = w[i-1]
  201.         if i % nk == 0:
  202.             temp = xor_bytes(sub_word(rot_word(temp)), rcon(i // nk))
  203.         elif nk > 6 and i % nk == 4:
  204.             temp = sub_word(temp)
  205.         w.append(xor_bytes(w[i - nk], temp))
  206.  
  207.  
  208.     return [w[i*4:(i+1)*4] for i in range(len(w) // 4)]
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. def aes_decryption(cipher: bytes, key: bytes) -> bytes:
  216.  
  217.  
  218.     key_byte_length = len(key)
  219.     key_bit_length = key_byte_length * 8
  220.     nk = key_byte_length // 4
  221.     if key_bit_length == 128:
  222.         nr = 10
  223.     elif key_bit_length == 192:
  224.         nr = 12
  225.     else:  # 256-bit keys
  226.         nr = 14
  227.  
  228.  
  229.     state = state_from_bytes(cipher)
  230.     key_schedule = key_expansion(key)
  231.     add_round_key(state, key_schedule, round=nr)
  232.     for round in range(nr, 0, -1):
  233.         inv_shift_rows(state)
  234.         inv_sub_bytes(state)
  235.         add_round_key(state, key_schedule, round)
  236.         inv_mix_columns(state)
  237.         print("Round ", round, " : ", state)
  238.  
  239.  
  240.     inv_shift_rows(state)
  241.     inv_sub_bytes(state)
  242.     add_round_key(state, key_schedule, round=0)
  243.     plain = bytes_from_state(state)
  244.     return plain
  245.  
  246.  
  247. key = bytearray.fromhex('00010203040060708090a0b0c0d0e0f')
  248. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  249. s.bind(('127.0.0.1', 1111))
  250. s.listen(1)
  251. conn, addr = s.accept()
  252.  
  253.  
  254. data = conn.recv(1024)
  255. print("Received Encrypted Data: ", data)
  256. print("Decrypted Data: ", aes_decryption(data, key).decode('utf-8'))
  257. conn.close()
  258. #1st run receiver.py tehn run sender.py then put in the value in the sender.py and see the output in the receiver.py
  259.  
  260.  
  261.  
  262. SEN
  263. import socket
  264.  
  265.  
  266. # CONSTANTS
  267.  
  268.  
  269. S_BOX_STRING = '63 7c 77 7b f2 6b 6f c5 30 01 67 2b fe d7 ab 76' \
  270.                'ca 82 c9 7d fa 59 47 f0 ad d4 a2 af 9c a4 72 c0' \
  271.                'b7 fd 93 26 36 3f f7 cc 34 a5 e5 f1 71 d8 31 15' \
  272.                '04 c7 23 c3 18 96 05 9a 07 12 80 e2 eb 27 b2 75' \
  273.                '09 83 2c 1a 1b 6e 5a a0 52 3b d6 b3 29 e3 2f 84' \
  274.                '53 d1 00 ed 20 fc b1 5b 6a cb be 39 4a 4c 58 cf' \
  275.                'd0 ef aa fb 43 4d 33 85 45 f9 02 7f 50 3c 9f a8' \
  276.                '51 a3 40 8f 92 9d 38 f5 bc b6 da 21 10 ff f3 d2' \
  277.                'cd 0c 13 ec 5f 97 44 17 c4 a7 7e 3d 64 5d 19 73' \
  278.                '60 81 4f dc 22 2a 90 88 46 ee b8 14 de 5e 0b db' \
  279.                'e0 32 3a 0a 49 06 24 5c c2 d3 ac 62 91 95 e4 79' \
  280.                'e7 c8 37 6d 8d d5 4e a9 6c 56 f4 ea 65 7a ae 08' \
  281.                'ba 78 25 2e 1c a6 b4 c6 e8 dd 74 1f 4b bd 8b 8a' \
  282.                '70 3e b5 66 48 03 f6 0e 61 35 57 b9 86 c1 1d 9e' \
  283.                '8c a1 89 0d bf e6 42 68 41 99 2d 0f b0 54 bb 16'.replace(" ", "")
  284. S_BOX = bytearray.fromhex(S_BOX_STRING)
  285.  
  286.  
  287.  
  288.  
  289. def sub_word(word: [int]) -> bytes:
  290.     substituted_word = bytes(S_BOX[i] for i in word)
  291.     return substituted_word
  292.  
  293.  
  294. def rcon(i: int) -> bytes:
  295.     rcon_lookup = bytearray.fromhex('0100408102040801b36')
  296.     rcon_value = bytes([rcon_lookup[i-1], 0, 0, 0])
  297.     return rcon_value
  298.  
  299.  
  300.  
  301.  
  302. def xor_bytes(a: bytes, b: bytes) -> bytes:
  303.     return bytes([x ^ y for (x, y) in zip(a, b)])
  304.  
  305.  
  306.  
  307.  
  308. def rot_word(word: [int]) -> [int]:
  309.     return word[1:] + word[:1]
  310.  
  311.  
  312.  
  313.  
  314. def key_expansion(key: bytes, nb: int = 4) -> [[[int]]]:
  315.  
  316.  
  317.     nk = len(key) // 4
  318.  
  319.  
  320.     key_bit_length = len(key) * 8
  321.  
  322.  
  323.     if key_bit_length == 128:
  324.         nr = 10
  325.     elif key_bit_length == 192:
  326.         nr = 12
  327.     else:  # 256-bit keys
  328.         nr = 14
  329.  
  330.  
  331.     w = state_from_bytes(key)
  332.  
  333.  
  334.     for i in range(nk, nb * (nr + 1)):
  335.         temp = w[i-1]
  336.         if i % nk == 0:
  337.             temp = xor_bytes(sub_word(rot_word(temp)), rcon(i // nk))
  338.         elif nk > 6 and i % nk == 4:
  339.             temp = sub_word(temp)
  340.         w.append(xor_bytes(w[i - nk], temp))
  341.  
  342.  
  343.     return [w[i*4:(i+1)*4] for i in range(len(w) // 4)]
  344.  
  345.  
  346.  
  347.  
  348. def add_round_key(state: [[int]], key_schedule: [[[int]]], round: int):
  349.     round_key = key_schedule[round]
  350.     for r in range(len(state)):
  351.         state[r] = [state[r][c] ^ round_key[r][c] for c in range(len(state[0]))]
  352.  
  353.  
  354.  
  355.  
  356. def sub_bytes(state: [[int]]):
  357.     for r in range(len(state)):
  358.         state[r] = [S_BOX[state[r][c]] for c in range(len(state[0]))]
  359.  
  360.  
  361.  
  362.  
  363. def shift_rows(state: [[int]]):
  364.     state[0][1], state[1][1], state[2][1], state[3][1] = state[1][1], state[2][1], state[3][1], state[0][1]
  365.     state[0][2], state[1][2], state[2][2], state[3][2] = state[2][2], state[3][2], state[0][2], state[1][2]
  366.     state[0][3], state[1][3], state[2][3], state[3][3] = state[3][3], state[0][3], state[1][3], state[2][3]
  367.  
  368.  
  369.  
  370.  
  371. def xtime(a: int) -> int:
  372.     if a & 0x80:
  373.         return ((a << 1) ^ 0x1b) & 0xff
  374.     return a << 1
  375.  
  376.  
  377.  
  378.  
  379. def mix_column(col: [int]):
  380.     c_0 = col[0]
  381.     all_xor = col[0] ^ col[1] ^ col[2] ^ col[3]
  382.     col[0] ^= all_xor ^ xtime(col[0] ^ col[1])
  383.     col[1] ^= all_xor ^ xtime(col[1] ^ col[2])
  384.     col[2] ^= all_xor ^ xtime(col[2] ^ col[3])
  385.     col[3] ^= all_xor ^ xtime(c_0 ^ col[3])
  386.  
  387.  
  388.  
  389.  
  390. def mix_columns(state: [[int]]):
  391.     for r in state:
  392.         mix_column(r)
  393.  
  394.  
  395.  
  396.  
  397. def state_from_bytes(data: bytes) -> [[int]]:
  398.     state = [data[i*4:(i+1)*4] for i in range(len(data) // 4)]
  399.     return state
  400.  
  401.  
  402.  
  403.  
  404. def bytes_from_state(state: [[int]]) -> bytes:
  405.     return bytes(state[0] + state[1] + state[2] + state[3])
  406.  
  407.  
  408.  
  409.  
  410. def aes_encryption(data: bytes, key: bytes) -> bytes:
  411.     state = state_from_bytes(data)
  412.     key_schedule = key_expansion(key)
  413.     add_round_key(state, key_schedule, round=0)
  414.  
  415.  
  416.     for round in range(1, 11):
  417.         sub_bytes(state)
  418.         shift_rows(state)
  419.         mix_columns(state)
  420.         add_round_key(state, key_schedule, round)
  421.         print("Round ", round, " : ", state)
  422.  
  423.  
  424.     sub_bytes(state)
  425.     shift_rows(state)
  426.     add_round_key(state, key_schedule, round=10)
  427.  
  428.  
  429.     cipher = bytes_from_state(state)
  430.     return cipher
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439. if name == "main":
  440.    
  441.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  442.     server_address = ('127.0.0.1', 1111)
  443.     s.connect(server_address)
  444.     plaintext = input("Enter the plaintext: ")
  445.  
  446.  
  447.     block_size = 16
  448.     padding_length = block_size - (len(plaintext) % block_size)
  449.     plaintext += chr(padding_length) * padding_length
  450.  
  451.  
  452.  
  453.  
  454.     plaintext_bytes = plaintext.encode()
  455.  
  456.  
  457.     key = bytearray.fromhex('00010203040060708090a0b0c0d0e0f')
  458.     ciphertext = aes_encryption(plaintext_bytes, key)
  459.     print("Ciphertext (hex): ", ciphertext.hex())
  460.     s.sendall(ciphertext)
  461.  
  462.  
  463.  
  464.  
  465.  
  466. DES
  467.  
  468. CLIENT
  469. import socket
  470. INITIAL_PERM = [58, 50, 42, 34, 26, 18, 10, 2,
  471.                 60, 52, 44, 36, 28, 20, 12, 4,
  472.                 62, 54, 46, 38, 30, 22, 14, 6,
  473.                 64, 56, 48, 40, 32, 24, 16, 8,
  474.                 57, 49, 41, 33, 25, 17, 9, 1,
  475.                 59, 51, 43, 35, 27, 19, 11, 3,
  476.                 61, 53, 45, 37, 29, 21, 13, 5,
  477.                 63, 55, 47, 39, 31, 23, 15, 7]
  478.  
  479.  
  480. D = [32, 1, 2, 3, 4, 5, 4, 5,
  481.         6, 7, 8, 9, 8, 9, 10, 11,
  482.         12, 13, 12, 13, 14, 15, 16, 17,
  483.         16, 17, 18, 19, 20, 21, 20, 21,
  484.         22, 23, 24, 25, 24, 25, 26, 27,
  485.         28, 29, 28, 29, 30, 31, 32, 1]
  486.  
  487.  
  488. PERM = [16, 7, 20, 21,
  489.     29, 12, 28, 17,
  490.     1, 15, 23, 26,
  491.     5, 18, 31, 10,
  492.     2, 8, 24, 14,
  493.     32, 27, 3, 9,
  494.     19, 13, 30, 6,
  495.     22, 11, 4, 25]
  496.  
  497.  
  498.  
  499.  
  500. SBOX = [[[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
  501.         [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
  502.         [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
  503.         [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]],
  504.  
  505.  
  506.         [[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
  507.         [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
  508.         [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
  509.         [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]],
  510.  
  511.  
  512.         [[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
  513.         [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
  514.         [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
  515.         [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]],
  516.  
  517.  
  518.         [[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
  519.         [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
  520.         [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
  521.         [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]],
  522.  
  523.  
  524.         [[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
  525.         [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
  526.         [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
  527.         [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]],
  528.  
  529.  
  530.         [[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
  531.         [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
  532.         [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
  533.         [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]],
  534.  
  535.  
  536.         [[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
  537.         [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
  538.         [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
  539.         [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]],
  540.  
  541.  
  542.         [[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
  543.         [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
  544.         [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
  545.         [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]]
  546.  
  547.  
  548. FPERM = [40, 8, 48, 16, 56, 24, 64, 32,
  549.             39, 7, 47, 15, 55, 23, 63, 31,
  550.             38, 6, 46, 14, 54, 22, 62, 30,
  551.             37, 5, 45, 13, 53, 21, 61, 29,
  552.             36, 4, 44, 12, 52, 20, 60, 28,
  553.             35, 3, 43, 11, 51, 19, 59, 27,
  554.             33, 1, 41, 9, 49, 17, 57, 25]
  555.  
  556.  
  557. # --parity bit drop table
  558. KEYP = [57, 49, 41, 33, 25, 17, 9,
  559.         1, 58, 50, 42, 34, 26, 18,
  560.         10, 2, 59, 51, 43, 35, 27,
  561.         19, 11, 3, 60, 52, 44, 36,
  562.         63, 55, 47, 39, 31, 23, 15,
  563.         7, 62, 54, 46, 38, 30, 22,
  564.         14, 6, 61, 53, 45, 37, 29,
  565.         21, 13, 5, 28, 20, 12, 4]
  566.  
  567.  
  568. # Number of bit shifts
  569. SHIFT_TABLE = [1, 1, 2, 2,
  570.             2, 2, 2, 2,
  571.             1, 2, 2, 2,
  572.             2, 2, 2, 1]
  573.  
  574.  
  575. # Key- Compression Table : Compression of key from 56 bits to 48 bits
  576. KEY_COMP = [14, 17, 11, 24, 1, 5,
  577.             3, 28, 15, 6, 21, 10,
  578.             23, 19, 12, 4, 26, 8,
  579.             16, 7, 27, 20, 13, 2,
  580.             41, 52, 31, 37, 47, 55,
  581.             30, 40, 51, 45, 33, 48,
  582.             44, 49, 39, 56, 34, 53,
  583.             46, 42, 50, 36, 29, 32]
  584.  
  585.  
  586.  
  587.  
  588. def hex2bin(s):
  589.     mp = {'0': "0000",
  590.         '1': "0001",
  591.         '2': "0010",
  592.         '3': "0011",
  593.         '4': "0100",
  594.         '5': "0101",
  595.         '6': "0110",
  596.         '7': "0111",
  597.         '8': "1000",
  598.         '9': "1001",
  599.         'A': "1010",
  600.         'B': "1011",
  601.         'C': "1100",
  602.         'D': "1101",
  603.         'E': "1110",
  604.         'F': "1111"}
  605.     bin = ""
  606.     for i in range(len(s)):
  607.         bin = bin + mp[s[i]]
  608.     return bin
  609.  
  610.  
  611. def bin2hex(s):
  612.     mp = {"0000": '0',
  613.         "0001": '1',
  614.         "0010": '2',
  615.         "0011": '3',
  616.         "0100": '4',
  617.         "0101": '5',
  618.         "0110": '6',
  619.         "0111": '7',
  620.         "1000": '8',
  621.         "1001": '9',
  622.         "1010": 'A',
  623.         "1011": 'B',
  624.         "1100": 'C',
  625.         "1101": 'D',
  626.         "1110": 'E',
  627.         "1111": 'F'}
  628.     hex = ""
  629.     for i in range(0, len(s), 4):
  630.         ch = ""
  631.         ch = ch + s[i]
  632.         ch = ch + s[i + 1]
  633.         ch = ch + s[i + 2]
  634.         ch = ch + s[i + 3]
  635.         hex = hex + mp[ch]
  636.  
  637.  
  638.     return hex
  639.  
  640.  
  641. def bin2dec(binary):
  642.  
  643.  
  644.     binary1 = binary
  645.     decimal, i, n = 0, 0, 0
  646.     while(binary != 0):
  647.         dec = binary % 10
  648.         decimal = decimal + dec * pow(2, i)
  649.         binary = binary//10
  650.         i += 1
  651.     return decimal
  652.  
  653.  
  654. def dec2bin(num):
  655.     res = bin(num).replace("0b", "")
  656.     if(len(res) % 4 != 0):
  657.         div = len(res) / 4
  658.         div = int(div)
  659.         counter = (4 * (div + 1)) - len(res)
  660.         for i in range(0, counter):
  661.             res = '0' + res
  662.     return res
  663.  
  664.  
  665. def permute(k, arr, n):
  666.     permutation = ""
  667.     for i in range(0, n):
  668.         permutation = permutation + k[arr[i] - 1]
  669.     return permutation
  670.  
  671.  
  672. def shift_left(k, nth_shifts):
  673.     s = ""
  674.     for i in range(nth_shifts):
  675.         for j in range(1, len(k)):
  676.             s = s + k[j]
  677.         s = s + k[0]
  678.         k = s
  679.         s = ""
  680.     return k
  681.  
  682.  
  683. def xor(a, b):
  684.     ans = ""
  685.     for i in range(len(a)):
  686.         if a[i] == b[i]:
  687.             ans = ans + "0"
  688.         else:
  689.             ans = ans + "1"
  690.     return ans
  691.  
  692.  
  693.  
  694.  
  695. def encrypt(pt, rkb, rk):
  696.     pt = hex2bin(pt)
  697.  
  698.  
  699.  
  700.  
  701.     pt = permute(pt, INITIAL_PERM, 64)
  702.     print("After initial permutation", bin2hex(pt))
  703.  
  704.  
  705.  
  706.  
  707.     left = pt[0:32]
  708.     right = pt[32:64]
  709.     for i in range(0, 16):
  710.    
  711.         right_expanded = permute(right, D, 48)
  712.  
  713.  
  714.         xor_x = xor(right_expanded, rkb[i])
  715.  
  716.  
  717.    
  718.         sbox_str = ""
  719.         for j in range(0, 8):
  720.             row = bin2dec(int(xor_x[j * 6] + xor_x[j * 6 + 5]))
  721.             col = bin2dec(
  722.                 int(xor_x[j * 6 + 1] + xor_x[j * 6 + 2] + xor_x[j * 6 + 3] + xor_x[j * 6 + 4]))
  723.             val = SBOX[j][row][col]
  724.             sbox_str = sbox_str + dec2bin(val)
  725.  
  726.  
  727.  
  728.  
  729.         sbox_str = permute(sbox_str, PERM, 32)
  730.  
  731.  
  732.  
  733.  
  734.         result = xor(left, sbox_str)
  735.         left = result
  736.  
  737.  
  738.  
  739.  
  740.         if(i != 15):
  741.             left, right = right, left
  742.         print("Round ", i + 1, " ", bin2hex(left),
  743.             " ", bin2hex(right), " ", rk[i])
  744.  
  745.  
  746.     combine = left + right
  747.  
  748.  
  749.     cipher_text = permute(combine, FPERM, 64)
  750.     return cipher_text
  751.  
  752.  
  753. def keygen(key):
  754.     key = hex2bin(key)
  755.     key = permute(key, KEYP, 56)
  756.  
  757.  
  758.     left = key[0:28]
  759.     right = key[28:56]
  760.     rkb = []
  761.     rk = []
  762.     for i in range(0, 16):
  763.  
  764.  
  765.         left = shift_left(left, SHIFT_TABLE[i])
  766.         right = shift_left(right, SHIFT_TABLE[i])
  767.  
  768.  
  769.         combine_str = left + right
  770.  
  771.  
  772.  
  773.  
  774.         round_key = permute(combine_str, KEY_COMP, 48)
  775.  
  776.  
  777.         rkb.append(round_key)
  778.         rk.append(bin2hex(round_key))
  779.     return rkb, rk
  780.  
  781.  
  782. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  783. port = 1234
  784. ip = '127.0.0.1'
  785. s.connect((ip, port))
  786. data = input("Enter Hexadecimal data to send: ")
  787. key ="AABB09182736CCDD"
  788.  
  789.  
  790. rkb, rk = keygen(key)
  791.  
  792.  
  793. cipher_text = bin2hex(encrypt(data, rkb, rk))
  794. print("Cipher Text : ", cipher_text)
  795. s.send(cipher_text.encode())
  796. s.close()
  797.  
  798.  
  799.  
  800.  
  801. SENDER
  802. import socket
  803.  
  804.  
  805. INITIAL_PERM = [58, 50, 42, 34, 26, 18, 10, 2,
  806.                 60, 52, 44, 36, 28, 20, 12, 4,
  807.                 62, 54, 46, 38, 30, 22, 14, 6,
  808.                 64, 56, 48, 40, 32, 24, 16, 8,
  809.                 57, 49, 41, 33, 25, 17, 9, 1,
  810.                 59, 51, 43, 35, 27, 19, 11, 3,
  811.                 61, 53, 45, 37, 29, 21, 13, 5,
  812.                 63, 55, 47, 39, 31, 23, 15, 7]
  813.  
  814.  
  815. D = [32, 1, 2, 3, 4, 5, 4, 5,
  816.         6, 7, 8, 9, 8, 9, 10, 11,
  817.         12, 13, 12, 13, 14, 15, 16, 17,
  818.         16, 17, 18, 19, 20, 21, 20, 21,
  819.         22, 23, 24, 25, 24, 25, 26, 27,
  820.         28, 29, 28, 29, 30, 31, 32, 1]
  821.  
  822.  
  823. PERM = [16, 7, 20, 21,
  824.     29, 12, 28, 17,
  825.     1, 15, 23, 26,
  826.     5, 18, 31, 10,
  827.     2, 8, 24, 14,
  828.     32, 27, 3, 9,
  829.     19, 13, 30, 6,
  830.     22, 11, 4, 25]
  831.  
  832.  
  833.  
  834.  
  835. SBOX = [[[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
  836.         [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
  837.         [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
  838.         [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13]],
  839.  
  840.  
  841.         [[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
  842.         [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
  843.         [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
  844.         [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9]],
  845.  
  846.  
  847.         [[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
  848.         [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
  849.         [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
  850.         [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12]],
  851.  
  852.  
  853.         [[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
  854.         [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
  855.         [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
  856.         [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14]],
  857.  
  858.  
  859.         [[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
  860.         [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
  861.         [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
  862.         [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3]],
  863.  
  864.  
  865.         [[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
  866.         [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
  867.         [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
  868.         [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13]],
  869.  
  870.  
  871.         [[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
  872.         [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
  873.         [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
  874.         [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]],
  875.  
  876.  
  877.         [[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
  878.         [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
  879.         [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
  880.         [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]]
  881.  
  882.  
  883. FPERM = [40, 8, 48, 16, 56, 24, 64, 32,
  884.             39, 7, 47, 15, 55, 23, 63, 31,
  885.             38, 6, 46, 14, 54, 22, 62, 30,
  886.             37, 5, 45, 13, 53, 21, 61, 29,
  887.             36, 4, 44, 12, 52, 20, 60, 28,
  888.             35, 3, 43, 11, 51, 19, 59, 27,
  889.             34, 2, 42, 10, 50, 18, 58, 26,
  890.             33, 1, 41, 9, 49, 17, 57, 25]
  891.  
  892.  
  893. # --parity bit drop table
  894. KEYP = [57, 49, 41, 33, 25, 17, 9,
  895.         1, 58, 50, 42, 34, 26, 18,
  896.         10, 2, 59, 51, 43, 35, 27,
  897.         19, 11, 3, 60, 52, 44, 36,
  898.         63, 55, 47, 39, 31, 23, 15,
  899.         7, 62, 54, 46, 38, 30, 22,
  900.         14, 6, 61, 53, 45, 37, 29,
  901.         21, 13, 5, 28, 20, 12, 4]
  902.  
  903.  
  904. # Number of bit shifts
  905. SHIFT_TABLE = [1, 1, 2, 2,
  906.             2, 2, 2, 2,
  907.             1, 2, 2, 2,
  908.             2, 2, 2, 1]
  909.  
  910.  
  911. # Key- Compression Table : Compression of key from 56 bits to 48 bits
  912. KEY_COMP = [14, 17, 11, 24, 1, 5,
  913.             3, 28, 15, 6, 21, 10,
  914.             23, 19, 12, 4, 26, 8,
  915.             16, 7, 27, 20, 13, 2,
  916.             41, 52, 31, 37, 47, 55,
  917.             30, 40, 51, 45, 33, 48,
  918.             44, 49, 39, 56, 34, 53,
  919.             46, 42, 50, 36, 29, 32]
  920.  
  921.  
  922.  
  923.  
  924. def hex2bin(s):
  925.     mp = {'0': "0000",
  926.         '1': "0001",
  927.         '2': "0010",
  928.         '3': "0011",
  929.         '4': "0100",
  930.         '5': "0101",
  931.         '6': "0110",
  932.         '7': "0111",
  933.         '8': "1000",
  934.         '9': "1001",
  935.         'A': "1010",
  936.         'B': "1011",
  937.         'C': "1100",
  938.         'D': "1101",
  939.         'E': "1110",
  940.         'F': "1111"}
  941.     bin = ""
  942.     for i in range(len(s)):
  943.         bin = bin + mp[s[i]]
  944.     return bin
  945.  
  946.  
  947. def bin2hex(s):
  948.     mp = {"0000": '0',
  949.         "0001": '1',
  950.         "0010": '2',
  951.         "0011": '3',
  952.         "0100": '4',
  953.         "0101": '5',
  954.         "0110": '6',
  955.         "0111": '7',
  956.         "1000": '8',
  957.         "1001": '9',
  958.         "1010": 'A',
  959.         "1011": 'B',
  960.         "1100": 'C',
  961.         "1101": 'D',
  962.         "1110": 'E',
  963.         "1111": 'F'}
  964.     hex = ""
  965.     for i in range(0, len(s), 4):
  966.         ch = ""
  967.         ch = ch + s[i]
  968.         ch = ch + s[i + 1]
  969.         ch = ch + s[i + 2]
  970.         ch = ch + s[i + 3]
  971.         hex = hex + mp[ch]
  972.  
  973.  
  974.     return hex
  975.  
  976.  
  977. def bin2dec(binary):
  978.  
  979.  
  980.     binary1 = binary
  981.     decimal, i, n = 0, 0, 0
  982.     while(binary != 0):
  983.         dec = binary % 10
  984.         decimal = decimal + dec * pow(2, i)
  985.         binary = binary//10
  986.         i += 1
  987.     return decimal
  988.  
  989.  
  990. def dec2bin(num):
  991.     res = bin(num).replace("0b", "")
  992.     if(len(res) % 4 != 0):
  993.         div = len(res) / 4
  994.         div = int(div)
  995.         counter = (4 * (div + 1)) - len(res)
  996.         for i in range(0, counter):
  997.             res = '0' + res
  998.     return res
  999.  
  1000.  
  1001. def permute(k, arr, n):
  1002.     permutation = ""
  1003.     for i in range(0, n):
  1004.         permutation = permutation + k[arr[i] - 1]
  1005.     return permutation
  1006.  
  1007.  
  1008. def shift_left(k, nth_shifts):
  1009.     s = ""
  1010.     for i in range(nth_shifts):
  1011.         for j in range(1, len(k)):
  1012.             s = s + k[j]
  1013.         s = s + k[0]
  1014.         k = s
  1015.         s = ""
  1016.     return k
  1017.  
  1018.  
  1019. def xor(a, b):
  1020.     ans = ""
  1021.     for i in range(len(a)):
  1022.         if a[i] == b[i]:
  1023.             ans = ans + "1"
  1024.         else:
  1025.             ans = ans + "0"
  1026.     return ans
  1027.  
  1028.  
  1029. def encrypt(pt, rkb, rk):
  1030.     pt = hex2bin(pt)
  1031.  
  1032.  
  1033.     # Initial Permutation
  1034.     pt = permute(pt, INITIAL_PERM, 64)
  1035.     print("After initial permutation", bin2hex(pt))
  1036.  
  1037.  
  1038.     # Splitting
  1039.     left = pt[0:32]
  1040.     right = pt[32:64]
  1041.     for i in range(0, 16):
  1042.         # Expansion D-box: Expanding the 32 bits data into 48 bits
  1043.         right_expanded = permute(right, D, 48)
  1044.  
  1045.  
  1046.  
  1047.  
  1048.         xor_x = xor(right_expanded, rkb[i])
  1049.  
  1050.  
  1051.  
  1052.  
  1053.         sbox_str = ""
  1054.         for j in range(0, 8):
  1055.             row = bin2dec(int(xor_x[j * 6] + xor_x[j * 6 + 5]))
  1056.             col = bin2dec(
  1057.                 int(xor_x[j * 6 + 1] + xor_x[j * 6 + 2] + xor_x[j * 6 + 3] + xor_x[j * 6 + 4]))
  1058.             val = SBOX[j][row][col]
  1059.             sbox_str = sbox_str + dec2bin(val)
  1060.  
  1061.  
  1062.  
  1063.  
  1064.         sbox_str = permute(sbox_str, PERM, 32)
  1065.  
  1066.  
  1067.         result = xor(left, sbox_str)
  1068.         left = result
  1069.  
  1070.  
  1071.         if(i != 15):
  1072.             left, right = right, left
  1073.         print("Round ", i + 1, " ", bin2hex(left),
  1074.             " ", bin2hex(right), " ", rk[i])
  1075.  
  1076.  
  1077.     combine = left + right
  1078.  
  1079.  
  1080.     cipher_text = permute(combine, FPERM, 64)
  1081.     return cipher_text
  1082.  
  1083.  
  1084. def keygen(key):
  1085.     key = hex2bin(key)
  1086.  
  1087.  
  1088.     key = permute(key, KEYP, 56)
  1089.  
  1090.  
  1091.     left = key[0:28]
  1092.     right = key[28:56]
  1093.     rkb = []
  1094.     rk = []
  1095.     for i in range(0, 16):
  1096.  
  1097.  
  1098.         left = shift_left(left, SHIFT_TABLE[i])
  1099.         right = shift_left(right, SHIFT_TABLE[i])
  1100.  
  1101.  
  1102.  
  1103.  
  1104.         combine_str = left + right
  1105.  
  1106.  
  1107.  
  1108.  
  1109.         round_key = permute(combine_str, KEY_COMP, 48)
  1110.  
  1111.  
  1112.         rkb.append(round_key)
  1113.         rk.append(bin2hex(round_key))
  1114.     return rkb, rk
  1115.  
  1116.  
  1117. key = input("Enter the hex key: ")
  1118.  
  1119.  
  1120. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1121. port = 1234
  1122. ip = '127.0.0.1'
  1123. s.bind((ip, port))
  1124. s.listen(5)
  1125. c, addr = s.accept()
  1126. data = c.recv(1024).decode()
  1127.  
  1128.  
  1129. rkb, rk = keygen(key)
  1130. print("Decryption")
  1131. rkb_rev = rkb[::-1]
  1132. rk_rev = rk[::-1]
  1133. text = bin2hex(encrypt(data, rkb_rev, rk_rev))
  1134. print("Plain Text : ", text)
  1135.  
  1136.  
  1137.  

Editor

You can edit this paste and save as new:


File Description
  • AD
  • Paste Code
  • 23 Apr-2024
  • 28.28 Kb
You can Share it: