def readBucket(bucketID, maxDataLength): if not os.path.exists(home + bucketLoc): os.makedirs(home + bucketLoc) inputFile = open(home + bucketLoc + str(bucketID), "rb") # rb = read binary #bytesIn = inputFile.read() bytesIn = pickle.load(inputFile) inputFile.close() if encrypt: bytesIn = Encryptor.decrypt(bytesIn, key) result = [] while True: leafBytes = bytesIn[:4] bytesIn = bytesIn[4:] if leafBytes == b"": break # break if end of file segIDBytes = bytesIn[:4] bytesIn = bytesIn[4:] dataLength = int.from_bytes(bytesIn[:4], byteorder="little") bytesIn = bytesIn[4:] data = bytesIn[:dataLength] bytesIn = bytesIn[maxDataLength:] result.append( Block.Block(int.from_bytes(leafBytes, byteorder="little"), int.from_bytes(segIDBytes, byteorder="little"), data)) return result
def readBucket(bucketID, maxDataLength): if not os.path.exists(home + bucketLoc): os.makedirs(home + bucketLoc) inputFile = open(home + bucketLoc + str(bucketID), "rb") # rb = read binary #bytesIn = inputFile.read() bytesIn = pickle.load(inputFile) inputFile.close() if encrypt: bytesIn = Encryptor.decrypt(bytesIn, key) result = [] while True: leafBytes = bytesIn[:4] bytesIn = bytesIn[4:] if leafBytes == b"": break # break if end of file segIDBytes = bytesIn[:4] bytesIn = bytesIn[4:] dataLength = int.from_bytes(bytesIn[:4], byteorder = "little") bytesIn = bytesIn[4:] data = bytesIn[:dataLength] bytesIn = bytesIn[maxDataLength:] result.append(Block.Block(int.from_bytes(leafBytes, byteorder = "little"), int.from_bytes(segIDBytes, byteorder = "little"), data)) return result
def files_decrypt(key, folder): for file in folder: find_folder(folder, file['title']) e_text = file.GetContentString() d_text = Encryptor.decrypt(e_text.encode(), key) file.SetContentString(d_text.decode()) file.Upload()
def decrypt_all_files(key, folder): for file1 in folder: find_folder(folder, file1['title']) print('title: %s, id: %s' % (file1['title'], file1['id'])) encrypted_text = file1.GetContentString() decrypted_text = Encryptor.decrypt(encrypted_text.encode(), key) file1.SetContentString(decrypted_text.decode()) file1.Upload() print(decrypted_text)
def decrypt(fileName, key): try: with open(fileName, "r+b") as file2Decrypt: data = base64.b64decode(file2Decrypt.read()) decryptor = Encryptor(key, data[:AES.block_size]) plainText = decryptor.decrypt(data) with open(fileName[:-4], "wb") as decryptedFile: decryptedFile.write(plainText) os.remove(fileName) except: print("Cannot decrypt this file")
def retrieve_file(symmetric_key, filename, drive, folder_id): file_list = drive.ListFile({ 'q': "'" + folder_id + "' in parents and trashed=false" }).GetList() for file1 in file_list: # find file to be downloaded if file1["title"] == filename: encrypted_text = file1.GetContentString() decrypted_text = Encryptor.decrypt( encrypted_text.encode(), symmetric_key, ) # printing downloaded text to check results print(decrypted_text) # save files in downloads with open(os.path.join("downloads", filename), 'wb') as d_file: d_file.write(decrypted_text) d_file.close()
def ORAMvsNormal(): numTests = 1000 oram = UserFileSys.UserFileSys(1301, 3, 65536, 100, 1.8, 2.0, 2.2, 1) oram._oram.autoResize = False for i in range (2,13): createTestFile(1 << i) oram.write("TestFiles/test" + str(1 << i) + ".txt") total = 0 totalSize = 0 for i in range(numTests): fileName = getFile() totalSize += int(fileName[14:fileName.index(".")]) start = time.clock() oram.read(fileName) timeTaken = time.clock() - start total += timeTaken print(total) print ("Throughput Disk + ORAM + Encryption: " + str(totalSize/total)) total = 0 totalSize = 0 for i in range(numTests): fileName = getFile() totalSize += int(fileName[14:fileName.index(".")]) inputFile = open(fileName, "rb") data = inputFile.read() inputFile.close() start = time.clock() data = Encryptor.encrypt(data, key) outputFile = open(fileName[:-4] + "_encrypted.txt", "wb") pickle.dump(data, outputFile) outputFile.close() inputFile = open(fileName[:-4] + "_encrypted.txt", "rb") data = pickle.load(inputFile) inputFile.close() data = Encryptor.decrypt(data, key) timeTaken = time.clock() - start total += timeTaken print(total) print("Throughput Disk + Encryption: " + str(totalSize/total)) total = 0 totalSize = 0 for i in range(numTests): start = time.clock() fileName = getFile() totalSize += int(fileName[14:fileName.index(".")]) file = open(fileName, "r") data = file.read() file.close() file = open(fileName, "w") file.write(data) file.close() total += (time.clock()-start) avg = total/numTests print(total) print ("Throughput Disk: " + str(totalSize/total))