Skip to content

Encrpter File

Samora Machel edited this page Aug 3, 2020 · 2 revisions

Encrypter File

Encrypt

def Encrypt(data: any, KeyFile: __file__ = ".Data/.encryptionKey", DataFile: __file__ = ".Data/.encrypted" ) -> None:

The function takes in 3 parameters:

  • Data
  • KeyFile
  • DataFile

Data as the name goes represents the data that is passed in. The data type to be passed in in any
KeyFile represents the location to where to store the key generated for encryption and decryption
DataFile represents the location to where to store the encrpted data

# Adds the directory .Data incase it is not present 
if not os.path.exists("./.Data"):
        os.mkdir(".Data")

All the data generated by the Encrypter are stored under one special file .Data. So the program first checks if the folder exists first and if it does not exists the it creates one.

# check if data is a dictionary and convert it
# to a json type otherwise let it remain as string
if (type(data) == dict):
        data = json.dumps(data)

We then check if the data passed is a a dictionary. If its a dictionary we then convert it to a json, this makes it easier to lateear parse it when decrypting the data. Incase to data passed is any other data type, when the treat it as a string.

# generate the key and then dump the key to a file 
key = xcrypt.Fernet.generate_key()
keyFile = open(KeyFile, 'wb')
pickle.dump(key, keyFile)
keyFile.close()

Before we encrypt the file we first generate a special key of we can later on use to decrypt the file. The key generated is then pickled then dumped in a file specifically the ( KeyFile ).

encryptionType = xcrypt.Fernet(key)

We then use the key generated to to select an Encryption Type which will be used for encrypting the data.

dataBytes = bytes(str(data), 'utf-8')
encryptedData = encryptionType.encrypt(b'%b'%(dataBytes))

Since the encryption support only bytes, we first change our data to bytes and then we encrypt the data using our encryption type.

# dumping the data in a file 
cryptoFile = open(DataFile, 'wb')
pickle.dump(encryptedData, cryptoFile)

Since our data is in bytes we then create a file that supports bytes and then dump our pickled data to the file

  1. Encrypter File
    1. Encrypt
  2. Decrypter File
    1. Decrypt
    2. isJson
    3. getData
  3. passwordCheck File
    1. checkPassword
  4. Main File
    1. MainApplication Class
      1. checkPasswordSecurityLevel
      2. isPasswordAvailable
      3. login
      4. getData
      5. viewUrl
      6. storeData

Clone this wiki locally