- import socket
- import struct
- def sha1(message):
- # Initialize variables
- h0 = 0x67452301
- h1 = 0xEFCDAB89
- h2 = 0x98BADCFE
- h3 = 0x10325476
- h4 = 0xC3D2E1F0
- # Pre-processing
- original_message = message
- message = bytearray(message, 'utf-8')
- ml = len(original_message) * 8 # Length of original message in bits
- message.append(0x80) # Append bit '1' to the message
- while len(message) % 64 != 56:
- message.append(0x00) # Append bits '0' to the message until message length is 64 bits less than multiple of 512
- message += struct.pack('>Q', ml) # Append original message length in bits as 64-bit big-endian integer
- # Process message in 512-bit chunks
- for i in range(0, len(message), 64):
- chunk = message[i:i+64]
- # Break chunk into sixteen 32-bit big-endian words
- words = [0] * 80
- for j in range(16):
- words[j] = struct.unpack('>I', chunk[j*4:j*4+4])[0]
- # Extend the sixteen 32-bit words into eighty 32-bit words
- for j in range(16, 80):
- words[j] = left_rotate(words[j-3] ^ words[j-8] ^ words[j-14] ^ words[j-16], 1)
- # Initialize hash value for this chunk
- a = h0
- b = h1
- c = h2
- d = h3
- e = h4
- # Main loop
- for j in range(80):
- if j < 20:
- f = (b & c) | ((~b) & d)
- k = 0x5A827999
- elif 20 <= j < 40:
- f = b ^ c ^ d
- k = 0x6ED9EBA1
- elif 40 <= j < 60:
- f = (b & c) | (b & d) | (c & d)
- k = 0x8F1BBCDC
- else:
- f = b ^ c ^ d
- k = 0xCA62C1D6
- temp = left_rotate(a, 5) + f + e + k + words[j] & 0xFFFFFFFF
- e = d
- d = c
- c = left_rotate(b, 30)
- b = a
- a = temp
- # Add this chunk's hash to result so far
- h0 = (h0 + a) & 0xFFFFFFFF
- h1 = (h1 + b) & 0xFFFFFFFF
- h2 = (h2 + c) & 0xFFFFFFFF
- h3 = (h3 + d) & 0xFFFFFFFF
- h4 = (h4 + e) & 0xFFFFFFFF
- # Produce the final hash value
- return '%08x%08x%08x%08x%08x' % (h0, h1, h2, h3, h4)
- def left_rotate(n, d):
- return ((n << d) | (n >> (32 - d))) & 0xFFFFFFFF
- def calculate_values(p, q, g, k, hm, x):
- # Calculate y
- y = pow(g, x, p)
- # Calculate r
- r = pow(g, k, p) % q
- # Calculate s
- k_inverse = pow(k, -1, q)
- s = (k_inverse * (int(hm, 16) + x * r)) % q
- # Calculate w
- w = pow(s, -1, q)
- # Calculate u1 and u2
- u1 = (int(hm, 16) * w) % q
- u2 = (r * w) % q
- # Calculate v
- v = (pow(g, u1, p) * pow(y, u2, p)) % p % q
- return y, r, s, w, u1, u2, v
- def main():
- # Client configuration
- host = '127.0.0.1'
- port = 12345
- # Create a socket object
- client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # Connect to the server
- client_socket.connect((host, port))
- # Gather inputs from the user
- p = int(input("Enter the value of p: "))
- q = int(input("Enter the value of q: "))
- g = int(input("Enter the value of g: "))
- k = int(input("Enter the value of k (User's per-Message Secret Number): "))
- message = input("Enter the message: ")
- # Calculate hash value (SHA-1)
- hm = sha1(message)
- print(f"The hash value of {message} (SHA-1): {hm}")
- x = int(input("Enter the value of x (User's Private Key): "))
- # Calculate values
- y, r, s, w, u1, u2, v = calculate_values(p, q, g, k, hm, x)
- # Print signature
- print(f"Signature: [r:{r},s:{s}]")
- # Ask user if they want to send the correct message
- send_correct = input("Do you want to send the correct message? (y/n): ")
- if send_correct.lower() == 'y':
- correct_message = input("Enter the correct message: ")
- correct_hm = sha1(correct_message)
- print(f"The hash value of {correct_message} (SHA-1): {correct_hm}")
- # Calculate u1 for correct message
- correct_u1 = (int(correct_hm, 16) * w) % q
- # Calculate v for correct message
- correct_v = (pow(g, correct_u1, p) * pow(y, u2, p)) % p % q
- # Send correct message values to the server
- data = f"{p},{q},{g},{k},{correct_hm},{x},{y},{r},{s},{w},{correct_u1},{u2},{correct_v}"
- else:
- # Send original message values to the server
- data = f"{p},{q},{g},{k},{hm},{x},{y},{r},{s},{w},{u1},{u2},{v}"
- print("Data Sent successfully to the Client")
- # Send inputs and calculated values to the server
- client_socket.send(data.encode())
- # Close the connection
- client_socket.close()
- if __name__ == "__main__":
- main()
[text] ssssssssssssssssssss
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: