Python, fernet decrypt turn my file read only - python

I'm with a personal proyect using python and sqlite3, the case is that i use fernet to encrypt the db file, but at decrypt the db it turn in a read only db and give me error at try do an INSERT.
sqlite3.OperationalError: attempt to write a readonly database
I try edit the linux permissions using chmod 664 and 777 but nothing seems work it still as only read db
----- EDIT WITH MORE INFO
I use local file and connect using sqlite3, i create a clase for manage the db
class DataBaseManager(object):
def __init__(self,db):
self.con = sqlite3.connect(db)
self.cur = self.con.cursor()
def inicializar(self):
checkTableStore = self.cur.execute(
"""SELECT name FROM sqlite_master WHERE type='table' AND name='store'; """
).fetchall()
if checkTableStore == []:
self.cur.execute(
"""create table store(
........
)"""
)
def __del__(self):
self.con.close
if in the first execution and the db file doesn't exist create it with the DataBaseManager(dbfile) and after check if the table that i need is created or not and create if not exist with DataBaseManager.inicializar()
keyfile = input("keyfile: ")
dbfile = "pstore.db"
dbencrypt = "pstore.enc"
dbm = DataBaseManager(dbfile)
DataBaseManager.inicializar()
For encrypt and decrypt i use a key generated with fernet
def encryptdb(keyfile, dbfile, dbencrypt):
with open(keyfile, "rb") as openkeyfile:
key = openkeyfile.read()
fernet = Fernet(key)
with open (dbfile, "rb") as opendbfile:
decrypteddb = opendbfile.read()
encrypted = fernet.encrypt(decrypteddb)
with open (dbencrypt, "wb") as encrypteddb:
encrypteddb.write(encrypted)
def decryptdb(keyfile, dbfile, dbencrypt):
with open(keyfile, "rb") as openkeyfile:
key = openkeyfile.read()
fernet = Fernet(key)
with open (dbencrypt, "rb") as openenc:
encrypteddb = openenc.read()
decrypted = fernet.decrypt(encrypteddb)
with open (dbfile, "wb") as decrypteddb:
decrypteddb.write(decrypted)
Before than made the encrypt i can do inserts without problem, but after encrypt and decrypt i got the problem that the database is only read

Ok, solved.
I tryed use a second file to save the db encrypted. But i proved save the encrypted output of fernet in the sabe db file that existed, it don't give to me the ro error after decrypt.

Related

Retrieve zipped file from bytea column in PostgreSQL using Python

I have a table in my PostgreSQL database in which a column type is set to bytea in order to store zipped files.
The storing procedure works fine. I have problems when I need to retrieve the zipped file I uploaded.
def getAnsibleByLibrary(projectId):
con = psycopg2.connect(
database="xyz",
user="user",
password="pwd",
host="localhost",
port="5432",
)
print("Database opened successfully")
cur = con.cursor()
query = "SELECT ansiblezip FROM library WHERE library.id = (SELECT libraryid from project WHERE project.id = '"
query += str(projectId)
query += "')"
cur.execute(query)
rows = cur.fetchall()
repository = rows[0][0]
con.commit()
con.close()
print(repository, type(repository))
with open("zippedOne.zip", "wb") as fin:
fin.write(repository)
This code creates a zippedOne.zip file but it seems to be an invalid archive.
I tried also saving repository.tobytes() but it gives the same result.
I don't understand how I can handle memoriview objects.
If I try:
print(repository, type(repository))
the result is:
<memory at 0x7f6b62879348> <class 'memoryview'>
If I try to unzip the file:
chain#wraware:~$ unzip zippedOne.zip
The result is:
Archive: zippedOne.zip
End-of-central-directory signature not found. Either this file is not
a zipfile, or it constitutes one disk of a multi-part archive. In the
latter case the central directory and zipfile comment will be found on
the last disk(s) of this archive.
unzip: cannot find zipfile directory in one of zippedOne.zip or
zippedOne.zip.zip, and cannot find zippedOne.zip.ZIP, period.
Trying to extract it in windows gives me the error: "The compressed (zipped) folder is invalid"
This code, based on the example in the question, works for me:
import io
import zipfile
import psycopg2
DROP = """DROP TABLE IF EXISTS so69434887"""
CREATE = """\
CREATE TABLE so69434887 (
id serial primary key,
ansiblezip bytea
)
"""
buf = io.BytesIO()
with zipfile.ZipFile(buf, mode='w') as zf:
zf.writestr('so69434887.txt', 'abc')
with psycopg2.connect(database="test") as conn:
cur = conn.cursor()
cur.execute(DROP)
cur.execute(CREATE)
conn.commit()
cur.execute("""INSERT INTO so69434887 (ansiblezip) VALUES (%s)""", (buf.getvalue(),))
conn.commit()
cur.execute("""SELECT ansiblezip FROM so69434887""")
memview, = cur.fetchone()
with open('so69434887.zip', 'wb') as f:
f.write(memview)
and is unzippable (on Linux, at least)
$ unzip -p so69434887.zip so69434887.txt
abc
So perhaps the data is not being inserted correctly.
FWIW I got the "End-of-central-directory signature not found" until I made sure I closed the zipfile object before writing to the database.

