Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Crypto.Cipher import AES
- from Crypto.Random import random
- from blessings import Terminal
- import base64
- import string, os, hashlib
- import sys, getopt, socket
- BUFFER_SIZE = 2048
- port=4632
- host="192.168.1.50"
- def getKeyFromPassword(password):
- h = hashlib.sha512()
- h.update(password)
- d = h.digest()[0:16]
- return d
- def pkcs7Padding (data, blockSize):
- # First determine how much is needed
- bytesNeeded = blockSize-len(data)%blockSize
- return data + chr(bytesNeeded)*bytesNeeded
- def isPkcs7PaddingValid(data, blockSize):
- last Char=data[-1)
- if (ord(lastChar) > blockSize or ord(lastChar)==0):
- return False
- for x in range (len(data)-ord(last Char), len(data)):
- if data[x) != lastChar:
- return False
- return True
- def removePkcs7Padding (dat a, blockSize):
- if isPkcs7PaddingValid(data, blockSize):
- lastChar=data[-1]
- return data[:len(data)-ord(lastChar)]
- else:
- return data
- print "Connecting to port " + str(port)
- # Create a TCP/IP socket
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
- # Connect the socket to the port where the server is listening
- server_address = (host, port)
- #print >>sys . stderr, 'connecting to %s port %s' % server_address
- sock.connect(server_address)
- print 'Connected to %s port %s' % server_address
- except socket.error:
- print >>sys.stderr, "Could not connect to the assignment server"
- exit(l)
- sock.send("l\n")
- reply = sock.recv(4096)
- # At this point the files should be on the Desktop in ciphertext1 and ciphertext2
- print reply
- # Part 1
- with open("./ciphertextl", "r") as cl:
- ciphertextl = cl.read()
- # This will parse the IV and ciphertext
- # Note that the file contains the hex string of the binary content
- # So we must decode it into a regular Python string
- ivl = ciphertextl[0:32).decode("hex")
- ciphertextl = ciphertextl[32:].decode("hex")
- ####################################
- # Now decrypt the ciphertextl file#
- ####################################
- # TODO
- # Sample decryption code:
- # cipher = AES.new("<KEY>", AES.MODE_CBC, <"IV">)
- # cipher.decrypt(ciphertext)
- # where <KEY> is replaced by the key derived from the password
- # and <IV> is replaced by the iv from the file
- with open("./ciphertext2", "r") as c2:
- ciphertext2 = c2.read()
- ########################################################
- # Parse the IV and ciphertext out. #
- # Cycle through the rockyou.txt file and test decrypt. #
- # When the padding is correct, print the password out. #
- ########################################################
- # TODO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement