def decryptSingleImage(encryptedimagePath: str, password: str, single=True): encryptedimage = None with open(encryptedimagePath, 'r') as file: encryptedimage = file.read() passKey = keys.getPasswordGeneratedKey(password) try: image = Fernet(passKey).decrypt(encryptedimage.encode()) image = image.decode() # print(image[:20]) except InvalidToken: print("\tWrong Password! Try Again") sys.exit(0) shape, image = image.split(',') shape = list(map(int, shape.strip().split())) image = list(map(int, image.strip().split())) image = np.resize(np.array(image), shape) __writeDecryptedImage(encryptedimagePath, image, single)
def main(): """Buscando chave de acesso no token (pen drive) Caso não seja encontrada, a chave recebe o valor 0, e o drive_service None, invalidando qualquer operação na GUI. """ try: if os.name == 'posix': f = open("/media/allan/KINGSTON/text.txt", "r") else: f = open("F:/text.txt", "r") key = f.readline().encode() f.close() except: key = 0 connected = False drive_service = None #Informações descriptografadas são salvas no dicionário info if key != 0: #Autenticacao utilizando credenciais no arquivo JSON ou arquivo PICKLE try: creds = None if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) with open('token.pickle', 'wb') as token: pickle.dump(creds, token) #Autenticação google drive drive_service = build('drive', 'v3', credentials=creds) connected = True except Exception as e: print( "Não foi possível conectar ao Drive. Utilizandos dados locais." ) #Se houver conexão, atualizar dados locais através do drive if connected: request = drive_service.files().get_media(fileId=DATA_FILE_ID) fh = io.BytesIO() downloader = MediaIoBaseDownload(fh, request) done = False while done is False: status, done = downloader.next_chunk() fh.seek(0) data = fh.read() with open('data', 'wb') as file: file.write(data) #Lendo dados salvos localmente, após tentar atualiza-los with open("data", "r") as data: for line in data: dec = Fernet(key).decrypt(bytes(line, 'utf-8')).decode('utf-8') pwd, user, site = dec.strip().split(',') info[site] = (pwd, user) #Instanciação da GUI app = QApplication(sys.argv) win = Window(drive_service, key, connected) win.show() sys.exit(app.exec_())