Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -- coding: utf-8 --
- import time
- import socket
- from random import getrandbits
- import sha256
- import AES
- sock = socket.socket()
- sock.connect(('192.168.0.10', 9090))
- # Функция возведение в степень по модулю
- def pow_mod(x, y, z):
- number = 1
- while y:
- if y & 1:
- number = number * x % z
- y >>= 1
- x = x * x % z
- return number
- array = []
- #Diffie-Hellman and first sending after connection
- g = 2
- prime = 7919
- bits = 32
- a = getrandbits(bits)
- print("a = "+str(a))
- sock.send(str(g).encode('utf8'))
- time.sleep(1)
- sock.send(str(prime).encode('utf8'))
- time.sleep(1)
- sock.send(str(pow(g, a, prime)).encode('utf8'))
- print("A = "+ str(pow(g, a, prime)))
- #Diffie-Hellman
- def Diffie_Hellman(B):
- sym_key = pow(B, a, prime)
- return str(sym_key)
- # 2 Функции нахождения d, мультиприкативно обратного по модулю. Расширенный алгоритм евклида
- def extended_gcd(a, b):
- x = 0
- y = 1
- lx = 1
- ly = 0
- oa = a
- ob = b
- while b != 0:
- q = a // b
- (a, b) = (b, a % b)
- (x, lx) = ((lx - (q * x)), x)
- (y, ly) = ((ly - (q * y)), y)
- if (lx < 0): lx += ob
- if (ly < 0): ly += oa
- return lx
- def secret_exp(e, p, q):
- phi_n = (p - 1) * (q - 1)
- d = extended_gcd(e, phi_n)
- return d
- # variables for RSA
- p = 90616372449506464795632382233197625012835195441861002997772088683163833175346890800504555974558822852050392479248153601909449469
- q = 49737963550113392255587354561530012739536093087993578789664292528482059608759747774698879066975239043402299636058235809117273623
- e = 65537
- n = p * q
- f_n = (p - 1) * (q - 1)
- d = secret_exp(e, p, q)
- def RSA_digital_ignature(data):
- hash = sha256.generate_hash(data).hex()
- print("hash = " + str(hash))
- asci_word = ""
- for char in hash: # Переводим наше слово в аски
- a = char
- b = ord(a)
- asci_word += str(b) # все слово
- asci_word = int(asci_word)
- cypher = pow_mod(asci_word, d, n)
- print("Зашифрованный хэш ="+str(cypher))
- return str(cypher)
- def change(data):
- array.append(data)
- print(array)
- if len(array) == 1:
- array.append(Diffie_Hellman(int(array[0])))
- print("Sym key = " + array[1])
- if len(array) == 2:
- sock.send(str(e).encode('utf8'))
- time.sleep(1)
- sock.send(str(n).encode('utf8'))
- array.append('end')
- print(array)
- while True:
- if len(array)<3:
- data = sock.recv(1024).decode('utf8')
- change(data)
- message = input("Введите сообщение, которое хотите отправить: ")
- message_cypher = AES.encrypt(array[1],message)
- print("Зашифрованное сообщение - "+str(message_cypher))
- sock.send(message_cypher)
- time.sleep(1)
- message_hash = RSA_digital_ignature(message)
- sock.send(message_hash.encode('utf8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement