- DIFFY
- ALICE
- import socket
- import math
- def mod_exp(base, exponent, modulus):
- return pow(base, exponent, modulus)
- def compute_Y(alpha, X, q):
- return mod_exp(alpha, X, q)
- def compute_shared_key(Y, X, q):
- return mod_exp(Y, X, q)
- def alice():
- alpha = 10 # Specified value
- q = 16 # Specified value
- X = int(input("Enter XA for Alice: "))
- YA = compute_Y(alpha, X, q)
- print(f"YA={YA}")
- # Communicate with Dart
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_dart:
- s_dart.connect(('localhost', 5001))
- s_dart.send(','.join([str(x) for x in [alpha, X, q, YA]]).encode('utf-8'))
- # Receive YD2 from Dart
- YD2_dart = int(s_dart.recv(1024).decode('utf-8'))
- print(f"Received YD2 from Dart: {YD2_dart}")
- # Compute and display K2
- K2 = compute_shared_key(YD2_dart, X, q)
- print(f"K2={K2}")
- if name == "main":
- alice()
- BOB
- import socket
- import math
- def mod_exp(base, exponent, modulus):
- return pow(base, exponent, modulus)
- def compute_Y(alpha, X, q):
- return mod_exp(alpha, X, q)
- def compute_shared_key(Y, X, q):
- return mod_exp(Y, X, q)
- def bob():
- alpha = 10 # Specified value
- q = 16 # Specified value
- XB = int(input("Enter XB for Bob: "))
- YB = compute_Y(alpha, XB, q)
- print(f"YB={YB}")
- # Communicate with Dart
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_dart:
- s_dart.bind(('localhost', 5002))
- s_dart.connect(1)
- print("Bob terminal is listening for Dart on port 5002")
- conn_dart, addr_dart = s_dart.accept()
- with conn_dart:
- print(f"Connection established with Dart: {addr_dart}")
- # Receive YD1 from Dart
- YD1_dart = int(conn_dart.recv(1024).decode('utf-8'))
- print(f"Received YD1 from Dart: {YD1_dart}")
- # Additional communication with Dart can be added here if needed
- # Compute and display K1
- K1 = compute_shared_key(YD1_dart, XB, q)
- print(f"K1={K1}")
- if name == "main":
- bob()
- DARTH
- import socket
- import math
- def mod_exp(base, exponent, modulus):
- return pow(base, exponent, modulus)
- def compute_Y(alpha, X, q):
- return mod_exp(alpha, X, q)
- def compute_k1(YB, XD1, q):
- return mod_exp(YB, 5, q)
- def compute_k2(YA, XD2, q):
- return mod_exp(YA, XD2, q)
- def compute_shared_key(Y, X, q):
- return mod_exp(Y, X, q)
- def dart():
- alpha = 15 # Specified value
- q = 12 # Specified value
- # Communicate with Alice
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_alice:
- s_alice.bind(('localhost', 5001))
- s_alice.listen(1)
- print("Dart terminal is listening for Alice on port 5001")
- conn_alice, addr_alice = s_alice.accept()
- with conn_alice:
- print(f"Connection established with Alice: {addr_alice}")
- data_alice = conn_alice.recv(1024).decode('utf-8')
- alpha, X, q, YA = map(int, data_alice.split(','))
- print(f"Received data from Alice: alpha={alpha}, X={X}, q={q}, YA={YA}")
- XD1 = int(input("Enter XD1: "))
- XD2 = int(input("Enter XD2: "))
- YD1 = compute_Y(alpha, XD1, q)
- YD2 = compute_Y(alpha, XD2, q)
- print(f"YD1={YD1}, YD2={YD2}")
- K1 = compute_k1(YD1, XD1, q)
- K2 = compute_k2(YD2, X, q)
- print(f"K1={K1}, K2={K2}")
- # Send YD2 to Alice
- conn_alice.send(str(YD2).encode('utf-8'))
- # Send YD1 to Bob
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s_bob:
- s_bob.connect(('localhost', 5002))
- s_bob.send(str(YD1).encode('utf-8'))
- # Additional communication with Bob can be added here if needed
- if name == "main":
- dart()
- MD5
- import math
- # Define MD5 specific functions
- def F(X, Y, Z):
- return (X & Y) | (~X & Z)
- def G(X, Y, Z):
- return (X & Z) | (Y & ~Z)
- def H(X, Y, Z):
- return X ^ Y ^ Z
- def I(X, Y, Z):
- return Y ^ (X | ~Z)
- # Define shift and constant values
- s = [
- 7, 12, 17, 22,
- 5, 9, 14, 20,
- 4, 11, 16, 23,
- 6, 10, 15, 21
- ]
- K = [int(abs(math.sin(i + 1)) * 2 ** 32) & 0xFFFFFFFF for i in range(64)]
- # Initialize variables
- A = 0x01234567
- B = 0x89ABCDEF
- C = 0xFEDCBA98
- D = 0x76543210
- def md5(message):
- global A, B, C, D
- message = bytearray(message, 'utf-8')
- for i in range(0, len(message), 64):
- X = [0] * 16
- for j in range(16):
- X[j] = int.from_bytes(message[i + j*4:i + j*4 + 4], byteorder='little')
- AA, BB, CC, DD = A, B, C, D
- # Round 1
- for j in range(16):
- 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
- A, B, C, D = D, A, B, C
- print(f"Round 1 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
- # Round 2
- for j in range(16):
- 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
- A, B, C, D = D, A, B, C
- print(f"Round 2 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
- # Round 3
- for j in range(16):
- 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
- A, B, C, D = D, A, B, C
- print(f"Round 3 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
- # Round 4
- for j in range(16):
- 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
- A, B, C, D = D, A, B, C
- print(f"Round 4 - Final Output: {A:08x} {B:08x} {C:08x} {D:08x}")
- A = (A + AA) & 0xFFFFFFFF
- B = (B + BB) & 0xFFFFFFFF
- C = (C + CC) & 0xFFFFFFFF
- D = (D + DD) & 0xFFFFFFFF
- digest = bytearray()
- for val in (A, B, C, D):
- digest.extend(val.to_bytes(4, byteorder='little'))
- final_digest = digest * 4 # Concatenate the 128-bit digest four times to get a 512-bit digest
- return final_digest.hex()[:32] # Return only the first 32 characters (128 bits) of the digest
- # Main function
- if name == "main":
- message = input("Enter a message to encrypt: ")
- digest = md5(message)
- print(f"MD5 Digest (128 bits): {digest}")
- RSA
- def gcd(a, b):
- while b:
- a, b = b, a % b
- return a
- def mod_inverse(a, m):
- m0, x0, x1 = m, 0, 1
- while a > 1:
- q = a // m
- m, a = a % m, m
- x0, x1 = x1 - q * x0, x0
- return x1 + m0 if x1 < 0 else x1
- def generate_keypair(p, q):
- n = p * q
- totient = (p - 1) * (q - 1)
- # Choose e such that 1 < e < totient(n) and gcd(e, totient(n)) = 1
- e = 2
- while gcd(e, totient) != 1:
- e += 1
- # Calculate d, the modular multiplicative inverse of e (mod totient(n))
- d = mod_inverse(e, totient)
- public_key = (e, n)
- private_key = (d, n)
- print("e:", e)
- print("where GCD(e, totient(n)=1)")
- return public_key, private_key
- def encrypt(message, public_key):
- e, n = public_key
- return pow(message, e, n)
- def decrypt(ciphertext, private_key):
- d, n = private_key
- return pow(ciphertext, d, n)
- # Example input: prime numbers p and q
- p = int(input("Enter prime number p: "))
- q = int(input("Enter prime number q: "))
- # Calculate n and totient
- n = p * q
- print("n:", n)
- totient = (p - 1) * (q - 1)
- print("totient(n):", totient)
- # Generate key pair
- public_key, private_key = generate_keypair(p, q)
- # Display public key and private key
- print("Public key (e, n):", public_key)
- print("Private key (d, n):", private_key)
- # Example message to encrypt
- message = int(input("Enter message to encrypt: "))
- # Encrypt the message
- ciphertext = encrypt(message, public_key)
- print("Encrypted message (C):", ciphertext)
- # Decrypt the message
- decrypted_message = decrypt(ciphertext, private_key)
- print("Decrypted message (M):", decrypted_message)
- # just run, 17, 11, 88
- SHA
- # Initial hash values
- a = 0x6A09E667F3BCC908
- b = 0xBB67AE8584CAA73B
- c = 0x3C6EF372FE94F82B
- d = 0xA54FF53A5F1D36F1
- e = 0x510E527FADE682D1
- f = 0x9B05688C2B3E6C1F
- g = 0x1F83D9ABFB41BD6B
- h = 0x5BE0CD19137E2179
- # Message schedule constants
- K = [
- 0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC,
- 0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118,
- 0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2,
- 0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694,
- 0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65,
- 0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5,
- 0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4,
- 0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70,
- 0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF,
- 0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B,
- 0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30,
- 0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8,
- 0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8,
- 0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3,
- 0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC,
- 0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B,
- 0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178,
- 0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B,
- 0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C,
- 0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817,
- ]
- # Initial message schedule
- W = [0] * 80
- # Get user input for the message
- message = input("Enter the message: ").encode('utf-8')
- bit_len = len(message) * 8
- message += b'\x80'
- while (len(message) + 8) % 128 != 0:
- message += b'\x00'
- message += bit_len.to_bytes(8, 'big')
- # Process the message in blocks of 1024 bits
- for i in range(0, len(message), 128):
- block = message[i:i+128]
- # Break the block into 64 64-bit words
- for j in range(16):
- W[j] = int.from_bytes(block[j*8:(j+1)*8], 'big')
- # Extend the 16 64-bit words into 80 64-bit words
- for j in range(16, 80):
- s0 = (W[j-15] >> 19 | W[j-15] << 63) ^ (W[j-15] >> 8 | W[j-15] << 56) ^ (W[j-15] >> 7)
- s1 = (W[j-2] >> 1 | W[j-2] << 45) ^ (W[j-2] >> 61 | W[j-2] << 3) ^ (W[j-2] >> 6)
- W[j] = (W[j-16] + s0 + W[j-7] + s1) & 0xFFFFFFFFFFFFFFFF
- # Initialize working variables
- a, b, c, d, e, f, g, h = a, b, c, d, e, f, g, h
- # Main loop
- for j in range(80):
- s0 = (a >> 28 | a << 36) ^ (a >> 34 | a << 30) ^ (a >> 39 | a << 25)
- s1 = (e >> 14 | e << 50) ^ (e >> 18 | e << 26) ^ (e >> 41 | e << 23)
- ch = (e & f) ^ (~e & g)
- maj = (a & b) ^ (a & c) ^ (b & c)
- temp1 = h + s1 + ch + K[j] + W[j]
- temp2 = s0 + maj
- h = g
- g = f
- f = e
- e = (d + temp1) & 0xFFFFFFFFFFFFFFFF
- d = c
- c = b
- b = a
- a = (temp1 + temp2) & 0xFFFFFFFFFFFFFFFF
- # Print values for each word and round
- 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")
- print("new block")
- # Print the final hash value
- hash_result = (a, b, c, d, e, f, g, h)
- final_hash = ''.join(format(word, '016X') for word in hash_result)
- print(f"Final Hash/Message Digest (512 bits): {final_hash}")
[text] DIFmdRs
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: