Encrypt your data and software with Python

Utpal Kumar   2 minute read      

Learn to encrypt and decrypt any files, data or software with python

In this post, we will build a python script to encrypt or decrypt any data, or software with Python. In the end, you can download the ready to use script.

Encrypt a file

The logic of encryption is simple here. We first read any file as binary file (hence use the ‘rb’ mode). Then we convert the read in data into an array of bytes. We then apply the XOR operation on each element of the array using a given key. The XOR operation (or “exclusive or” in Python) compares two binary numbers bitwise. If both bits are the same, XOR outputs 0. If both bits are different, XOR outputs 1. The given key is your passkey that can be used to decrypt the files as well. Finally, we write the binray XOR operated data into a file.

Write an encryption function

def Encrypt(filename, key, pref):
    #reads the input file
    file = open(filename, "rb")
    data = file.read()
    file.close()

    #converts into an arrray of bytes
    data = bytearray(data)
    for kk in key:
        kk = ord(kk)
        for index, value in enumerate(data):
            #apply XOR operation
            data[index] = value ^ kk
    #writes the output file
    file = open (pref + "-" + filename, "wb")
    file.write(data)
    file.close()

Decrypt a file

Decryption works in the same way as encryption and uses the same key.

Write a decryption function

def Decrypt(filename, key, pref):
    #reads the input file
    file = open(filename, "rb")
    data = file.read()
    file.close()

    #converts into an arrray of bytes
    data = bytearray(data)
    for kk in key:
        kk = ord(kk)
        for index, value in enumerate(data):
            #apply XOR operation
            data[index] = value ^ kk
    
    #writes the output file
    defilename = filename.split("-")[-1] #remove the encryption prefix
    file = open (pref + "-" +defilename, "wb")
    file.write(data)
    file.close()

Complete Script

You can download the complete script from my github repository.

Usage

$ python endecrypt_with_python.py -h
usage: endecrypt_with_python.py [-h] [-encpref ENCRYPT_PREFIX]
                                [-decpref DECRYPT_PREFIX] [-en] [-de] -inp
                                INPUTS [INPUTS ...] -k KEY

Python utility program to encrypt/decrypt multiple files (by Utpal Kumar, UC
Berkeley, 2021/11)

optional arguments:
  -h, --help            show this help message and exit
  -encpref ENCRYPT_PREFIX, --encrypt_prefix ENCRYPT_PREFIX
                        prefix for the encrypted files
  -decpref DECRYPT_PREFIX, --decrypt_prefix DECRYPT_PREFIX
                        prefix for the decrypted files
  -en, --encrypt        encrypt the files
  -de, --decrypt        decrypt the files
  -inp INPUTS [INPUTS ...], --inputs INPUTS [INPUTS ...]
                        input files to encrypt or decrypt
  -k KEY, --key KEY     key to encrypt or decrypt the files

enter filename(s) and key to encrypt and decrypt

Example

Encrypt files

$ python endecrypt_with_python.py -inp "myscript.py" "helloworld.py" -k "password" -en 

Decrypt files:

$ python endecrypt_with_python.py -inp "CC-myscript.py" "CC-helloworld.py" -k "password" -de 

Disclaimer of liability

The information provided by the Earth Inversion is made available for educational purposes only.

Whilst we endeavor to keep the information up-to-date and correct. Earth Inversion makes no representations or warranties of any kind, express or implied about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services or related graphics content on the website for any purpose.

UNDER NO CIRCUMSTANCE SHALL WE HAVE ANY LIABILITY TO YOU FOR ANY LOSS OR DAMAGE OF ANY KIND INCURRED AS A RESULT OF THE USE OF THE SITE OR RELIANCE ON ANY INFORMATION PROVIDED ON THE SITE. ANY RELIANCE YOU PLACED ON SUCH MATERIAL IS THEREFORE STRICTLY AT YOUR OWN RISK.


Leave a comment