24
loading...
This website collects cookies to deliver better user experience
.db
file in the system as plain text because the data is readable. So, we have to keep this database encrypted hence we will use following method to encrypt the data base.createdb.py
.The purpose of this file is to create a new database mySecret.db
having following columns: createdb.py
.import sqlite3
def create():
conn = sqlite3.connect("mySecret.db")
crsr = conn.cursor()
sql_command = """CREATE TABLE ROUTINE (
routineID VARCHAR(10000) PRIMARY KEY,
date DATE,
startTime VARCHAR(5),
endTime VARCHAR(5),
status VARCHAR(10));"""
crsr.execute(sql_command)
conn.commit()
conn.close()
sqlite3
is the library we have to import in order to work with database in python.CREATE TABLE ROUTINE (
routineID VARCHAR(10000) PRIMARY KEY,
date DATE,
startTime VARCHAR(5),
endTime VARCHAR(5),
status VARCHAR(10));
generateKey.py
.The purpose of this file is to generate a universal key which would be use while encryption and decryption of a cell in database.generateKey.py
.from cryptography.fernet import Fernet
def generate():
key = Fernet.generate_key()
with open("universal.key","wb") as key_files:
key_files.write(key)
universal.key
.encrypt.py
.The purpose of this file is to encrypt the secret message passed as parameter.encrypt.py
.from cryptography.fernet import Fernet
def loadKey():
key = open("universal.key","rb").read()
return key
def Encrypt(secret):
key = loadKey()
encodeSecret = secret.encode()
fer = Fernet(key)
return fer.encrypt(encodeSecret)
loadKey()
which will load the universal key stored inside the universal.key
. Then we will make an Encrypt()
block accepting "secret" as the parameter. For encryption we will first encode the secret message then pollute it with the salt made by Fernet(key)
by encrypting with the encoded message.decrypt.py
.The purpose of this file is to decrypt the encrypted message passed as parameter.decrypt.py
.from cryptography.fernet import Fernet
def loadKey():
key = open("universal.key","rb").read()
return key
def Decrypt(encryptSecret):
key = loadKey()
fer = Fernet(key)
decryptSecret = fer.decrypt(encryptSecret)
return decryptSecret.decode()
loadKey()
which will load the universal key stored inside the universal.key
. Then we will make an Decrypt()
block accepting "encryptSecret" as the parameter. For decryption we will first depollute the secret message from the salt made by Fernet(key)
by decrypting with the encrypted message then decode it to view the original secret message.dataentry.py
.The purpose of this file is to insert encrypted data into the database mySecret.db
having following columns: dataentry.py
.import sqlite3
import encrypt
def enter(id,dt,st,et,status):
conn = sqlite3.connect("mySecret.db")
crsr = conn.cursor()
sql_command = """INSERT INTO ROUTINE(routineID,date,startTIme,endTime,status)
VALUES (?,?,?,?,?);"""
id = encrypt.Encrypt(id)
dt = encrypt.Encrypt(dt)
st = encrypt.Encrypt(st)
et = encrypt.Encrypt(et)
status = encrypt.Encrypt(status)
tup = (id,dt,st,et,status)
crsr.execute(sql_command,tup)
conn.commit()
conn.close()
sqlite3
is the library we have to import in order to work with database in python. And we are also importing 'encrypt' module made by us.enter()
block takes 5 parameters as input. And then encrypt each parameter and then insert into the db in the form of tuple as shown in the code.INSERT INTO ROUTINE(routineID,date,startTIme,endTime,status)
VALUES (?,?,?,?,?);
dataprinting.py
.The purpose of this file is to print decrypted data from the database mySecret.db
having following columns: dataprinting.py
.import sqlite3
import decrypt
def printit():
conn = sqlite3.connect("mySecret.db")
crsr = conn.cursor()
crsr.execute("SELECT * FROM ROUTINE")
rows = crsr.fetchall()
for row in rows:
for cell in row:
print(decrypt.Decrypt(cell))
conn.commit()
conn.close()
sqlite3
is the library we have to import in order to work with database in python. And we are also importing 'decrypt' module made by us.printit()
fetch all rows(tuple) of database. And then decrypt each cell of each tuple and then print it. SELECT * FROM ROUTINE
main.py
.from os import path
import createdb
import generateKey
import dataentry
import dataprinting
def main():
if(path.exists('mySecret.db')==False):
generateKey.generate()
createdb.create()
exit = int(input("enter 0 if you want to exit or 1 to continue entring data"))
while(exit==1):
id = input("input routineID")
dt = input("input date in DD-MM-YYYY")
st = input("input start time in hh:mm")
et = input("input end time in hh:mm")
status = input("not done/pending/inprogress/done")
dataentry.enter(id,dt,st,et,status)
exit = int(input("enter 0 if you want to exit or 1 to continue entering data"))
dataprinting.printit()
main()
path from os
, self made modules createdb
,generateKey
,dataentry
anddataprinting
. The purpose of path
is to check whether the database 'mySecret.db' already exists or not. If it is not exist we will create the db and generate one universal key otherwise leave these two steps as shown in the main()
block. Now we are asking user to select either 1 "if they want to insert data in db" or 0 "if they do not want to insert data in db". Then in while loop we are taking 5 inputs and entering in database using module dataentry
.Once the data is being entered control comes out of while loop and using module dataprinting
print the data.main.py
the code it will ask enter 0 if you want to exit or 1 to continue entering data'. Click 1 and enter insert all data as asked. once all rows are finished press 0 to exit out of loop. And all the data will be printed. But in behind the scene some process happened.
mySecret.db` and 'univeral.key' are created.Output
Universal Key
yn1ZrHZ2VBLqq7iZ38pTMvRf9-3pyAVHFlWjADIPO20=
Consider below image to open db
Data inside DB:
routineID | date | startTime | endTime | status |
---|---|---|---|---|
gAAAAABhWDfTHxfYmU4kHl9uSqt-bF0LpSlZZyxyk2cXbyummG0HRfLRH6Ouja8u7yt12Ii-qAHzX_hkohLhg9GKglzEZhT2Hg== | gAAAAABhWDfTh4WTHW0ezh8kpWnr7Ccs5ZvdI12jtbxVRKa4pmKOx-Fm6cZ1NGbV_uTkk9--GG97LEjUSEEPRN-8Cj3RYtWSjQ== | gAAAAABhWDfTeGHg8foPNYIZxchL61UDmORidKvFgvdy_eiBX8vEV-24qk5fTrjinehW51AAs3ZZHWN3R-tvnJdfUpcykvMpsg== | gAAAAABhWDfTNFTmf7Cxgc48IUmrrKakT4-CSi4FYLp57DTzlqy3s9TFuEKXpIAPRa2gNWif_vO2Nc_6_Na1CpmvdrHNRdaTZQ== | gAAAAABhWDfT_hCJYgSVWZjCTy3YqWyhvORmVDi4tS4EZHsHJLe6P3t4ljZRFro21eFKemOnK9SZh3uzl7NOM7c54nDTBEQkSg== |
gAAAAABhWDhKooyIOnSHEJWhkj8QUaNw1YPIPzxyYHUE7yoBGWsIDlVy5_s7_SJPnF6yj5kxf_Lykl6SwYpVRMNbmJ9dGU7NkA== | gAAAAABhWDhKF9ATmebjCCNW_dLK74LvNm9l8sBk7PbxPG-70KsHolHNjZ5mFE3aj62XJS1TaGrZTxtMBYUiNuBs1WZwcqzymQ== | gAAAAABhWDhKJgoYW2tihp7tEgKEF1QG5EMHYkZ3nA6I27T_bArxt7V3-PZ1sn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