Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import time
- import socket
- from Cryptodome.PublicKey import RSA
- from Cryptodome.Cipher import AES, PKCS1_OAEP
- PORT = 8989
- RUNNING_DIRECTORY = os.getcwd()
- def send_small(text, key, sock):
- cipher = AES.new(key, AES.MODE_EAX)
- nonce = cipher.nonce
- ciphertext, tag = Encryptsmall(cipher, text)
- sock.send(ciphertext)
- sock.send(tag)
- sock.send(nonce)
- def recive_small(key, sock):
- text = sock.recv(4096)
- tag = sock.recv(4096)
- nonce = sock.recv(4096)
- cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
- return Decryptsmall(cipher, text, tag)
- def Encryptsmall(cipher, data="Testing"):
- ciphertext, tag = cipher.encrypt_and_digest(data)
- return ciphertext, tag
- def Decryptsmall(cipher, ciphertext, tag):
- plaintext = cipher.decrypt(ciphertext)
- try:
- cipher.verify(tag)
- print("The message is authentic:", plaintext)
- except ValueError:
- print("Key incorrect or message corrupted")
- return plaintext
- def send_file(sending_file, key, sock):
- send_small("upload " + sending_file.split(os.sep)[-1], key, sock)
- time.sleep(1)
- file_to_send = open(sending_file, "rb")
- l = file_to_send.read(1024)
- while l:
- time.sleep(3)
- send_small(l, key, sock)
- l = file_to_send.read(1024)
- print "."
- time.sleep(2)
- send_small("!@#", key, sock)
- print "Finished uploading"
- def decrypt_data(opened_file):
- file_out = open("temp2.bin", "wb")
- private_key = RSA.import_key(open('private.pem').read())
- print "."
- enc_session_key, nonce, tag, ciphertext = \
- [opened_file.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1)]
- print "."
- # Decrypt the session key with the private RSA key
- cipher_rsa = PKCS1_OAEP.new(private_key)
- session_key = cipher_rsa.decrypt(enc_session_key)
- print "."
- # Decrypt the data with the AES session key
- cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
- data = cipher_aes.decrypt_and_verify(ciphertext, tag)
- file_out.write(data)
- print "finished decrypting"
- def generate_keys():
- '''
- Generate an RSA keypair with an exponent of 65537 in PEM format
- param: bits The key length in bits
- Return private key and public key
- '''
- key = RSA.generate(2048)
- private_key = key.export_key()
- file_out = open("Private.pem", "wb+")
- file_out.write(private_key)
- file_out.close()
- public_key = key.publickey().export_key()
- file_out = open("Public.pem", "wb+")
- file_out.write(public_key)
- file_out.close()
- def starting_up():
- Pass = False
- if not (os.path.exists(RUNNING_DIRECTORY + '/Public.pem')):
- print ("Generating Private and Public key.......")
- generate_keys()
- else:
- print ("Private and Public keys found")
- if not (os.path.exists(RUNNING_DIRECTORY + '/Recived_files')):
- os.mkdir(RUNNING_DIRECTORY + '/Recived_files')
- print "Enter IP Of the Server"
- while Pass == False:
- ip = raw_input()
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- address = (ip, 8989)
- try:
- sock.connect(address)
- Pass = True
- except Exception, e:
- print (e)
- print "wrong ip\server isn't open try again"
- f = open("Public.pem", 'rb')
- sock.send(f.read(4096))
- f = open("temp.bin", 'wb') # opens file for encrypted data
- f.write(sock.recv(4096))
- f.close()
- f = open("temp.bin", 'rb') # opens file in read mode
- decrypt_data(f)
- f.close()
- os.remove("temp.bin") # removes uneccesery files
- f = open('temp2.bin')
- key = f.read(4096)
- f.close()
- os.remove('temp2.bin')
- print key
- return key, sock
- def main():
- command = ""
- password = ""
- username = ""
- server_response = ""
- key, sock = starting_up()
- repassword = "!"
- print 'would you like to:'
- print '1) sign in'
- print '2) sign up'
- print '3) exit'
- while True:
- response = raw_input()
- if response == "1":
- print ("Username: ")
- username = raw_input()
- print ("Password: ")
- password = raw_input()
- send_small("SI " + username + " " + password, key, sock)
- server_response = recive_small(key, sock)
- if server_response == "Cool":
- print "Welcome back " + username
- break
- else:
- print "wrong username/password"
- continue
- if response == "2":
- print ("Username: ")
- username = raw_input()
- while password != repassword:
- print ("Password: ")
- password = raw_input()
- print "Repeat password"
- repassword = raw_input()
- if password == repassword:
- send_small("S_U " + username + " " + password, key, sock)
- server_response = recive_small(key, sock)
- if server_response == "Cool":
- print "welcome " + username
- break
- else:
- print "passwords don't match try again"
- continue
- if server_response == "Cool":
- break
- else:
- print "username already taken"
- continue
- if response == "3":
- send_small("bye", key, sock)
- sock.close()
- sys.exit()
- else:
- continue
- current_dir = ""
- while command != "disconnect":
- print "command:"
- command = raw_input()
- if command.split(" ")[0] == "list":
- send_small(current_dir, key, sock)
- print recive_small(key, sock)
- if command.split(" ")[0] == "cd":
- send_small("cd " + current_dir + command.split(" ")[1] + "/", key, sock)
- if recive_small(key, sock) == "OK":
- current_dir += "/" + command.split(" ")[1]
- print current_dir
- else:
- print "directory doesn't exist"
- if command.split(" ")[0] == "cd ..":
- send_small(command.split(" ")[0] + current_dir, key, sock)
- if recive_small(key, sock) == "OK":
- print ok
- if command.split(" ")[0] == "upload":
- send_file(command.split(" ")[1], key, sock)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement