[text] aaaaaaaaaaa

Viewer

copydownloadembedprintName: aaaaaaaaaaa
  1. 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. 2. 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.  
  1138.  
  1139. 3. DIFFY
  1140. ALICE
  1141. import socket
  1142. import math
  1143.  
  1144.  
  1145. def mod_exp(base, exponent, modulus):
  1146.     return pow(base, exponent, modulus)
  1147.  
  1148.  
  1149. def compute_Y(alpha, X, q):
  1150.     return mod_exp(alpha, X, q)
  1151.  
  1152.  
  1153. def compute_shared_key(Y, X, q):
  1154.     return mod_exp(Y, X, q)
  1155.  
  1156.  
  1157. def alice():
  1158.     alpha = 10  # Specified value
  1159.     q = 16     # Specified value
  1160.  
  1161.  
  1162.     X = int(input("Enter XA for Alice: "))
  1163.  
  1164.  
  1165.     YA = compute_Y(alpha, X, q)
  1166.     print(f"YA={YA}")
  1167.  
  1168.  
  1169.     # Communicate with Dart
  1170.     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_dart:
  1171.         s_dart.connect(('localhost', 5001))
  1172.         s_dart.send(','.join([str(x) for x in [alpha, X, q, YA]]).encode('utf-8'))
  1173.  
  1174.  
  1175.         # Receive YD2 from Dart
  1176.         YD2_dart = int(s_dart.recv(1024).decode('utf-8'))
  1177.         print(f"Received YD2 from Dart: {YD2_dart}")
  1178.  
  1179.  
  1180.         # Compute and display K2
  1181.         K2 = compute_shared_key(YD2_dart, X, q)
  1182.         print(f"K2={K2}")
  1183.  
  1184.  
  1185. if name == "main":
  1186.     alice()
  1187. BOB
  1188. import socket
  1189. import math
  1190.  
  1191.  
  1192. def mod_exp(base, exponent, modulus):
  1193.     return pow(base, exponent, modulus)
  1194.  
  1195.  
  1196. def compute_Y(alpha, X, q):
  1197.     return mod_exp(alpha, X, q)
  1198.  
  1199.  
  1200. def compute_shared_key(Y, X, q):
  1201.     return mod_exp(Y, X, q)
  1202.  
  1203.  
  1204. def bob():
  1205.     alpha = 10  # Specified value
  1206.     q = 16     # Specified value
  1207.  
  1208.  
  1209.     XB = int(input("Enter XB for Bob: "))
  1210.  
  1211.  
  1212.     YB = compute_Y(alpha, XB, q)
  1213.     print(f"YB={YB}")
  1214.  
  1215.  
  1216.     # Communicate with Dart
  1217.     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_dart:
  1218.         s_dart.bind(('localhost', 5002))
  1219.         s_dart.connect(1)
  1220.         print("Bob terminal is listening for Dart on port 5002")
  1221.  
  1222.  
  1223.         conn_dart, addr_dart = s_dart.accept()
  1224.         with conn_dart:
  1225.             print(f"Connection established with Dart: {addr_dart}")
  1226.  
  1227.  
  1228.             # Receive YD1 from Dart
  1229.             YD1_dart = int(conn_dart.recv(1024).decode('utf-8'))
  1230.             print(f"Received YD1 from Dart: {YD1_dart}")
  1231.  
  1232.  
  1233.             # Additional communication with Dart can be added here if needed
  1234.  
  1235.  
  1236.             # Compute and display K1
  1237.             K1 = compute_shared_key(YD1_dart, XB, q)
  1238.             print(f"K1={K1}")
  1239.  
  1240.  
  1241. if name == "main":
  1242.     bob()
  1243. DARTH
  1244. import socket
  1245. import math
  1246.  
  1247.  
  1248. def mod_exp(base, exponent, modulus):
  1249.     return pow(base, exponent, modulus)
  1250.  
  1251.  
  1252. def compute_Y(alpha, X, q):
  1253.     return mod_exp(alpha, X, q)
  1254.    
  1255. def compute_k1(YB, XD1, q):
  1256.     return mod_exp(YB, 5, q)  
  1257.    
  1258. def compute_k2(YA, XD2, q):
  1259.     return mod_exp(YA, XD2, q)
  1260.    
  1261. def compute_shared_key(Y, X, q):
  1262.     return mod_exp(Y, X, q)
  1263.  
  1264.  
  1265. def dart():
  1266.     alpha = 15  # Specified value
  1267.     q = 12     # Specified value
  1268.  
  1269.  
  1270.     # Communicate with Alice
  1271.     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_alice:
  1272.         s_alice.bind(('localhost', 5001))
  1273.         s_alice.listen(1)
  1274.         print("Dart terminal is listening for Alice on port 5001")
  1275.  
  1276.  
  1277.         conn_alice, addr_alice = s_alice.accept()
  1278.         with conn_alice:
  1279.             print(f"Connection established with Alice: {addr_alice}")
  1280.             data_alice = conn_alice.recv(1024).decode('utf-8')
  1281.             alpha, X, q, YA = map(int, data_alice.split(','))
  1282.  
  1283.  
  1284.             print(f"Received data from Alice: alpha={alpha}, X={X}, q={q}, YA={YA}")
  1285.  
  1286.  
  1287.             XD1 = int(input("Enter XD1: "))
  1288.             XD2 = int(input("Enter XD2: "))
  1289.  
  1290.  
  1291.             YD1 = compute_Y(alpha, XD1, q)
  1292.             YD2 = compute_Y(alpha, XD2, q)
  1293.  
  1294.  
  1295.             print(f"YD1={YD1}, YD2={YD2}")
  1296.            
  1297.             K1 = compute_k1(YD1, XD1, q)
  1298.             K2 = compute_k2(YD2, X, q)
  1299.  
  1300.  
  1301.             print(f"K1={K1}, K2={K2}")
  1302.  
  1303.  
  1304.             # Send YD2 to Alice
  1305.             conn_alice.send(str(YD2).encode('utf-8'))
  1306.  
  1307.  
  1308.             # Send YD1 to Bob
  1309.             with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_bob:
  1310.                 s_bob.connect(('localhost', 5002))
  1311.                 s_bob.send(str(YD1).encode('utf-8'))
  1312.  
  1313.  
  1314.                 # Additional communication with Bob can be added here if needed
  1315.  
  1316.  
  1317. if name == "main":
  1318.     dart()
  1319.  
  1320.  
  1321.  
  1322. 4. MD5
  1323. import math
  1324.  
  1325.  
  1326. # Define MD5 specific functions
  1327. def F(X, Y, Z):
  1328.     return (X & Y) | (~X & Z)
  1329.  
  1330.  
  1331. def G(X, Y, Z):
  1332.     return (X & Z) | (Y & ~Z)
  1333.  
  1334.  
  1335. def H(X, Y, Z):
  1336.     return X ^ Y ^ Z
  1337.  
  1338.  
  1339. def I(X, Y, Z):
  1340.     return Y ^ (X | ~Z)
  1341.  
  1342.  
  1343. # Define shift and constant values
  1344. s = [
  1345.     7, 12, 17, 22,
  1346.     5, 9, 14, 20,
  1347.     4, 11, 16, 23,
  1348.     6, 10, 15, 21
  1349. ]
  1350.  
  1351.  
  1352. K = [int(abs(math.sin(i + 1)) * 2 ** 32) & 0xFFFFFFFF for i in range(64)]
  1353.  
  1354.  
  1355. # Initialize variables
  1356. A = 0x01234567
  1357. B = 0x89ABCDEF
  1358. C = 0xFEDCBA98
  1359. D = 0x76543210
  1360.  
  1361.  
  1362. def md5(message):
  1363.     global A, B, C, D
  1364.     message = bytearray(message, 'utf-8')
  1365.     for i in range(0, len(message), 64):
  1366.         X = [0] * 16
  1367.         for j in range(16):
  1368.             X[j] = int.from_bytes(message[i + j*4:i + j*4 + 4], byteorder='little')
  1369.        
  1370.         AA, BB, CC, DD = A, B, C, D
  1371.        
  1372.         # Round 1
  1373.         for j in range(16):
  1374.             A = (B + ((A + F(B, C, D) + X[j] + K[j]) & 0xFFFFFFFF) << s[j % 4] | (A + F(B, C, D) + X[j] + K[j]) >> (32 - s[j % 4])) & 0xFFFFFFFF
  1375.             A, B, C, D = D, A, B, C
  1376.         print(f"Round 1 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
  1377.        
  1378.         # Round 2
  1379.         for j in range(16):
  1380.             A = (B + ((A + G(B, C, D) + X[(1 + 5*j) % 16] + K[16+j]) & 0xFFFFFFFF) << s[j % 4 + 4] | (A + G(B, C, D) + X[(1 + 5*j) % 16] + K[16+j]) >> (32 - s[j % 4 + 4])) & 0xFFFFFFFF
  1381.             A, B, C, D = D, A, B, C
  1382.         print(f"Round 2 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
  1383.        
  1384.         # Round 3
  1385.         for j in range(16):
  1386.             A = (B + ((A + H(B, C, D) + X[(5 + 3*j) % 16] + K[32+j]) & 0xFFFFFFFF) << s[j % 4 + 8] | (A + H(B, C, D) + X[(5 + 3*j) % 16] + K[32+j]) >> (32 - s[j % 4 + 8])) & 0xFFFFFFFF
  1387.             A, B, C, D = D, A, B, C
  1388.         print(f"Round 3 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
  1389.        
  1390.         # Round 4
  1391.         for j in range(16):
  1392.             A = (B + ((A + I(B, C, D) + X[(7*j) % 16] + K[48+j]) & 0xFFFFFFFF) << s[j % 4 + 12] | (A + I(B, C, D) + X[(7*j) % 16] + K[48+j]) >> (32 - s[j % 4 + 12])) & 0xFFFFFFFF
  1393.             A, B, C, D = D, A, B, C
  1394.         print(f"Round 4 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
  1395.        
  1396.         A = (A + AA) & 0xFFFFFFFF
  1397.         B = (B + BB) & 0xFFFFFFFF
  1398.         C = (C + CC) & 0xFFFFFFFF
  1399.         D = (D + DD) & 0xFFFFFFFF
  1400.    
  1401.     digest = bytearray()
  1402.     for val in (A, B, C, D):
  1403.         digest.extend(val.to_bytes(4, byteorder='little'))
  1404.     final_digest = digest * 4  # Concatenate the 128-bit digest four times to get a 512-bit digest
  1405.     return final_digest.hex()[:32]  # Return only the first 32 characters (128 bits) of the digest
  1406.  
  1407.  
  1408. # Main function
  1409. if name == "main":
  1410.     message = input("Enter a message to encrypt: ")
  1411.     digest = md5(message)
  1412.     print(f"MD5 Digest (128 bits): {digest}")
  1413.  
  1414.  
  1415.  
  1416.  
  1417. 5. RSA
  1418. def gcd(a, b):
  1419.     while b:
  1420.         a, b = b, a % b
  1421.     return a
  1422.  
  1423.  
  1424. def mod_inverse(a, m):
  1425.     m0, x0, x1 = m, 0, 1
  1426.     while a > 1:
  1427.         q = a // m
  1428.         m, a = a % m, m
  1429.         x0, x1 = x1 - q * x0, x0
  1430.     return x1 + m0 if x1 < 0 else x1
  1431.  
  1432.  
  1433. def generate_keypair(p, q):
  1434.     n = p * q
  1435.    
  1436.     totient = (p - 1) * (q - 1)
  1437.    
  1438.    
  1439.  
  1440.  
  1441.     # Choose e such that 1 < e < totient(n) and gcd(e, totient(n)) = 1
  1442.     e = 2
  1443.     while gcd(e, totient) != 1:
  1444.         e += 1
  1445.  
  1446.  
  1447.     # Calculate d, the modular multiplicative inverse of e (mod totient(n))
  1448.     d = mod_inverse(e, totient)
  1449.  
  1450.  
  1451.     public_key = (e, n)
  1452.     private_key = (d, n)
  1453.     print("e:", e)
  1454.     print("where GCD(e, totient(n)=1)")
  1455.  
  1456.  
  1457.     return public_key, private_key
  1458.  
  1459.  
  1460. def encrypt(message, public_key):
  1461.     e, n = public_key
  1462.     return pow(message, e, n)
  1463.  
  1464.  
  1465. def decrypt(ciphertext, private_key):
  1466.     d, n = private_key
  1467.     return pow(ciphertext, d, n)
  1468.  
  1469.  
  1470. # Example input: prime numbers p and q
  1471. p = int(input("Enter prime number p: "))
  1472. q = int(input("Enter prime number q: "))
  1473.  
  1474.  
  1475. # Calculate n and totient
  1476. n = p * q
  1477. print("n:", n)
  1478. totient = (p - 1) * (q - 1)
  1479. print("totient(n):", totient)
  1480.  
  1481.  
  1482. # Generate key pair
  1483. public_key, private_key = generate_keypair(p, q)
  1484.  
  1485.  
  1486.  
  1487.  
  1488. # Display public key and private key
  1489. print("Public key (e, n):", public_key)
  1490. print("Private key (d, n):", private_key)
  1491.  
  1492.  
  1493. # Example message to encrypt
  1494. message = int(input("Enter message to encrypt: "))
  1495.  
  1496.  
  1497. # Encrypt the message
  1498. ciphertext = encrypt(message, public_key)
  1499. print("Encrypted message (C):", ciphertext)
  1500.  
  1501.  
  1502. # Decrypt the message
  1503. decrypted_message = decrypt(ciphertext, private_key)
  1504. print("Decrypted message (M):", decrypted_message)
  1505. # just run, 17, 11, 88
  1506.  
  1507.  
  1508. 6. SHA
  1509. # Initial hash values
  1510. a = 0x6A09E667F3BCC908
  1511. b = 0xBB67AE8584CAA73B
  1512. c = 0x3C6EF372FE94F82B
  1513. d = 0xA54FF53A5F1D36F1
  1514. e = 0x510E527FADE682D1
  1515. f = 0x9B05688C2B3E6C1F
  1516. g = 0x1F83D9ABFB41BD6B
  1517. h = 0x5BE0CD19137E2179
  1518.  
  1519.  
  1520. # Message schedule constants
  1521. K = [
  1522.     0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
  1523.     0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
  1524.     0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
  1525.     0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694,
  1526.     0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
  1527.     0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
  1528.     0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
  1529.     0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70,
  1530.     0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
  1531.     0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B,
  1532.     0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
  1533.     0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8,
  1534.     0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
  1535.     0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
  1536.     0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
  1537.     0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
  1538.     0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
  1539.     0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B,
  1540.     0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
  1541.     0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817,
  1542. ]
  1543.  
  1544.  
  1545. # Initial message schedule
  1546. W = [0] * 80
  1547.  
  1548.  
  1549. # Get user input for the message
  1550. message = input("Enter the message: ").encode('utf-8')
  1551. bit_len = len(message) * 8
  1552. message += b'\x80'
  1553. while (len(message) + 8) % 128 != 0:
  1554.     message += b'\x00'
  1555. message += bit_len.to_bytes(8, 'big')
  1556.  
  1557.  
  1558.  
  1559.  
  1560. # Process the message in blocks of 1024 bits
  1561. for i in range(0, len(message), 128):
  1562.     block = message[i:i+128]
  1563.    
  1564.     # Break the block into 64 64-bit words
  1565.     for j in range(16):
  1566.         W[j] = int.from_bytes(block[j*8:(j+1)*8], 'big')
  1567.  
  1568.  
  1569.     # Extend the 16 64-bit words into 80 64-bit words
  1570.     for j in range(16, 80):
  1571.         s0 = (W[j-15] >> 19 | W[j-15] << 63) ^ (W[j-15] >> 8 | W[j-15] << 56) ^ (W[j-15] >> 7)
  1572.         s1 = (W[j-2] >> 1 | W[j-2] << 45) ^ (W[j-2] >> 61 | W[j-2] << 3) ^ (W[j-2] >> 6)
  1573.         W[j] = (W[j-16] + s0 + W[j-7] + s1) & 0xFFFFFFFFFFFFFFFF
  1574.  
  1575.  
  1576.     # Initialize working variables
  1577.     a, b, c, d, e, f, g, h = a, b, c, d, e, f, g, h
  1578.  
  1579.  
  1580.     # Main loop
  1581.     for j in range(80):
  1582.         s0 = (a >> 28 | a << 36) ^ (a >> 34 | a << 30) ^ (a >> 39 | a << 25)
  1583.         s1 = (e >> 14 | e << 50) ^ (e >> 18 | e << 26) ^ (e >> 41 | e << 23)
  1584.         ch = (e & f) ^ (~e & g)
  1585.         maj = (a & b) ^ (a & c) ^ (b & c)
  1586.         temp1 = h + s1 + ch + K[j] + W[j]
  1587.         temp2 = s0 + maj
  1588.  
  1589.  
  1590.         h = g
  1591.         g = f
  1592.         f = e
  1593.         e = (d + temp1) & 0xFFFFFFFFFFFFFFFF
  1594.         d = c
  1595.         c = b
  1596.         b = a
  1597.         a = (temp1 + temp2) & 0xFFFFFFFFFFFFFFFF
  1598.  
  1599.  
  1600.         # Print values for each word and round
  1601.         print(f"w{j+1:02} = {W[j]:016X} Round {j+1}: a = {a:016X} b = {b:016X} c = {c:016X} d = {d:016X} e = {e:016X} f = {f:016X} g = {g:016X} h = {h:016X}\n")
  1602.     print("new block")  
  1603.  
  1604.  
  1605. # Print the final hash value
  1606. hash_result = (a, b, c, d, e, f, g, h)
  1607. final_hash = ''.join(format(word, '016X') for word in hash_result)
  1608. print(f"Final Hash/Message Digest (512 bits): {final_hash}")
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614. 7. DSS
  1615. import socket
  1616. import struct
  1617.  
  1618. def sha1(message):
  1619.     # Initialize variables
  1620.     h0 = 0x67452301
  1621.     h1 = 0xEFCDAB89
  1622.     h2 = 0x98BADCFE
  1623.     h3 = 0x10325476
  1624.     h4 = 0xC3D2E1F0
  1625.  
  1626.     # Pre-processing
  1627.     original_message = message
  1628.     message = bytearray(message, 'utf-8')
  1629.     ml = len(original_message) * 8  # Length of original message in bits
  1630.     message.append(0x80)  # Append bit '1' to the message
  1631.     while len(message) % 64 != 56:
  1632.         message.append(0x00)  # Append bits '0' to the message until message length is 64 bits less than multiple of 512
  1633.     message += struct.pack('>Q', ml)  # Append original message length in bits as 64-bit big-endian integer
  1634.  
  1635.     # Process message in 512-bit chunks
  1636.     for i in range(0, len(message), 64):
  1637.         chunk = message[i:i+64]
  1638.  
  1639.         # Break chunk into sixteen 32-bit big-endian words
  1640.         words = [0] * 80
  1641.         for j in range(16):
  1642.             words[j] = struct.unpack('>I', chunk[j*4:j*4+4])[0]
  1643.  
  1644.         # Extend the sixteen 32-bit words into eighty 32-bit words
  1645.         for j in range(16, 80):
  1646.             words[j] = left_rotate(words[j-3] ^ words[j-8] ^ words[j-14] ^ words[j-16], 1)
  1647.  
  1648.         # Initialize hash value for this chunk
  1649.         a = h0
  1650.         b = h1
  1651.         c = h2
  1652.         d = h3
  1653.         e = h4
  1654.  
  1655.         # Main loop
  1656.         for j in range(80):
  1657.             if j < 20:
  1658.                 f = (b & c) | ((~b) & d)
  1659.                 k = 0x5A827999
  1660.             elif 20 <= j < 40:
  1661.                 f = b ^ c ^ d
  1662.                 k = 0x6ED9EBA1
  1663.             elif 40 <= j < 60:
  1664.                 f = (b & c) | (b & d) | (c & d)
  1665.                 k = 0x8F1BBCDC
  1666.             else:
  1667.                 f = b ^ c ^ d
  1668.                 k = 0xCA62C1D6
  1669.  
  1670.             temp = left_rotate(a, 5) + f + e + k + words[j] & 0xFFFFFFFF
  1671.             e = d
  1672.             d = c
  1673.             c = left_rotate(b, 30)
  1674.             b = a
  1675.             a = temp
  1676.  
  1677.         # Add this chunk's hash to result so far
  1678.         h0 = (h0 + a) & 0xFFFFFFFF
  1679.         h1 = (h1 + b) & 0xFFFFFFFF
  1680.         h2 = (h2 + c) & 0xFFFFFFFF
  1681.         h3 = (h3 + d) & 0xFFFFFFFF
  1682.         h4 = (h4 + e) & 0xFFFFFFFF
  1683.  
  1684.     # Produce the final hash value
  1685.     return '%08x%08x%08x%08x%08x' % (h0, h1, h2, h3, h4)
  1686.  
  1687. def left_rotate(n, d):
  1688.     return ((n << d) | (n >> (32 - d))) & 0xFFFFFFFF
  1689.  
  1690. def calculate_values(p, q, g, k, hm, x):
  1691.     # Calculate y
  1692.     y = pow(g, x, p)
  1693.  
  1694.     # Calculate r
  1695.     r = pow(g, k, p) % q
  1696.  
  1697.     # Calculate s
  1698.     k_inverse = pow(k, -1, q)
  1699.     s = (k_inverse * (int(hm, 16) + x * r)) % q
  1700.  
  1701.     # Calculate w
  1702.     w = pow(s, -1, q)
  1703.  
  1704.     # Calculate u1 and u2
  1705.     u1 = (int(hm, 16) * w) % q
  1706.     u2 = (r * w) % q
  1707.  
  1708.     # Calculate v
  1709.     v = (pow(g, u1, p) * pow(y, u2, p)) % p % q
  1710.  
  1711.     return y, r, s, w, u1, u2, v
  1712.  
  1713. def main():
  1714.     # Client configuration
  1715.     host = '127.0.0.1'
  1716.     port = 12345
  1717.  
  1718.     # Create a socket object
  1719.     client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1720.  
  1721.     # Connect to the server
  1722.     client_socket.connect((host, port))
  1723.  
  1724.     # Gather inputs from the user
  1725.     p = int(input("Enter the value of p: "))
  1726.     q = int(input("Enter the value of q: "))
  1727.     g = int(input("Enter the value of g: "))
  1728.     k = int(input("Enter the value of k (User's per-Message Secret Number): "))
  1729.     message = input("Enter the message: ")
  1730.  
  1731.     # Calculate hash value (SHA-1)
  1732.     hm = sha1(message)
  1733.     print(f"The hash value of {message} (SHA-1): {hm}")
  1734.  
  1735.     x = int(input("Enter the value of x (User's Private Key): "))
  1736.  
  1737.     # Calculate values
  1738.     y, r, s, w, u1, u2, v = calculate_values(p, q, g, k, hm, x)
  1739.  
  1740.     # Print signature
  1741.     print(f"Signature: [r:{r},s:{s}]")
  1742.  
  1743.     # Ask user if they want to send the correct message
  1744.     send_correct = input("Do you want to send the correct message? (y/n): ")
  1745.  
  1746.     if send_correct.lower() == 'y':
  1747.         correct_message = input("Enter the correct message: ")
  1748.         correct_hm = sha1(correct_message)
  1749.         print(f"The hash value of {correct_message} (SHA-1): {correct_hm}")
  1750.  
  1751.         # Calculate u1 for correct message
  1752.         correct_u1 = (int(correct_hm, 16) * w) % q
  1753.  
  1754.         # Calculate v for correct message
  1755.         correct_v = (pow(g, correct_u1, p) * pow(y, u2, p)) % p % q
  1756.  
  1757.         # Send correct message values to the server
  1758.         data = f"{p},{q},{g},{k},{correct_hm},{x},{y},{r},{s},{w},{correct_u1},{u2},{correct_v}"
  1759.     else:
  1760.         # Send original message values to the server
  1761.         data = f"{p},{q},{g},{k},{hm},{x},{y},{r},{s},{w},{u1},{u2},{v}"
  1762.     print("Data Sent successfully to the Client")    
  1763.  
  1764.     # Send inputs and calculated values to the server
  1765.     client_socket.send(data.encode())
  1766.  
  1767.     # Close the connection
  1768.     client_socket.close()
  1769.  
  1770. if _name_ == "_main_":
  1771.     main()
  1772. import socket
  1773.  
  1774. def main():
  1775.     # Server configuration
  1776.     host = '127.0.0.1'
  1777.     port = 12345
  1778.  
  1779.     # Create a socket object
  1780.     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  1781.  
  1782.     # Bind the socket to the address
  1783.     server_socket.bind((host, port))
  1784.  
  1785.     # Listen for incoming connections
  1786.     server_socket.listen(1)
  1787.     print("Server is listening on port", port)
  1788.  
  1789.     # Accept a connection
  1790.     client_socket, addr = server_socket.accept()
  1791.     print("Connection from", addr)
  1792.  
  1793.     # Receive data from the client
  1794.     data = client_socket.recv(1024).decode()
  1795.     print("Received data from server:", data)
  1796.  
  1797.     # Split received data into individual values
  1798.     values = data.split(',')
  1799.  
  1800.     # Display received values
  1801.     print("Received values from server:")
  1802.     print("p:", values[0])
  1803.     print("q:", values[1])
  1804.     print("g:", values[2])
  1805.     print("k:", values[3])
  1806.     print("hm:", values[4])
  1807.     print("x:", values[5])
  1808.     print("y:", values[6])
  1809.     print("s:", values[8])
  1810.     print("\n")
  1811.     print("Verification:")
  1812.     print("w:", values[9])
  1813.     print("u1:", values[10])
  1814.     print("u2:", values[11])
  1815.     print("v:", values[12])
  1816.     print("\n")
  1817.     print("r' (from sender):", values[7])
  1818.     print("v  (calculated):", values[12])
  1819.  
  1820.     # Perform DSS verification
  1821.     r = int(values[8])
  1822.     v = int(values[11])
  1823.     if v == r:
  1824.         print("Digital Signature Standard (DSS) verification Successful")
  1825.     else:
  1826.         print("Digital Signature Standard (DSS) Verification Failed")
  1827.  
  1828.     # Close the connection
  1829.     client_socket.close()
  1830.  
  1831. if _name_ == "_main_":
  1832.     main()

Editor

You can edit this paste and save as new:


File Description
  • aaaaaaaaaaa
  • Paste Code
  • 23 Apr-2024
  • 47.03 Kb
You can Share it: