Python path error in encryption decryption function - python

I am attempting to use encryption key from my USB stick to decrypt a file on my computer.
The output I get from my current script (based on source: https://medium.com/codex/encrypting-your-files-using-a-usb-stick-as-the-key-python-e04b26657357) keeps throwing path errors. It can load the key but somehow doesnt decrypt the file on my computer. Can someone see where this error is coming from or how it can be solved?
I get the following output:
C:\Users\Asus\PycharmProjects\pythonProject220728usbkey\venv\Scripts\python.exe C:/Users/Asus/PycharmProjects/pythonProject220728usbkey/main.py
Trying to find key...
Key Found
Traceback (most recent call last):
File "C:\Users\Asus\PycharmProjects\pythonProject220728usbkey\main.py", line 68, in <module>
encryptFiles(key,files)
File "C:\Users\Asus\PycharmProjects\pythonProject220728usbkey\main.py", line 30, in encryptFiles
files = os.listdir(directory)
NotADirectoryError: [WinError 267] El nombre del directorio no es vĂ¡lido: 'C:\\Users\\Asus\\PycharmProjects\\pythonProject220728usbkey\\enc_levensonderhoud.xlsx'
Process finished with exit code 1
-------------------------------------------------------------------------------------#
#See my current script below:
import os
import wmi
from cryptography.fernet import Fernet
my_key = 'TICOZM'
files = r'C:\Users\Asus\PycharmProjects\pythonProject220728usbkey\enc_levensonderhoud.xlsx'
c = wmi.WMI()
def check_for_key():
for disk in c.Win32_LogicalDisk():
if disk.VolumeName==my_key:
return disk
def load_key(usbDisk):
port = usbDisk.DeviceID
try:
print('Trying to find key...')
with open(f'{port}\\encryptionKey.key','rb') as encryptKey:
key = encryptKey.read()
print('Key Found')
except:
print('Key not found... Creating a new key')
key = Fernet.generate_key()
with open(f'{port}\\encryptionKey.key','wb') as encryptKey:
encryptKey.write(key)
return key
def encryptFiles(key,directory):
files = os.listdir(directory)
cipher = Fernet(key)
global state
state = 'encrypted'
for file in files:
with open(f'{directory}\{file}','rb') as old:
original = old.read()
encrypted = cipher.encrypt(original)
with open(f'{directory}\{file}','wb') as old:
old.write(encrypted)
def decryptFiles(key, directory):
files = os.listdir(directory)
cipher = Fernet(key)
global state
state = 'decrypted'
for file in files:
with open(f'{directory}\{file}', 'rb') as old:
encrypted = old.read()
decrypted = cipher.decrypt(encrypted)
with open(f'{directory}\{file}', 'wb') as old:
old.write(decrypted)
state = 'decrypted'
if __name__=='__main__':
while True:
disk = check_for_key()
try:
key = load_key(disk)
except:
print('No Key Available')
if disk!=None:
current_state = 'decrypted'
if current_state!=state:
decryptFiles(key,files)
else:
current_state = 'encrypted'
if current_state!=state:
encryptFiles(key,files)

Related

How to avoid/bypass Python Fernet "InvalidToken"

I want to decrypt many files and it's working perfectly (encrypt, decrypt).
My problem is that, if any file exists which is not is encrypted, I get an "invalidToken" error message, and the program is unable to decrypt the remaining encrypted files.
Here is my code
import os
from cryptography.fernet import Fernet
import sys
import warnings
InvalidToken = False
key = b'SpecialKindKey'
fernet = Fernet(key)
encr_file_list = []
path ="C:/users/NineTail/desktop/pokemon/go/"
#we shall store all the file names in this list
filelist = []
for root, dirs, files in os.walk(path):
for file in files:
#append the file name to the list
filelist.append(os.path.join(root,file))
#print all the file names
for name in filelist:
print(name)
for file in filelist:
with open(file, 'rb') as f:
file_content = f.read()
decrypted_content = fernet.decrypt(file_content)
with open(file, 'wb') as f:
f.write(decrypted_content)
Error message :
Traceback (most recent call last):
File "r3.py", line 36, in <module>
decrypted_content = fernet.decrypt(file_content)
File "C:\Program Files\Python38\lib\site-packages\cryptography\fernet.py", line 85, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "C:\Program Files\Python38\lib\site-packages\cryptography\fernet.py", line 121, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
Use try/except to try to decrypt. If decrypting fails, continue without writing.
...
from cryptography.fernet import Fernet, InvalidToken
...
for file in filelist:
with open(file, 'rb') as f:
file_content = f.read()
try:
decrypted_content = fernet.decrypt(file_content)
except InvalidToken:
continue
with open(file, 'wb') as f:
f.write(decrypted_content)
...
Perhaps try having a loop that repeats the number of times of files you have, and calls each file separately from the rest
Define a list of files, and store that in a variable
say:
num = #number of files
files = [file1.txt,file2.txt]
For i in range(num):
files[num] = open(item, 'r')
num += 1

cryptography.fernet.InvalidToken problem with cryptography

Getting this error when trying to run this:
File "Test Files.py", line 502, in decryptdefault
decrypted = fernet.decrypt(d)
File "/usr/lib/python3/dist-packages/cryptography/fernet.py", line 74, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "/usr/lib/python3/dist-packages/cryptography/fernet.py", line 92, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
FYI dk variable is defined with key (default key)
dk = 'niwaXsYbDiAxmLiqRiFbDa_8gHio15sNQ6ZO-sQ0nR4='
# Decrypts the file with default key
def decryptdefault(inclufile):
Key = dk
fernet = Fernet(Key)
readfile = open(inclufile, 'rb')
d = readfile.read()
readfile.close()
# Decrypts and puts it into the text
if readfile != "":
decrypted = fernet.decrypt(d)
decrypted = str(decrypted).replace('b\'', '', 1)
decrypted = decrypted[:-3]
return str(decrypted)
Edit: I added the key for those who asked
I have found out, through trial and error with the same project later down the line, that you need to turn your key into something like this key = b'niwaXsYbDiAxmLiqRiFbDa_8gHio15sNQ6ZO-sQ0nR4='
The main difference being the key is encoded in a utf-8 format and is now readable by Fernet and doesn't return that error. Here is a function that uses Tkinter, Fernet, and os to actually decrypt my file.
# Propriatary method of encrypting files
def decrypt(self, file):
with open(file, 'rb') as readfile:
contents = readfile.read()
self.title(os.path.basename(file) + ' - SecureNote')
# self.textbox is a variable inside of the class I am using for my window
self.textbox.delete(1.0, tk.END)
if contents != "":
# getword retur
Key = bytes(getword('Key:', 1), encoding="utf-8")
fernet = Fernet(Key)
decrypted = fernet.decrypt(contents).decode('utf-8')
self.textbox.insert(1.0, str(decrypted))
del Key
del fernet
else:
pass

How to get an encryption key from file

I'm trying to get the encryption key from Google Chrome and I get an error saying:
line 13, in find_crypt
print(encrypt['encryption_key'])
KeyError: 'encryption_key'
The code is:
from genericpath import exists
import win32crypt
import json
import os
def find_crypt(path):
path = path.replace('\Default', '')
for file in os.listdir(path):
if file == 'Local State':
f = open('{0}\\{1}'.format(path, file), 'r', encoding='utf-8')
encrypt = json.load(f)
print(encrypt['encryption_key'])
def find_passwords(path):
for file in os.listdir(path):
if file == 'Login Data':
pass
def main():
local = os.getenv('LocalAppData')
roaming = os.getenv('AppData')
directories = {
'chrome' : local + '\\Google\\Chrome\\User Data\\Default'
}
for name, directory in directories.items():
if not os.path.exists(directory):
continue
else:
find_crypt(directory)
find_passwords(directory)
if __name__ == '__main__':
main()
The Local State File does not store any key with the name encryption_key but instead with encrypted_key.
That is why your Python Program yells [at the line 13] it cant find any key with the name encryption_key.
Also the encrypted_key is inside another object with the name os_crypt
Enough Talking, Here is the modified code :)
def find_crypt(path):
path = path.replace('\Default', '')
print(path)
for file in os.listdir(path):
if file == 'Local State':
f = open('{0}\\{1}'.format(path, file), 'r', encoding='utf-8')
encrypt = json.load(f)
print(encrypt['os_crypt']['encrypted_key'])