Insert PDF into Postgres database with python and retrieve it

I have to store a small PDF file in a Postgres database (already have a table ready with a bytea column for the data), then be able to delete the file, and use the data in the database to restore the PDF as it was.
For context, I'm working with FastApi in Python3 so I can get the file as bytes, or as a whole file. So the main steps are:
Getting the file as bytes or a file via FastAPI
Inserting it into the Postgres DB
Retrieve the data in the DB
Make a new PDF file with the data.
How can I do that in a clean way?
The uploading function from FastAPI :
def import_data(file: UploadFile= File(...)):
# Put the whole data into a variable as bytes
pdfFile = file.file.read()
database.insertPdfInDb(pdfFile)
# Saving the file we just got to check if it's intact (it is)
file_name = file.filename.replace(" ", "-")
with open(file_name,'wb+') as f:
f.write(pdfFile)
f.close()
return {"filename": file.filename}
The function inserting the data into the Postgres DB :
def insertPdfInDb(pdfFile):
conn = connectToDb()
curs = conn.cursor()
curs.execute("INSERT INTO PDFSTORAGE(pdf, description) values (%s, 'Some description...')", (psycopg2.Binary(pdfFile),))
conn.commit()
print("PDF insertion in the database attempted.")
disconnectFromDb(conn)
return 0
# Saving the file we just got to check if it's intact (it is)
file_name = file.filename.replace(" ", "-")
with open(file_name,'wb+') as f:
f.write(pdfFile)
f.close()
return {"filename": file.filename}
The exporting part is just started and entirely try-and-error code.

encrypt password in python

