[text] ssssssssssssssssssss

Viewer

copydownloadembedprintName: ssssssssssssssssssss
  1. import socket
  2. import struct
  3.  
  4. def sha1(message):
  5.     # Initialize variables
  6.     h0 = 0x67452301
  7.     h1 = 0xEFCDAB89
  8.     h2 = 0x98BADCFE
  9.     h3 = 0x10325476
  10.     h4 = 0xC3D2E1F0
  11.  
  12.     # Pre-processing
  13.     original_message = message
  14.     message = bytearray(message, 'utf-8')
  15.     ml = len(original_message) * 8  # Length of original message in bits
  16.     message.append(0x80)  # Append bit '1' to the message
  17.     while len(message) % 64 != 56:
  18.         message.append(0x00)  # Append bits '0' to the message until message length is 64 bits less than multiple of 512
  19.     message += struct.pack('>Q', ml)  # Append original message length in bits as 64-bit big-endian integer
  20.  
  21.     # Process message in 512-bit chunks
  22.     for i in range(0, len(message), 64):
  23.         chunk = message[i:i+64]
  24.  
  25.         # Break chunk into sixteen 32-bit big-endian words
  26.         words = [0] * 80
  27.         for j in range(16):
  28.             words[j] = struct.unpack('>I', chunk[j*4:j*4+4])[0]
  29.  
  30.         # Extend the sixteen 32-bit words into eighty 32-bit words
  31.         for j in range(16, 80):
  32.             words[j] = left_rotate(words[j-3] ^ words[j-8] ^ words[j-14] ^ words[j-16], 1)
  33.  
  34.         # Initialize hash value for this chunk
  35.         a = h0
  36.         b = h1
  37.         c = h2
  38.         d = h3
  39.         e = h4
  40.  
  41.         # Main loop
  42.         for j in range(80):
  43.             if j < 20:
  44.                 f = (b & c) | ((~b) & d)
  45.                 k = 0x5A827999
  46.             elif 20 <= j < 40:
  47.                 f = b ^ c ^ d
  48.                 k = 0x6ED9EBA1
  49.             elif 40 <= j < 60:
  50.                 f = (b & c) | (b & d) | (c & d)
  51.                 k = 0x8F1BBCDC
  52.             else:
  53.                 f = b ^ c ^ d
  54.                 k = 0xCA62C1D6
  55.  
  56.             temp = left_rotate(a, 5) + f + e + k + words[j] & 0xFFFFFFFF
  57.             e = d
  58.             d = c
  59.             c = left_rotate(b, 30)
  60.             b = a
  61.             a = temp
  62.  
  63.         # Add this chunk's hash to result so far
  64.         h0 = (h0 + a) & 0xFFFFFFFF
  65.         h1 = (h1 + b) & 0xFFFFFFFF
  66.         h2 = (h2 + c) & 0xFFFFFFFF
  67.         h3 = (h3 + d) & 0xFFFFFFFF
  68.         h4 = (h4 + e) & 0xFFFFFFFF
  69.  
  70.     # Produce the final hash value
  71.     return '%08x%08x%08x%08x%08x' % (h0, h1, h2, h3, h4)
  72.  
  73. def left_rotate(n, d):
  74.     return ((n << d) | (n >> (32 - d))) & 0xFFFFFFFF
  75.  
  76. def calculate_values(p, q, g, k, hm, x):
  77.     # Calculate y
  78.     y = pow(g, x, p)
  79.  
  80.     # Calculate r
  81.     r = pow(g, k, p) % q
  82.  
  83.     # Calculate s
  84.     k_inverse = pow(k, -1, q)
  85.     s = (k_inverse * (int(hm, 16) + x * r)) % q
  86.  
  87.     # Calculate w
  88.     w = pow(s, -1, q)
  89.  
  90.     # Calculate u1 and u2
  91.     u1 = (int(hm, 16) * w) % q
  92.     u2 = (r * w) % q
  93.  
  94.     # Calculate v
  95.     v = (pow(g, u1, p) * pow(y, u2, p)) % p % q
  96.  
  97.     return y, r, s, w, u1, u2, v
  98.  
  99. def main():
  100.     # Client configuration
  101.     host = '127.0.0.1'
  102.     port = 12345
  103.  
  104.     # Create a socket object
  105.     client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  106.  
  107.     # Connect to the server
  108.     client_socket.connect((host, port))
  109.  
  110.     # Gather inputs from the user
  111.     p = int(input("Enter the value of p: "))
  112.     q = int(input("Enter the value of q: "))
  113.     g = int(input("Enter the value of g: "))
  114.     k = int(input("Enter the value of k (User's per-Message Secret Number): "))
  115.     message = input("Enter the message: ")
  116.  
  117.     # Calculate hash value (SHA-1)
  118.     hm = sha1(message)
  119.     print(f"The hash value of {message} (SHA-1): {hm}")
  120.  
  121.     x = int(input("Enter the value of x (User's Private Key): "))
  122.  
  123.     # Calculate values
  124.     y, r, s, w, u1, u2, v = calculate_values(p, q, g, k, hm, x)
  125.  
  126.     # Print signature
  127.     print(f"Signature: [r:{r},s:{s}]")
  128.  
  129.     # Ask user if they want to send the correct message
  130.     send_correct = input("Do you want to send the correct message? (y/n): ")
  131.  
  132.     if send_correct.lower() == 'y':
  133.         correct_message = input("Enter the correct message: ")
  134.         correct_hm = sha1(correct_message)
  135.         print(f"The hash value of {correct_message} (SHA-1): {correct_hm}")
  136.  
  137.         # Calculate u1 for correct message
  138.         correct_u1 = (int(correct_hm, 16) * w) % q
  139.  
  140.         # Calculate v for correct message
  141.         correct_v = (pow(g, correct_u1, p) * pow(y, u2, p)) % p % q
  142.  
  143.         # Send correct message values to the server
  144.         data = f"{p},{q},{g},{k},{correct_hm},{x},{y},{r},{s},{w},{correct_u1},{u2},{correct_v}"
  145.     else:
  146.         # Send original message values to the server
  147.         data = f"{p},{q},{g},{k},{hm},{x},{y},{r},{s},{w},{u1},{u2},{v}"
  148.     print("Data Sent successfully to the Client")    
  149.  
  150.     # Send inputs and calculated values to the server
  151.     client_socket.send(data.encode())
  152.  
  153.     # Close the connection
  154.     client_socket.close()
  155.  
  156. if __name__ == "__main__":
  157.     main()

Editor

You can edit this paste and save as new:


File Description
  • ssssssssssssssssssss
  • Paste Code
  • 16 Apr-2024
  • 4.72 Kb
You can Share it: