Encryption (Version 1)

Note

External module required!!! If you wish to use this module, please go and download Cryptography. Otherwise this module is useless. Use pip install PythonFunctions[encryption] to get the required modules.

Note

This will not work on some devices due to the requirements required. This is not our falut but something to do with Cryptography.

An easier class to encrypt and decrypt data, just some simple functions

from PythonFunctions.Encryption import Encryption

GetKey

Encryption.GetKey()

Using Fernet, makes and returns a key for you. If you loose this key you are 99% gaurenteed to be locked out of your data.

Returns

Your key

Return type

bytes

from PythonFunctions.Encryption import Encryption
print(Encryption.GetKey())  # Returns your key

Note

When decrypting, if the wrong key is used. cryptography.fernet.InvalidToken will be raised

EncryptData

Encryption.EncryptData(data, key)

Using your key, encrypts the data and returns it.

Parameters
  • data (any) – The data to encrypt

  • key (bytes) – The key to encrypt the data with (generated by Encryption.GetKey())

Returns

The encrypted data

Return type

bytes

Note

Even though the data is any, it will be encoded using utf-8 into bytes first.

from PythonFunctions.Encryption import Encryption
print(Encryption.EncryptData("Hello World", key)) # Some kind of weird byte mess

DecryptData

Encryption.DecryptData(data, key)

Using your key, decrypts the data and retuns it

Parameters
  • data (bytes) – The encrypted data

  • key (bytes) – The key to decrypt the data (Same one used to encrypt it)

Returns

The decrypted data

Return type

any

from PythonFunctions.Encryption import Encryption
print(Encryption.DecryptData(data, key)) # "Hello World"

encrypt

Encryption.encrypt(data, key, *, filename='encrypted')
Parameters
  • data (any) – The data to save + encrypt

  • key (bytes) – The key to encrypt the data with

  • filename (str) – Optional, where to save the data to. (Defaults to encrypted)

Returns

What happened

Return type

str

from PythonFunctions.Encryption import Encryption
Encryption.encrypt("Hello World", key, filename="Secret") # Saves the encrypted version of "Hello World" to "Secret"

decrypt

Encryption.decrypt(key, *, filename='encrypted')
Parameters
  • key (bytes) – The key to decrypt the data with

  • filename (str) – Optional, where to load the data from. (Defaults to encrypted)

Returns

What happened

Return type

str

from PythonFunctions.Encryption import Encryption
Encryption.decrypt(key, filename="Secret") # "Hello World" (if same key is used)