Decrypt message with cryptography.fernet do not work

I just tried my hand at encrypting and decrypting data. I first generated a key, then encrypted data with it and saved it to an XML file. Now this data is read and should be decrypted again.
But now I get the error message "cryptography.fernet.InvalidToken".
import xml.etree.cElementTree as ET
from cryptography.fernet import Fernet
from pathlib import Path
def load_key():
"""
Load the previously generated key
"""
return open("../login/secret.key", "rb").read()
def generate_key():
"""
Generates a key and save it into a file
"""
key = Fernet.generate_key()
with open("../login/secret.key", "wb") as key_file:
key_file.write(key)
def decrypt_message(encrypted_message):
"""
Decrypts an encrypted message
"""
key = load_key()
f = Fernet(key)
message = encrypted_message.encode('utf-8')
decrypted_message = f.decrypt(message)
return(decrypted_message)
def decryptMessage(StringToDecrypt):
decryptedMessage = decrypt_message(StringToDecrypt)
return decryptedMessage
def loginToRoster(chrome):
credentials = readXML()
user = decryptMessage(credentials[0])
pw = decryptMessage(credentials[1])
userName = chrome.find_element_by_id('UserName')
userName.send_keys(user)
password = chrome.find_element_by_id('Password')
password.send_keys(pw)
In the tuple "credentials" there are 2 encrypted strings.
Please help - have already tried everything to change the formats, but no chance.
Edit:
Errormessage:
Traceback (most recent call last):
File "C:/Users/r/Documents/GitHub/ServiceEvaluationRK/source/main.py", line 27, in <module>
login.loginToRoster(chrome)
File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py", line 106, in loginToRoster
user = decryptMessage(credentials[0])
File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py", line 49, in decryptMessage
decryptedMessage = decrypt_message(StringToDecrypt)
File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py", line 43, in decrypt_message
decrypted_message = f.decrypt(message)
File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\venv\lib\site-packages\cryptography\fernet.py", line 75, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\venv\lib\site-packages\cryptography\fernet.py", line 107, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
I found an answer to my problem:
I took ASCII instead of utf-8. And I added a .decode('ASCII') at the function "loginToRoster" to both variables 'user' and 'pw'
Now the encryption and decryption works fine.
So, the 'loginToRoster' functions looks like:
def loginToRoster(chrome):
credentials = readXML()
user = decryptMessage(credentials[0]).decode('ASCII')
pw = decryptMessage(credentials[1]).decode('ASCII')
userName = chrome.find_element_by_id('UserName')
userName.send_keys(user)
password = chrome.find_element_by_id('Password')
password.send_keys(pw)
Where have you defined load_key() in the decrypt_message function. It's not a method it's just a undefined function. You're probably getting that error since the key is invalid because you're not getting the one you saved.