I want to generate my Python code in a setup.exe. The user stores an email password in the script. My question: Do I have to additionally encrypt this password, even though I create an * .exe file.
def load_settings(self):
# print(__file__)
# print(os.path.dirname(__file__))
pf = os.path.dirname(__file__)
pa = os.path.join(pf, "settings.json")
# print(pa)
if os.path.exists(pa):
# print("Pfad existiert")
with open(pa, "r") as infile:
data = json.load(infile)
self.ein.pfadbez.setText(data["pfad"])
self.ein.name.setText(data["name"])
self.ein.mail.setText(data["mail"])
self.ein.ausgangserver.setText(data["smtp"])
self.ein.port.setText(data["port"])
self.ein.login.setText(data["login"])
self.ein.passwort.setText(data["pw"])
From the way you worded your question, it sounds like you want a user to store a password within the code itself, or in a text file. Variables are called variables because they vary - a password won't be saved between executions unless stored in plain text, which is where encryption will be needed.
Further, generating Python code from a Windows executable will still require that Python code to be put somewhere for execution, and since Python is fundamentally open-source, hiding it in a compiled package won't do much.
Going about text encryption is simple - since you're on Windows, you could use Pycryptodomex, which will simplify the process of encrypting text. This tutorial could help.
Here's my code:
from cryptography.fernet import Fernet
import pyperclip
print("For this program to work, please send the file named 'pwkey' and the encrypted code to the other user.")
key = Fernet.generate_key()
file = open('pwkey', 'wb')
file.write(key)
file.close()
print('File Generated')
original = input('Enter message>>>')
message = original.encode()
f = Fernet(key)
encrypted = f.encrypt(message)
encrypted = encrypted.decode("ascii")
print('Encrypted:', encrypted)
pyperclip.copy(encrypted)
print('Please tell the other user to input the encrypted code in the Decrypt program')
print('(Code copied to Clipboard)')
print("Note: Please delete the 'pwkey' file after sending it to the other user. It
is for one-time use only.")
And decrypting
# Decrypt
from cryptography.fernet import Fernet
print("For this program to work, make sure you have the file named 'pwkey' in your Python folder and the encrypted "
"code.")
file = open('pwkey', 'rb')
key = file.read()
file.close()
print('Key Retrieved')
encrypted = input('Please input your encrypted code>>>')
encrypted = bytes(encrypted, 'utf-8')
f = Fernet(key)
decrypted = f.decrypt(encrypted)
decrypted = decrypted.decode()
print('Decrypted Message:')
print(decrypted)
print("Note: Please delete the 'pwkey' file after getting the decrypted message.")

How to download BLOB .docx file from MySQL using python?

Current Code:
import mysql.connector
import sys
def write_file(data, filename):
with open(filename, 'wb') as f:
f.write(data)
sampleNum = 0;
# select photo column of a specific author
# read database configuration
db_config = mysql.connector.connect(user='root', password='test',
host='localhost',
database='technical')
# query blob data form the authors table
cursor = db_config.cursor()
try:
sampleNum=sampleNum+1;
query = "SELECT file FROM test WHERE id=%s"
cursor.execute(query,(sampleNum,))
photo = cursor.fetchone()[0]
write_file(photo, 'User'+str(sampleNum)+'.jpg')
except AttributeError as e:
print(e)
finally:
cursor.close()
Goal of above code
Code above allows me to get the image from MySQL that is stored as a BLOB and save it into a folder where .py script is saved.
It works fine!
Similar code with .docx
import mysql.connector
import sys
def write_file(data, filename):
with open(filename, 'wb') as f:
f.write(data)
sampleNum = 0;
db_config = mysql.connector.connect(user='root', password='test',
host='localhost',
database='technical')
cursor = db_config.cursor()
try:
sampleNum=sampleNum+1;
query = "SELECT fileAttachment FROM document_control WHERE id=%s"
cursor.execute(query,(sampleNum,))
file = cursor.fetchone()[0]
write_file(file, 'User'+str(sampleNum)+'.docx')
except AttributeError as e:
print(e)
finally:
cursor.close()
Here I am trying to extract and save a docx file from MySQL stored as a BLOB and it does not work.
The output of above script is the following:
f.write(data)
TypeError: a bytes-like object is required, not 'str'
How can I extract the .docx file from MySQL?
As per the insert query you mentioned
insert into document_control (fileattachment) values ('C:/Users/<user>/Desktop/Weekly Checks.xlsx');
it seems that you are just inserting the filepath in the database.
You must use LOAD_FILE to insert the actual file in the database blob object.
How to use LOAD_FILE to load a file into a MySQL blob?

Decrypt with Fernet [Python]

I am currently working with a login system and I want to decrypt a password that is stored in my database (It is encrypted from the beginning)
I've tried this:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
# THIS IS STORED IN MY DATABASE FOR EXAMPLE
password = "gAAAAABWC9P7-9RsxTz_dwxh9-O2VUB7Ih8UCQL1_Zk4suxnkCvb26Ie4i8HSUJ4caHZuiNtjLl3qfmCv_fS3_VpjL7HxCz7_Q=="
passwordChosen = password.decode('utf-8')
decryptedPasswordDB = cipher_suite.decrypt(passwordChosen)
print(decryptedPasswordDB)
I'm getting the error "InvalidToken"...
I want the decryptedPasswordDB varaible to print the actually password.
I appreciate all the help. Thanks.
First, decrypt then decode
passwordChosen = cipher.decrypt(password)
decryptedPasswordDB = passwordChosen.decode('utf-8')

Categories

Resources