Advertisement
Guest User

Untitled

a guest
Jun 11th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.55 KB | None | 0 0
  1. import os
  2. import sys
  3. import time
  4. import socket
  5. from Cryptodome.PublicKey import RSA
  6. from Cryptodome.Cipher import AES, PKCS1_OAEP
  7.  
  8. PORT = 8989
  9. RUNNING_DIRECTORY = os.getcwd()
  10.  
  11.  
  12. def send_small(text, key, sock):
  13.     cipher = AES.new(key, AES.MODE_EAX)
  14.     nonce = cipher.nonce
  15.     ciphertext, tag = Encryptsmall(cipher, text)
  16.     sock.send(ciphertext)
  17.     sock.send(tag)
  18.     sock.send(nonce)
  19.  
  20.  
  21. def recive_small(key, sock):
  22.     text = sock.recv(4096)
  23.     tag = sock.recv(4096)
  24.     nonce = sock.recv(4096)
  25.     cipher = AES.new(key, AES.MODE_EAX, nonce=nonce)
  26.     return Decryptsmall(cipher, text, tag)
  27.  
  28.  
  29. def Encryptsmall(cipher, data="Testing"):
  30.     ciphertext, tag = cipher.encrypt_and_digest(data)
  31.     return ciphertext, tag
  32.  
  33.  
  34. def Decryptsmall(cipher, ciphertext, tag):
  35.     plaintext = cipher.decrypt(ciphertext)
  36.     try:
  37.         cipher.verify(tag)
  38.         print("The message is authentic:", plaintext)
  39.     except ValueError:
  40.         print("Key incorrect or message corrupted")
  41.  
  42.     return plaintext
  43.  
  44.  
  45. def send_file(sending_file, key, sock):
  46.  
  47.     send_small("upload " + sending_file.split(os.sep)[-1], key, sock)
  48.     time.sleep(1)
  49.     file_to_send = open(sending_file, "rb")
  50.     l = file_to_send.read(1024)
  51.     while l:
  52.         time.sleep(3)
  53.         send_small(l, key, sock)
  54.         l = file_to_send.read(1024)
  55.         print "."
  56.     time.sleep(2)
  57.     send_small("!@#", key, sock)
  58.     print "Finished uploading"
  59.  
  60.  
  61. def decrypt_data(opened_file):
  62.     file_out = open("temp2.bin", "wb")
  63.     private_key = RSA.import_key(open('private.pem').read())
  64.  
  65.     print "."
  66.  
  67.     enc_session_key, nonce, tag, ciphertext = \
  68.         [opened_file.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1)]
  69.  
  70.     print "."
  71.  
  72.     # Decrypt the session key with the private RSA key
  73.     cipher_rsa = PKCS1_OAEP.new(private_key)
  74.     session_key = cipher_rsa.decrypt(enc_session_key)
  75.  
  76.     print "."
  77.  
  78.     # Decrypt the data with the AES session key
  79.     cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
  80.     data = cipher_aes.decrypt_and_verify(ciphertext, tag)
  81.     file_out.write(data)
  82.     print "finished decrypting"
  83.  
  84.  
  85. def generate_keys():
  86.     '''
  87.    Generate an RSA keypair with an exponent of 65537 in PEM format
  88.    param: bits The key length in bits
  89.    Return private key and public key
  90.    '''
  91.  
  92.     key = RSA.generate(2048)
  93.     private_key = key.export_key()
  94.     file_out = open("Private.pem", "wb+")
  95.     file_out.write(private_key)
  96.     file_out.close()
  97.  
  98.     public_key = key.publickey().export_key()
  99.     file_out = open("Public.pem", "wb+")
  100.     file_out.write(public_key)
  101.     file_out.close()
  102.  
  103.  
  104. def starting_up():
  105.     Pass = False
  106.  
  107.     if not (os.path.exists(RUNNING_DIRECTORY + '/Public.pem')):
  108.         print ("Generating Private and Public key.......")
  109.         generate_keys()
  110.  
  111.     else:
  112.         print ("Private and Public keys found")
  113.  
  114.     if not (os.path.exists(RUNNING_DIRECTORY + '/Recived_files')):
  115.         os.mkdir(RUNNING_DIRECTORY + '/Recived_files')
  116.  
  117.     print "Enter IP Of the Server"
  118.  
  119.     while Pass == False:
  120.         ip = raw_input()
  121.         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  122.         address = (ip, 8989)
  123.         try:
  124.             sock.connect(address)
  125.             Pass = True
  126.         except Exception, e:
  127.             print (e)
  128.             print "wrong ip\server isn't open try again"
  129.  
  130.     f = open("Public.pem", 'rb')
  131.     sock.send(f.read(4096))
  132.  
  133.     f = open("temp.bin", 'wb')  # opens file for encrypted data
  134.     f.write(sock.recv(4096))
  135.     f.close()
  136.     f = open("temp.bin", 'rb')  # opens file in read mode
  137.  
  138.     decrypt_data(f)
  139.     f.close()
  140.  
  141.     os.remove("temp.bin")  # removes uneccesery files
  142.  
  143.     f = open('temp2.bin')
  144.     key = f.read(4096)
  145.     f.close()
  146.  
  147.     os.remove('temp2.bin')
  148.  
  149.     print key
  150.  
  151.     return key, sock
  152.  
  153.  
  154. def main():
  155.  
  156.     command = ""
  157.     password = ""
  158.     username = ""
  159.     server_response = ""
  160.     key, sock = starting_up()
  161.  
  162.     repassword = "!"
  163.     print 'would you like to:'
  164.     print '1) sign in'
  165.     print '2) sign up'
  166.     print '3) exit'
  167.  
  168.     while True:
  169.  
  170.         response = raw_input()
  171.  
  172.         if response == "1":
  173.             print ("Username: ")
  174.             username = raw_input()
  175.             print ("Password: ")
  176.             password = raw_input()
  177.             send_small("SI " + username + " " + password, key, sock)
  178.  
  179.             server_response = recive_small(key, sock)
  180.             if server_response == "Cool":
  181.                 print "Welcome back " + username
  182.                 break
  183.             else:
  184.                 print "wrong username/password"
  185.                 continue
  186.  
  187.         if response == "2":
  188.             print ("Username: ")
  189.             username = raw_input()
  190.             while password != repassword:
  191.                 print ("Password: ")
  192.                 password = raw_input()
  193.                 print "Repeat password"
  194.                 repassword = raw_input()
  195.                 if password == repassword:
  196.                     send_small("S_U " + username + " " + password, key, sock)
  197.  
  198.                     server_response = recive_small(key, sock)
  199.  
  200.                     if server_response == "Cool":
  201.                         print "welcome " + username
  202.                         break
  203.  
  204.                 else:
  205.                     print "passwords don't match try again"
  206.                     continue
  207.             if server_response == "Cool":
  208.                 break
  209.             else:
  210.                 print "username already taken"
  211.                 continue
  212.  
  213.         if response == "3":
  214.             send_small("bye", key, sock)
  215.             sock.close()
  216.             sys.exit()
  217.  
  218.         else:
  219.             continue
  220.  
  221.     current_dir = ""
  222.  
  223.     while command != "disconnect":
  224.         print "command:"
  225.         command = raw_input()
  226.  
  227.         if command.split(" ")[0] == "list":
  228.             send_small(current_dir, key, sock)
  229.             print recive_small(key, sock)
  230.  
  231.         if command.split(" ")[0] == "cd":
  232.             send_small("cd " + current_dir + command.split(" ")[1] + "/", key, sock)
  233.             if recive_small(key, sock) == "OK":
  234.                 current_dir += "/" + command.split(" ")[1]
  235.                 print current_dir
  236.             else:
  237.                 print "directory doesn't exist"
  238.  
  239.         if command.split(" ")[0] == "cd ..":
  240.             send_small(command.split(" ")[0] + current_dir, key, sock)
  241.             if recive_small(key, sock) == "OK":
  242.                 print ok
  243.  
  244.         if command.split(" ")[0] == "upload":
  245.             send_file(command.split(" ")[1], key, sock)
  246.  
  247.  
  248. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement