[text] lelo

Viewer

  1. CLIENT
  2.  
  3. import socket
  4.  
  5. def main():
  6.     # Server configuration
  7.     host = '127.0.0.1'
  8.     port = 12345
  9.  
  10.     # Create a socket object
  11.     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  12.  
  13.     # Bind the socket to the address
  14.     server_socket.bind((host, port))
  15.  
  16.     # Listen for incoming connections
  17.     server_socket.listen(1)
  18.     print("Server is listening on port", port)
  19.  
  20.     # Accept a connection
  21.     client_socket, addr = server_socket.accept()
  22.     print("Connection from", addr)
  23.  
  24.     # Receive data from the client
  25.     data = client_socket.recv(1024).decode()
  26.     print("Received data from server:", data)
  27.  
  28.     # Split received data into individual values
  29.     values = data.split(',')
  30.  
  31.     # Display received values
  32.     print("Received values from server:")
  33.     print("p:", values[0])
  34.     print("q:", values[1])
  35.     print("g:", values[2])
  36.     print("k:", values[3])
  37.     print("hm:", values[4])
  38.     print("x:", values[5])
  39.     print("y:", values[6])
  40.     print("s:", values[8])
  41.     print("\n")
  42.     print("Verification:")
  43.     print("w:", values[9])
  44.     print("u1:", values[10])
  45.     print("u2:", values[11])
  46.     print("v:", values[12])
  47.     print("\n")
  48.     print("r' (from sender):", values[7])
  49.     print("v  (calculated):", values[12])
  50.  
  51.     # Perform DSS verification
  52.     r = int(values[7])
  53.     v = int(values[12])
  54.     if v == r:
  55.         print("Digital Signature Standard (DSS) verification Successful")
  56.     else:
  57.         print("Digital Signature Standard (DSS) Verification Failed")
  58.  
  59.     # Close the connection
  60.     client_socket.close()
  61.  
  62. if __name__ == "__main__":
  63.     main()
  64.  
  65.  
  66. Server
  67.  
  68.  
  69. import socket
  70. import struct
  71.  
  72. def sha1(message):
  73.     # Initialize variables
  74.     h0 = 0x67452301
  75.     h1 = 0xEFCDAB89
  76.     h2 = 0x98BADCFE
  77.     h3 = 0x10325476
  78.     h4 = 0xC3D2E1F0
  79.  
  80.     # Pre-processing
  81.     original_message = message
  82.     message = bytearray(message, 'utf-8')
  83.     ml = len(original_message) * 8  # Length of original message in bits
  84.     message.append(0x80)  # Append bit '1' to the message
  85.     while len(message) % 64 != 56:
  86.         message.append(0x00)  # Append bits '0' to the message until message length is 64 bits less than multiple of 512
  87.     message += struct.pack('>Q', ml)  # Append original message length in bits as 64-bit big-endian integer
  88.  
  89.     # Process message in 512-bit chunks
  90.     for i in range(0, len(message), 64):
  91.         chunk = message[i:i+64]
  92.  
  93.         # Break chunk into sixteen 32-bit big-endian words
  94.         words = [0] * 80
  95.         for j in range(16):
  96.             words[j] = struct.unpack('>I', chunk[j*4:j*4+4])[0]
  97.  
  98.         # Extend the sixteen 32-bit words into eighty 32-bit words
  99.         for j in range(16, 80):
  100.             words[j] = left_rotate(words[j-3] ^ words[j-8] ^ words[j-14] ^ words[j-16], 1)
  101.  
  102.         # Initialize hash value for this chunk
  103.         a = h0
  104.         b = h1
  105.         c = h2
  106.         d = h3
  107.         e = h4
  108.  
  109.         # Main loop
  110.         for j in range(80):
  111.             if j < 20:
  112.                 f = (b & c) | ((~b) & d)
  113.                 k = 0x5A827999
  114.             elif 20 <= j < 40:
  115.                 f = b ^ c ^ d
  116.                 k = 0x6ED9EBA1
  117.             elif 40 <= j < 60:
  118.                 f = (b & c) | (b & d) | (c & d)
  119.                 k = 0x8F1BBCDC
  120.             else:
  121.                 f = b ^ c ^ d
  122.                 k = 0xCA62C1D6
  123.  
  124.             temp = left_rotate(a, 5) + f + e + k + words[j] & 0xFFFFFFFF
  125.             e = d
  126.             d = c
  127.             c = left_rotate(b, 30)
  128.             b = a
  129.             a = temp
  130.  
  131.         # Add this chunk's hash to result so far
  132.         h0 = (h0 + a) & 0xFFFFFFFF
  133.         h1 = (h1 + b) & 0xFFFFFFFF
  134.         h2 = (h2 + c) & 0xFFFFFFFF
  135.         h3 = (h3 + d) & 0xFFFFFFFF
  136.         h4 = (h4 + e) & 0xFFFFFFFF
  137.  
  138.     # Produce the final hash value
  139.     return '%08x%08x%08x%08x%08x' % (h0, h1, h2, h3, h4)
  140.  
  141. def left_rotate(n, d):
  142.     return ((n << d) | (n >> (32 - d))) & 0xFFFFFFFF
  143.  
  144. def calculate_values(p, q, g, k, hm, x):
  145.     # Calculate y
  146.     y = pow(g, x, p)
  147.  
  148.     # Calculate r
  149.     r = pow(g, k, p) % q
  150.  
  151.     # Calculate s
  152.     k_inverse = pow(k, -1, q)
  153.     s = (k_inverse * (int(hm, 16) + x * r)) % q
  154.  
  155.     # Calculate w
  156.     w = pow(s, -1, q)
  157.  
  158.     # Calculate u1 and u2
  159.     u1 = (int(hm, 16) * w) % q
  160.     u2 = (r * w) % q
  161.  
  162.     # Calculate v
  163.     v = (pow(g, u1, p) * pow(y, u2, p)) % p % q
  164.  
  165.     return y, r, s, w, u1, u2, v
  166.  
  167. def main():
  168.     # Client configuration
  169.     host = '127.0.0.1'
  170.     port = 12345
  171.  
  172.     # Create a socket object
  173.     client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  174.  
  175.     # Connect to the server
  176.     client_socket.connect((host, port))
  177.  
  178.     # Gather inputs from the user
  179.     p = int(input("Enter the value of p: "))
  180.     q = int(input("Enter the value of q: "))
  181.     g = int(input("Enter the value of g: "))
  182.     k = int(input("Enter the value of k (User's per-Message Secret Number): "))
  183.     message = input("Enter the message: ")
  184.  
  185.     # Calculate hash value (SHA-1)
  186.     hm = sha1(message)
  187.     print(f"The hash value of {message} (SHA-1): {hm}")
  188.  
  189.     x = int(input("Enter the value of x (User's Private Key): "))
  190.  
  191.     # Calculate values
  192.     y, r, s, w, u1, u2, v = calculate_values(p, q, g, k, hm, x)
  193.  
  194.     # Print signature
  195.     print(f"Signature: [r:{r},s:{s}]")
  196.  
  197.     # Ask user if they want to send the correct message
  198.     send_correct = input("Do you want to send the correct message? (y/n): ")
  199.  
  200.     if send_correct.lower() == 'y':
  201.         correct_message = input("Enter the correct message: ")
  202.         correct_hm = sha1(correct_message)
  203.         print(f"The hash value of {correct_message} (SHA-1): {correct_hm}")
  204.  
  205.         # Calculate u1 for correct message
  206.         correct_u1 = (int(correct_hm, 16) * w) % q
  207.  
  208.         # Calculate v for correct message
  209.         correct_v = (pow(g, correct_u1, p) * pow(y, u2, p)) % p % q
  210.  
  211.         # Send correct message values to the server
  212.         data = f"{p},{q},{g},{k},{correct_hm},{x},{y},{r},{s},{w},{correct_u1},{u2},{correct_v}"
  213.     else:
  214.         # Send original message values to the server
  215.         data = f"{p},{q},{g},{k},{hm},{x},{y},{r},{s},{w},{u1},{u2},{v}"
  216.     print("Data Sent successfully to the Client")    
  217.  
  218.     # Send inputs and calculated values to the server
  219.     client_socket.send(data.encode())
  220.  
  221.     # Close the connection
  222.     client_socket.close()
  223.  
  224. if __name__ == "__main__":
  225.     main()
  226.  

Editor

You can edit this paste and save as new:


File Description
  • lelo
  • Paste Code
  • 16 Apr-2024
  • 6.35 Kb
You can Share it: