Facebook
From admin, 2 Weeks ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 173
  1. # Function to perform the Diffie-Hellman key exchange
  2. def diffie_hellman(P, G, a, b):
  3.     # Calculate public keys
  4.     A = (G ** a) % P
  5.     B = (G ** b) % P
  6.  
  7.     # Calculate shared secret
  8.     S1 = (B ** a) % P
  9.     S2 = (A ** b) % P
  10.  
  11.     # Ensure shared secrets match
  12.     assert S1 == S2, "Shared secrets do not match!"
  13.  
  14.     return S1
  15.  
  16. # Parameters
  17. P = 33
  18. G = 8
  19. a = 3  # User 1's private key
  20. b = 2  # User 2's private key
  21.  
  22. # Perform Diffie-Hellman key exchange
  23. shared_secret = diffie_hellman(P, G, a, b)
  24.  
  25. # Function to decode hash using shared secret
  26. def decode_hash(hash_value, shared_secret):
  27.     decoded_hash = int(hash_value, 16) ^ shared_secret  # Convert hash to integer and perform XOR
  28.     return decoded_hash
  29.  
  30. # Provided hash value
  31. hash_value = "cd7760167c59fa13ddc180e7ce78c5e7e23eb54dc2e38bd2a0022058b16a5de98b2d579e680a4caa3b8b507dba9c1944"
  32.  
  33. # Decode hash using shared secret
  34. decoded_hash = decode_hash(hash_value, shared_secret)
  35.  
  36. print("Shared Secret:", shared_secret)
  37. print("Decoded Hash:", hex(decoded_hash)[2:])  # Convert decoded hash back to hexadecimal and remove the '0x' prefix
  38.