How to make sure the Fernet key stays the same when initialized?

So, I am making a password manager and I am using the cryptography module. The problem with this program is when decrypting. It will work fine when I encrypt and decrypt in the same session but when I encrypt, close, and then decrypt in a different session, errors will be raised. This error isn't happening because I am generating a random key every time, but I think the key is changing when I initialize it using the Fernet() method. How do I solve this?
#The Dependicies and Packages required for this script
import sqlite3
from cryptography.fernet import Fernet
def generate_key():
"""
Generates a key and save it into a file
"""
key = Fernet.generate_key()
with open("secret.key", "wb") as key_file:
key_file.write(key)
def load_key():
"""
Loads the key named `secret.key` from the current directory.
"""
return open("secret.key", "rb").read()
#These are the keys for encryption
Key = load_key()
f = Fernet(Key)
def decode_data(datas):
new_name = f.decrypt(datas)
final_name = new_name.decode()
return final_name
def find_password():
"""
This function is to get the password of the website that the user expected
"""
website_name = input("What is the website's name for which you need a password>")
c.execute("SELECT * FROM passwords")
data = c.fetchall()
print(data)
for row in data:
print(row[0])
name = decode_data(row[0])
if name == website_name:
password = decode_data(row[2])
print(f'The password to {website_name} is {password}')
def main():
go_on = True
while go_on:
direction_question = input("This is your password manager. Press 1 to create a new pasword, Press 2 to search for a password, or Press 3 to exit the program>")
if direction_question.lower() == "1":
create_password()
if direction_question.lower() == "2":
find_password()
if direction_question.lower() == "3":
go_on = False
else:
print("Invalid response")
db.commit()
db.close()
if __name__ == "__main__":
db = sqlite3.connect('password.db')
c = db.cursor()
main()
These errors were raised
File "/usr/local/lib/python3.7/site-packages/cryptography/fernet.py", line 114, in _verify_signature
h.verify(data[-32:])
File "/usr/local/lib/python3.7/site-packages/cryptography/hazmat/primitives/hmac.py", line 68, in verify
ctx.verify(signature)
File "/usr/local/lib/python3.7/site-packages/cryptography/hazmat/backends/openssl/hmac.py", line 78, in verify
raise InvalidSignature("Signature did not match digest.")
cryptography.exceptions.InvalidSignature: Signature did not match digest.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "password_manager.py", line 86, in <module>
main()
File "password_manager.py", line 74, in main
find_password()
File "password_manager.py", line 61, in find_password
name = decode_data(row[0])
File "password_manager.py", line 28, in decode_data
new_name = f.decrypt(datas)
File "/usr/local/lib/python3.7/site-packages/cryptography/fernet.py", line 77, in decrypt
return self._decrypt_data(data, timestamp, ttl, int(time.time()))
File "/usr/local/lib/python3.7/site-packages/cryptography/fernet.py", line 126, in _decrypt_data
self._verify_signature(data)
File "/usr/local/lib/python3.7/site-packages/cryptography/fernet.py", line 116, in _verify_signature
raise InvalidToken
cryptography.fernet.InvalidToken
You have done a good job storing once generated keys to a separate file.I've run your scrip and it works fine. The only issue I found is that the following part should be within the main() method.
Key = load_key()
f = Fernet(Key)
As the main() method is actually getting an empty string as a key rather than reading the stored key in the .key file.Hence, it is throwing
raise InvalidSignature("Signature did not match digest.")
cryptography.exceptions.InvalidSignature: Signature did not match digest.
and
raise InvalidToken
cryptography.fernet.InvalidToken

Categories