[text] DIFmdRs

Viewer

  1. DIFFY
  2. ALICE
  3. import socket
  4. import math
  5.  
  6.  
  7. def mod_exp(base, exponent, modulus):
  8.     return pow(base, exponent, modulus)
  9.  
  10.  
  11. def compute_Y(alpha, X, q):
  12.     return mod_exp(alpha, X, q)
  13.  
  14.  
  15. def compute_shared_key(Y, X, q):
  16.     return mod_exp(Y, X, q)
  17.  
  18.  
  19. def alice():
  20.     alpha = 10  # Specified value
  21.     q = 16     # Specified value
  22.  
  23.  
  24.     X = int(input("Enter XA for Alice: "))
  25.  
  26.  
  27.     YA = compute_Y(alpha, X, q)
  28.     print(f"YA={YA}")
  29.  
  30.  
  31.     # Communicate with Dart
  32.     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_dart:
  33.         s_dart.connect(('localhost', 5001))
  34.         s_dart.send(','.join([str(x) for x in [alpha, X, q, YA]]).encode('utf-8'))
  35.  
  36.  
  37.         # Receive YD2 from Dart
  38.         YD2_dart = int(s_dart.recv(1024).decode('utf-8'))
  39.         print(f"Received YD2 from Dart: {YD2_dart}")
  40.  
  41.  
  42.         # Compute and display K2
  43.         K2 = compute_shared_key(YD2_dart, X, q)
  44.         print(f"K2={K2}")
  45.  
  46.  
  47. if name == "main":
  48.     alice()
  49. BOB
  50. import socket
  51. import math
  52.  
  53.  
  54. def mod_exp(base, exponent, modulus):
  55.     return pow(base, exponent, modulus)
  56.  
  57.  
  58. def compute_Y(alpha, X, q):
  59.     return mod_exp(alpha, X, q)
  60.  
  61.  
  62. def compute_shared_key(Y, X, q):
  63.     return mod_exp(Y, X, q)
  64.  
  65.  
  66. def bob():
  67.     alpha = 10  # Specified value
  68.     q = 16     # Specified value
  69.  
  70.  
  71.     XB = int(input("Enter XB for Bob: "))
  72.  
  73.  
  74.     YB = compute_Y(alpha, XB, q)
  75.     print(f"YB={YB}")
  76.  
  77.  
  78.     # Communicate with Dart
  79.     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_dart:
  80.         s_dart.bind(('localhost', 5002))
  81.         s_dart.connect(1)
  82.         print("Bob terminal is listening for Dart on port 5002")
  83.  
  84.  
  85.         conn_dart, addr_dart = s_dart.accept()
  86.         with conn_dart:
  87.             print(f"Connection established with Dart: {addr_dart}")
  88.  
  89.  
  90.             # Receive YD1 from Dart
  91.             YD1_dart = int(conn_dart.recv(1024).decode('utf-8'))
  92.             print(f"Received YD1 from Dart: {YD1_dart}")
  93.  
  94.  
  95.             # Additional communication with Dart can be added here if needed
  96.  
  97.  
  98.             # Compute and display K1
  99.             K1 = compute_shared_key(YD1_dart, XB, q)
  100.             print(f"K1={K1}")
  101.  
  102.  
  103. if name == "main":
  104.     bob()
  105. DARTH
  106. import socket
  107. import math
  108.  
  109.  
  110. def mod_exp(base, exponent, modulus):
  111.     return pow(base, exponent, modulus)
  112.  
  113.  
  114. def compute_Y(alpha, X, q):
  115.     return mod_exp(alpha, X, q)
  116.    
  117. def compute_k1(YB, XD1, q):
  118.     return mod_exp(YB, 5, q)  
  119.    
  120. def compute_k2(YA, XD2, q):
  121.     return mod_exp(YA, XD2, q)
  122.    
  123. def compute_shared_key(Y, X, q):
  124.     return mod_exp(Y, X, q)
  125.  
  126.  
  127. def dart():
  128.     alpha = 15  # Specified value
  129.     q = 12     # Specified value
  130.  
  131.  
  132.     # Communicate with Alice
  133.     with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_alice:
  134.         s_alice.bind(('localhost', 5001))
  135.         s_alice.listen(1)
  136.         print("Dart terminal is listening for Alice on port 5001")
  137.  
  138.  
  139.         conn_alice, addr_alice = s_alice.accept()
  140.         with conn_alice:
  141.             print(f"Connection established with Alice: {addr_alice}")
  142.             data_alice = conn_alice.recv(1024).decode('utf-8')
  143.             alpha, X, q, YA = map(int, data_alice.split(','))
  144.  
  145.  
  146.             print(f"Received data from Alice: alpha={alpha}, X={X}, q={q}, YA={YA}")
  147.  
  148.  
  149.             XD1 = int(input("Enter XD1: "))
  150.             XD2 = int(input("Enter XD2: "))
  151.  
  152.  
  153.             YD1 = compute_Y(alpha, XD1, q)
  154.             YD2 = compute_Y(alpha, XD2, q)
  155.  
  156.  
  157.             print(f"YD1={YD1}, YD2={YD2}")
  158.            
  159.             K1 = compute_k1(YD1, XD1, q)
  160.             K2 = compute_k2(YD2, X, q)
  161.  
  162.  
  163.             print(f"K1={K1}, K2={K2}")
  164.  
  165.  
  166.             # Send YD2 to Alice
  167.             conn_alice.send(str(YD2).encode('utf-8'))
  168.  
  169.  
  170.             # Send YD1 to Bob
  171.             with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_bob:
  172.                 s_bob.connect(('localhost', 5002))
  173.                 s_bob.send(str(YD1).encode('utf-8'))
  174.  
  175.  
  176.                 # Additional communication with Bob can be added here if needed
  177.  
  178.  
  179. if name == "main":
  180.     dart()
  181.  
  182.  
  183.  
  184. MD5
  185. import math
  186.  
  187.  
  188. # Define MD5 specific functions
  189. def F(X, Y, Z):
  190.     return (X & Y) | (~X & Z)
  191.  
  192.  
  193. def G(X, Y, Z):
  194.     return (X & Z) | (Y & ~Z)
  195.  
  196.  
  197. def H(X, Y, Z):
  198.     return X ^ Y ^ Z
  199.  
  200.  
  201. def I(X, Y, Z):
  202.     return Y ^ (X | ~Z)
  203.  
  204.  
  205. # Define shift and constant values
  206. s = [
  207.     7, 12, 17, 22,
  208.     5, 9, 14, 20,
  209.     4, 11, 16, 23,
  210.     6, 10, 15, 21
  211. ]
  212.  
  213.  
  214. K = [int(abs(math.sin(i + 1)) * 2 ** 32) & 0xFFFFFFFF for i in range(64)]
  215.  
  216.  
  217. # Initialize variables
  218. A = 0x01234567
  219. B = 0x89ABCDEF
  220. C = 0xFEDCBA98
  221. D = 0x76543210
  222.  
  223.  
  224. def md5(message):
  225.     global A, B, C, D
  226.     message = bytearray(message, 'utf-8')
  227.     for i in range(0, len(message), 64):
  228.         X = [0] * 16
  229.         for j in range(16):
  230.             X[j] = int.from_bytes(message[i + j*4:i + j*4 + 4], byteorder='little')
  231.        
  232.         AA, BB, CC, DD = A, B, C, D
  233.        
  234.         # Round 1
  235.         for j in range(16):
  236.             A = (B + ((A + F(B, C, D) + X[j] + K[j]) & 0xFFFFFFFF) << s[j % 4] | (A + F(B, C, D) + X[j] + K[j]) >> (32 - s[j % 4])) & 0xFFFFFFFF
  237.             A, B, C, D = D, A, B, C
  238.         print(f"Round 1 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
  239.        
  240.         # Round 2
  241.         for j in range(16):
  242.             A = (B + ((A + G(B, C, D) + X[(1 + 5*j) % 16] + K[16+j]) & 0xFFFFFFFF) << s[j % 4 + 4] | (A + G(B, C, D) + X[(1 + 5*j) % 16] + K[16+j]) >> (32 - s[j % 4 + 4])) & 0xFFFFFFFF
  243.             A, B, C, D = D, A, B, C
  244.         print(f"Round 2 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
  245.        
  246.         # Round 3
  247.         for j in range(16):
  248.             A = (B + ((A + H(B, C, D) + X[(5 + 3*j) % 16] + K[32+j]) & 0xFFFFFFFF) << s[j % 4 + 8] | (A + H(B, C, D) + X[(5 + 3*j) % 16] + K[32+j]) >> (32 - s[j % 4 + 8])) & 0xFFFFFFFF
  249.             A, B, C, D = D, A, B, C
  250.         print(f"Round 3 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
  251.        
  252.         # Round 4
  253.         for j in range(16):
  254.             A = (B + ((A + I(B, C, D) + X[(7*j) % 16] + K[48+j]) & 0xFFFFFFFF) << s[j % 4 + 12] | (A + I(B, C, D) + X[(7*j) % 16] + K[48+j]) >> (32 - s[j % 4 + 12])) & 0xFFFFFFFF
  255.             A, B, C, D = D, A, B, C
  256.         print(f"Round 4 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
  257.        
  258.         A = (A + AA) & 0xFFFFFFFF
  259.         B = (B + BB) & 0xFFFFFFFF
  260.         C = (C + CC) & 0xFFFFFFFF
  261.         D = (D + DD) & 0xFFFFFFFF
  262.    
  263.     digest = bytearray()
  264.     for val in (A, B, C, D):
  265.         digest.extend(val.to_bytes(4, byteorder='little'))
  266.     final_digest = digest * 4  # Concatenate the 128-bit digest four times to get a 512-bit digest
  267.     return final_digest.hex()[:32]  # Return only the first 32 characters (128 bits) of the digest
  268.  
  269.  
  270. # Main function
  271. if name == "main":
  272.     message = input("Enter a message to encrypt: ")
  273.     digest = md5(message)
  274.     print(f"MD5 Digest (128 bits): {digest}")
  275.  
  276.  
  277.  
  278.  
  279. RSA
  280. def gcd(a, b):
  281.     while b:
  282.         a, b = b, a % b
  283.     return a
  284.  
  285.  
  286. def mod_inverse(a, m):
  287.     m0, x0, x1 = m, 0, 1
  288.     while a > 1:
  289.         q = a // m
  290.         m, a = a % m, m
  291.         x0, x1 = x1 - q * x0, x0
  292.     return x1 + m0 if x1 < 0 else x1
  293.  
  294.  
  295. def generate_keypair(p, q):
  296.     n = p * q
  297.    
  298.     totient = (p - 1) * (q - 1)
  299.    
  300.    
  301.  
  302.  
  303.     # Choose e such that 1 < e < totient(n) and gcd(e, totient(n)) = 1
  304.     e = 2
  305.     while gcd(e, totient) != 1:
  306.         e += 1
  307.  
  308.  
  309.     # Calculate d, the modular multiplicative inverse of e (mod totient(n))
  310.     d = mod_inverse(e, totient)
  311.  
  312.  
  313.     public_key = (e, n)
  314.     private_key = (d, n)
  315.     print("e:", e)
  316.     print("where GCD(e, totient(n)=1)")
  317.  
  318.  
  319.     return public_key, private_key
  320.  
  321.  
  322. def encrypt(message, public_key):
  323.     e, n = public_key
  324.     return pow(message, e, n)
  325.  
  326.  
  327. def decrypt(ciphertext, private_key):
  328.     d, n = private_key
  329.     return pow(ciphertext, d, n)
  330.  
  331.  
  332. # Example input: prime numbers p and q
  333. p = int(input("Enter prime number p: "))
  334. q = int(input("Enter prime number q: "))
  335.  
  336.  
  337. # Calculate n and totient
  338. n = p * q
  339. print("n:", n)
  340. totient = (p - 1) * (q - 1)
  341. print("totient(n):", totient)
  342.  
  343.  
  344. # Generate key pair
  345. public_key, private_key = generate_keypair(p, q)
  346.  
  347.  
  348.  
  349.  
  350. # Display public key and private key
  351. print("Public key (e, n):", public_key)
  352. print("Private key (d, n):", private_key)
  353.  
  354.  
  355. # Example message to encrypt
  356. message = int(input("Enter message to encrypt: "))
  357.  
  358.  
  359. # Encrypt the message
  360. ciphertext = encrypt(message, public_key)
  361. print("Encrypted message (C):", ciphertext)
  362.  
  363.  
  364. # Decrypt the message
  365. decrypted_message = decrypt(ciphertext, private_key)
  366. print("Decrypted message (M):", decrypted_message)
  367. # just run, 17, 11, 88
  368.  
  369.  
  370. SHA
  371. # Initial hash values
  372. a = 0x6A09E667F3BCC908
  373. b = 0xBB67AE8584CAA73B
  374. c = 0x3C6EF372FE94F82B
  375. d = 0xA54FF53A5F1D36F1
  376. e = 0x510E527FADE682D1
  377. f = 0x9B05688C2B3E6C1F
  378. g = 0x1F83D9ABFB41BD6B
  379. h = 0x5BE0CD19137E2179
  380.  
  381.  
  382. # Message schedule constants
  383. K = [
  384.     0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
  385.     0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
  386.     0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
  387.     0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694,
  388.     0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
  389.     0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
  390.     0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
  391.     0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70,
  392.     0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
  393.     0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B,
  394.     0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
  395.     0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8,
  396.     0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
  397.     0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
  398.     0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
  399.     0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
  400.     0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
  401.     0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B,
  402.     0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
  403.     0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817,
  404. ]
  405.  
  406.  
  407. # Initial message schedule
  408. W = [0] * 80
  409.  
  410.  
  411. # Get user input for the message
  412. message = input("Enter the message: ").encode('utf-8')
  413. bit_len = len(message) * 8
  414. message += b'\x80'
  415. while (len(message) + 8) % 128 != 0:
  416.     message += b'\x00'
  417. message += bit_len.to_bytes(8, 'big')
  418.  
  419.  
  420.  
  421.  
  422. # Process the message in blocks of 1024 bits
  423. for i in range(0, len(message), 128):
  424.     block = message[i:i+128]
  425.    
  426.     # Break the block into 64 64-bit words
  427.     for j in range(16):
  428.         W[j] = int.from_bytes(block[j*8:(j+1)*8], 'big')
  429.  
  430.  
  431.     # Extend the 16 64-bit words into 80 64-bit words
  432.     for j in range(16, 80):
  433.         s0 = (W[j-15] >> 19 | W[j-15] << 63) ^ (W[j-15] >> 8 | W[j-15] << 56) ^ (W[j-15] >> 7)
  434.         s1 = (W[j-2] >> 1 | W[j-2] << 45) ^ (W[j-2] >> 61 | W[j-2] << 3) ^ (W[j-2] >> 6)
  435.         W[j] = (W[j-16] + s0 + W[j-7] + s1) & 0xFFFFFFFFFFFFFFFF
  436.  
  437.  
  438.     # Initialize working variables
  439.     a, b, c, d, e, f, g, h = a, b, c, d, e, f, g, h
  440.  
  441.  
  442.     # Main loop
  443.     for j in range(80):
  444.         s0 = (a >> 28 | a << 36) ^ (a >> 34 | a << 30) ^ (a >> 39 | a << 25)
  445.         s1 = (e >> 14 | e << 50) ^ (e >> 18 | e << 26) ^ (e >> 41 | e << 23)
  446.         ch = (e & f) ^ (~e & g)
  447.         maj = (a & b) ^ (a & c) ^ (b & c)
  448.         temp1 = h + s1 + ch + K[j] + W[j]
  449.         temp2 = s0 + maj
  450.  
  451.  
  452.         h = g
  453.         g = f
  454.         f = e
  455.         e = (d + temp1) & 0xFFFFFFFFFFFFFFFF
  456.         d = c
  457.         c = b
  458.         b = a
  459.         a = (temp1 + temp2) & 0xFFFFFFFFFFFFFFFF
  460.  
  461.  
  462.         # Print values for each word and round
  463.         print(f"w{j+1:02} = {W[j]:016X} Round {j+1}: a = {a:016X} b = {b:016X} c = {c:016X} d = {d:016X} e = {e:016X} f = {f:016X} g = {g:016X} h = {h:016X}\n")
  464.     print("new block")  
  465.  
  466.  
  467. # Print the final hash value
  468. hash_result = (a, b, c, d, e, f, g, h)
  469. final_hash = ''.join(format(word, '016X') for word in hash_result)
  470. print(f"Final Hash/Message Digest (512 bits): {final_hash}")
  471.  
  472.  

Editor

You can edit this paste and save as new:


File Description
  • DIFmdRs
  • Paste Code
  • 23 Apr-2024
  • 12.39 Kb
You can Share it: