def encrypt(keyfile, plaintext): ciphertext = None retval = 0 try: # name the temporary files randomly # since the client and server are in the same dir, sometimes # when both tried to use openSSL simultaneously there were collisions temp_name = str(uuid.uuid1()) plaintext_file = 'p' + temp_name cipher_file = 'c' + temp_name # write out input / create output files fileIO.writeFile(plaintext_file, plaintext) fileIO.writeFile(cipher_file, '') # run openssl enc command with open(keyfile) as f: retval = subprocess.call(['openssl', 'enc', '-aes-256-cbc', '-a', '-pass', 'stdin', '-out', cipher_file, '-in', plaintext_file], stdin=f) # read in the output ciphertext = fileIO.readFile(cipher_file) finally: # delete temp files fileIO.removeFile(plaintext_file) fileIO.removeFile(cipher_file) # return the output if ciphertext is None or retval != 0: raise Exception('Encrypt failed') else: return ciphertext
def storeFiletable(peer_name, dictionary): filetable_name = 'CA/' + peer_name + '/filetable.dat' keyfile = 'CA/' + peer_name + '/' + peer_name + '.key' # If we don't remove the file before rewriting it we get bad decrypts fileIO.removeFile(filetable_name) ciphertext = openSSL.encrypt(keyfile, str(dictionary)) fileIO.writeFile(filetable_name, ciphertext)
def hash(plaintext, outputType = '-hex'): hashKey = None retval = 0 try: # name the temporary files randomly # since the client and server are in the same dir, sometimes # when both tried to use openSSL simultaneously there were collisions temp_name = str(uuid.uuid1()) plaintext_file = 'p' + temp_name key_file = 'k' + temp_name # write out input / create output files fileIO.writeFile(plaintext_file, plaintext) fileIO.writeFile(key_file, '') # run openssl hash command retval = subprocess.call(['openssl', 'dgst', '-sha256', outputType, '-out', key_file, plaintext_file]) # read in the output hashKey = fileIO.readFile(key_file) finally: # delete temp files fileIO.removeFile(plaintext_file) fileIO.removeFile(key_file) # return the output if hashKey is None or retval != 0: raise Exception('Hash failed') else: return hashKey.replace('SHA256(plain.tmp)= ', '', 1)
def save(self): contents = "" for level in self: line = level for item in Progress.attributes: line += "," + str(self[level][item]) contents += line + "\n" writeFile(Progress.path, contents)
def storeFile(peer_name, net): peer_info = net.getPeerInfo() client_common_name = peer_info[4][0][1] filename = net.recv() file_contents = net.recv() fileIO.writeFile(tmp_file_dir + '/' + filename, file_contents) os.chdir('refmon') process = subprocess.Popen(['ocaml', 'RefMon.ml', peer_name, 'execute', client_common_name, 'put', filename], stdout=subprocess.PIPE) out, err = process.communicate() os.chdir('..') net.send(out.decode("utf-8")) fileIO.removeFile(tmp_file_dir + '/' + filename) print('Stored', filename, 'for', client_common_name)
def hash(plaintext, outputType): hashKey = None retval = 0 try: # write out input / create output files fileIO.writeFile('plain.tmp', plaintext) fileIO.writeFile('key.tmp', "") # run openssl hash command retval = subprocess.call(['openssl', 'dgst', '-sha256', outputType, '-out', 'key.tmp', 'plain.tmp']) # read in the output hashKey = fileIO.readFile('key.tmp') finally: # delete temp files fileIO.removeFile('plain.tmp') fileIO.removeFile('key.tmp') # return the output if hashKey is None or retval != 0: raise Exception("hash failed") else: return hashKey.replace('SHA256(plain.tmp)= ', '', 1)
def retrieveFile(peer_name, filename): # Use this peer's private key as the keyfile for decrypting the file (Symmetric encryption) keyfile = 'CA/' + peer_name + '/' + peer_name + '.key' # Look up which host has the file if filename in filetable.getFiletable(peer_name): file_host = filetable.getFiletable(peer_name)[filename] # Use the peer_map to figure out the address of that host file_location = peer_ip.peer_map[file_host] else: print('What host would you like to retrieve the file from?') host_input = input() file_location = None # Request a list of files we can access from every host for hostname, host in peer_ip.peer_map.items(): name = str(eval(hostname)[4][0][1]) if(host_input == name): file_location = host if(file_location == None): print("Host not found") return # Hash the filename so it matches what we stored on the host # hashed_filename = hash_filename(filename) # Request the file from the host net = client_side_connection.ClientSideConnection( peer_name = peer_name, ip = file_location[0], portno = int(file_location[1])) net.send('Retrieve') net.send(filename) # Wait for the file contents, then decrypt them and store them file_contents = net.recv() # file_contents = openSSL.decrypt(keyfile, encrypted_contents) print(net.recv()) fileIO.writeFile(filename, file_contents) net.done()
def encrypt(key, plaintext): ciphertext = None retval = 0 try: # write out input / create output files fileIO.writeFile('key.tmp', key) fileIO.writeFile('plain.tmp', plaintext) fileIO.writeFile('cipher.tmp', "") # run openssl enc command with file('key.tmp') as f: retval = subprocess.call([ 'openssl', 'enc', '-aes-256-cbc', '-pass', 'stdin', '-out', 'cipher.tmp', '-in', 'plain.tmp' ], stdin=f) # read in the output ciphertext = fileIO.readFile('cipher.tmp') finally: # delete temp files fileIO.removeFile('key.tmp') fileIO.removeFile('plain.tmp') fileIO.removeFile('cipher.tmp') # return the output if ciphertext is None or retval != 0: raise Exception("encrypt failed") else: return ciphertext
def generatePassword(): password = None retval = 0 try: # get the current time time = datetime.now().time() # write out input / create output files fileIO.writeFile('rand.tmp', "") # run openssl rand command retval = subprocess.call(['openssl', 'rand', '-hex', '-out', 'rand.tmp', '16']) # read in the output rand = fileIO.readFile('rand.tmp') # the password is actually the hash of the current time and random nonce value password = hash(str(time)+str(rand), '-hex') finally: # delete temp files fileIO.removeFile('rand.tmp') #return the output if password is None or retval != 0: raise Exception("generatePassword failed") else: return password
def generatePassword(): password = None retval = 0 try: # get the current time time = datetime.now().time() # write out input / create output files fileIO.writeFile('rand.tmp', "") # run openssl rand command retval = subprocess.call( ['openssl', 'rand', '-hex', '-out', 'rand.tmp', '16']) # read in the output rand = fileIO.readFile('rand.tmp') # the password is actually the hash of the current time and random nonce value password = hash(str(time) + str(rand), '-hex') finally: # delete temp files fileIO.removeFile('rand.tmp') #return the output if password is None or retval != 0: raise Exception("generatePassword failed") else: return password
def hash(plaintext, outputType): hashKey = None retval = 0 try: # write out input / create output files fileIO.writeFile('plain.tmp', plaintext) fileIO.writeFile('key.tmp', "") # run openssl hash command retval = subprocess.call([ 'openssl', 'dgst', '-sha256', outputType, '-out', 'key.tmp', 'plain.tmp' ]) # read in the output hashKey = fileIO.readFile('key.tmp') finally: # delete temp files fileIO.removeFile('plain.tmp') fileIO.removeFile('key.tmp') # return the output if hashKey is None or retval != 0: raise Exception("hash failed") else: return hashKey.replace('SHA256(plain.tmp)= ', '', 1)
def encrypt(key, plaintext): ciphertext = None retval = 0 try: # write out input / create output files fileIO.writeFile('key.tmp', key) fileIO.writeFile('plain.tmp', plaintext) fileIO.writeFile('cipher.tmp', "") # run openssl enc command with file('key.tmp') as f: retval = subprocess.call(['openssl', 'enc', '-aes-256-cbc', '-pass', 'stdin', '-out', 'cipher.tmp', '-in', 'plain.tmp'], stdin=f) # read in the output ciphertext = fileIO.readFile('cipher.tmp') finally: # delete temp files fileIO.removeFile('key.tmp') fileIO.removeFile('plain.tmp') fileIO.removeFile('cipher.tmp') # return the output if ciphertext is None or retval != 0: raise Exception("encrypt failed") else: return ciphertext
def processResults(results, myNameList): for index in range(len(results)): myGender = results[index]['gender'] myName = myNameList[index] myOutString = str(myName) + "," + str(myGender) fileIO.writeFile(myOutString, 'results/genderAPI.csv')
def closeWallet(key, plaintext): encrypted = openSSL.encrypt(key, plaintext) fileIO.writeFile(WALLET_FILE, encrypted)
def processResults(results, myNameList): for index in range( len(results) ): myGender = results[index]['gender'] myName = myNameList[index] myOutString = str(myName)+","+str(myGender) fileIO.writeFile(myOutString, 'results/genderAPI.csv')