Advertisement
Guest User

Lab Template code

a guest
Apr 8th, 2020
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. from Crypto.Cipher import AES
  2. from Crypto.Random import random
  3. from blessings import Terminal
  4. import base64
  5. import string, os, hashlib
  6. import sys, getopt, socket
  7.  
  8. BUFFER_SIZE = 2048
  9. port=4632
  10. host="192.168.1.50"
  11.  
  12. def getKeyFromPassword(password):
  13.     h = hashlib.sha512()
  14.     h.update(password)
  15.     d = h.digest()[0:16]
  16.     return d
  17.  
  18. def pkcs7Padding (data, blockSize):
  19.     # First determine how much is needed
  20.     bytesNeeded = blockSize-len(data)%blockSize
  21.     return data + chr(bytesNeeded)*bytesNeeded
  22.  
  23. def isPkcs7PaddingValid(data, blockSize):
  24.     last Char=data[-1)
  25.     if (ord(lastChar) > blockSize or ord(lastChar)==0):
  26.         return False
  27.     for x in range (len(data)-ord(last Char), len(data)):
  28.         if data[x) != lastChar:
  29.             return False
  30.     return True
  31.  
  32. def removePkcs7Padding (dat a, blockSize):
  33.     if isPkcs7PaddingValid(data, blockSize):
  34.         lastChar=data[-1]
  35.         return data[:len(data)-ord(lastChar)]
  36.     else:
  37.         return data
  38.  
  39. print "Connecting to port " + str(port)
  40. # Create a TCP/IP socket
  41. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  42. try:
  43.     # Connect the socket to the port where the server is listening
  44.     server_address = (host, port)
  45.     #print >>sys . stderr, 'connecting to %s port %s' % server_address
  46.     sock.connect(server_address)
  47.     print 'Connected to %s port %s' % server_address
  48. except socket.error:
  49.     print >>sys.stderr, "Could not connect to the assignment server"
  50.     exit(l)
  51.  
  52. sock.send("l\n")
  53. reply = sock.recv(4096)
  54. # At this point the files should be on the Desktop in ciphertext1 and ciphertext2
  55. print reply
  56.  
  57. # Part 1
  58. with open("./ciphertextl", "r") as cl:
  59.     ciphertextl = cl.read()
  60.  
  61. # This will parse the IV and ciphertext
  62. # Note that the file contains the hex string of the binary content
  63. # So we must decode it into a regular Python string
  64. ivl = ciphertextl[0:32).decode("hex")
  65. ciphertextl = ciphertextl[32:].decode("hex")
  66.  
  67. ####################################
  68. # Now decrypt the ciphertextl file#
  69. ####################################
  70. # TODO
  71. # Sample decryption code:
  72. # cipher = AES.new("<KEY>", AES.MODE_CBC, <"IV">)
  73. # cipher.decrypt(ciphertext)
  74. # where <KEY> is replaced by the key derived from the password
  75. # and <IV> is replaced by the iv from the file
  76.  
  77. with open("./ciphertext2", "r") as c2:
  78.     ciphertext2 = c2.read()
  79.  
  80. ########################################################
  81. # Parse the IV and ciphertext out.                     #
  82. # Cycle through the rockyou.txt file and test decrypt. #
  83. # When the padding is correct, print the password out. #
  84. ########################################################
  85. # TODO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement