Advertisement
Stingaleed

Socket-Client

May 19th, 2022
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -- coding: utf-8 --
  3. import time
  4. import socket
  5. from random import getrandbits
  6. import sha256
  7. import AES
  8.  
  9. sock = socket.socket()
  10. sock.connect(('192.168.0.10', 9090))
  11.  
  12. # Функция возведение в степень по модулю
  13. def pow_mod(x, y, z):
  14.     number = 1
  15.     while y:
  16.         if y & 1:
  17.             number = number * x % z
  18.         y >>= 1
  19.         x = x * x % z
  20.     return number
  21.  
  22. array = []
  23. #Diffie-Hellman and first sending after connection
  24. g = 2
  25. prime = 7919
  26. bits = 32
  27. a = getrandbits(bits)
  28. print("a = "+str(a))
  29.  
  30. sock.send(str(g).encode('utf8'))
  31. time.sleep(1)
  32. sock.send(str(prime).encode('utf8'))
  33. time.sleep(1)
  34. sock.send(str(pow(g, a, prime)).encode('utf8'))
  35. print("A = "+ str(pow(g, a, prime)))
  36.  
  37.  
  38. #Diffie-Hellman
  39. def Diffie_Hellman(B):
  40.     sym_key = pow(B, a, prime)
  41.     return str(sym_key)
  42.  
  43. # 2 Функции нахождения d, мультиприкативно обратного по модулю. Расширенный алгоритм евклида
  44. def extended_gcd(a, b):
  45.     x = 0
  46.     y = 1
  47.     lx = 1
  48.     ly = 0
  49.     oa = a
  50.     ob = b
  51.     while b != 0:
  52.         q = a // b
  53.         (a, b) = (b, a % b)
  54.         (x, lx) = ((lx - (q * x)), x)
  55.         (y, ly) = ((ly - (q * y)), y)
  56.     if (lx < 0): lx += ob
  57.     if (ly < 0): ly += oa
  58.     return lx
  59.  
  60. def secret_exp(e, p, q):
  61.     phi_n = (p - 1) * (q - 1)
  62.     d = extended_gcd(e, phi_n)
  63.     return d
  64.  
  65.  
  66. # variables for RSA
  67. p = 90616372449506464795632382233197625012835195441861002997772088683163833175346890800504555974558822852050392479248153601909449469
  68. q = 49737963550113392255587354561530012739536093087993578789664292528482059608759747774698879066975239043402299636058235809117273623
  69. e = 65537
  70. n = p * q
  71. f_n = (p - 1) * (q - 1)
  72. d = secret_exp(e, p, q)
  73.  
  74.  
  75. def RSA_digital_ignature(data):
  76.     hash = sha256.generate_hash(data).hex()
  77.     print("hash = " + str(hash))
  78.     asci_word = ""
  79.     for char in hash:  # Переводим наше слово в аски
  80.         a = char
  81.         b = ord(a)
  82.         asci_word += str(b)  # все слово
  83.     asci_word = int(asci_word)
  84.     cypher = pow_mod(asci_word, d, n)
  85.     print("Зашифрованный хэш ="+str(cypher))
  86.     return str(cypher)
  87.  
  88.  
  89. def change(data):
  90.     array.append(data)
  91.     print(array)
  92.     if len(array) == 1:
  93.         array.append(Diffie_Hellman(int(array[0])))
  94.         print("Sym key = " + array[1])
  95.     if len(array) == 2:
  96.         sock.send(str(e).encode('utf8'))
  97.         time.sleep(1)
  98.         sock.send(str(n).encode('utf8'))
  99.         array.append('end')
  100.  
  101.     print(array)
  102.  
  103.  
  104.  
  105.  
  106.  
  107. while True:
  108.  
  109.     if len(array)<3:
  110.         data = sock.recv(1024).decode('utf8')
  111.         change(data)
  112.  
  113.  
  114.     message = input("Введите сообщение, которое хотите отправить: ")
  115.     message_cypher = AES.encrypt(array[1],message)
  116.     print("Зашифрованное сообщение - "+str(message_cypher))
  117.     sock.send(message_cypher)
  118.     time.sleep(1)
  119.     message_hash = RSA_digital_ignature(message)
  120.     sock.send(message_hash.encode('utf8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement