[text] d

Viewer

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

Editor

You can edit this paste and save as new:


File Description
  • d
  • Paste Code
  • 23 Apr-2024
  • 6.33 Kb
You can Share it: