XOR Password Encryption GUI Part 1

before beginning this project you should have a good grasp on binary and logic gates, if not than it might be helpful to check out this post I wrote on binary numbers and logic gates as it serves as a good prerequisite.

What the finished Product will look like:

The finished product will be a fully functional GUI that a user can interact with to encrypt their passwords and it will looks something like this:

GUI-EX1

The GUI will be handle any encryption key between 1 and 9999:

GUI-EX1

With this encryption any password or message can be made impossible to decipher without the key!

GUI-EX1

What this project consists of:

In this project we’ll make a XOR binary encryption GUI that takes in any encryptions key and outputs the encrypted string to the user. Not only will you learn how to make a secure password encryption, but also how to make GUI’s using the tkinter module in Python.

Preprocessing the data:

Before we can run the XOR gate operation on the password and key we have to “preprocess” the data. All this means is turning the password and key into binary numbers so that they can be run through the XOR operator. First we’ll preprocess the password, create a function called preprocess that takes in the user’s unencrypted password as a parameter:

def pre_process(string):

To preprocess the string we have to first turn each character in the string into it’s corresponding ASCII value, and append it to an empty list variable. Next we’ll take each of the ASCII values, find the binary value and append it to it’s own list. With that in mind create two list variables for the string’s ASCII values and binary values:

def pre_process(string):
    string_ord = []
    binary_ord = []

Create a for loop that takes each character in the string and appends the character’s ASCII value to the string_ord list. We can accomplish this using the built in ord() function to find the ASCII value of the character passing through the for loop:

def pre_process(string):
    string_ord = []
    binary_ord = []
    for c in string:
        string_ord.append(ord(c))

Now create another for loop that goes through each number in the string_ord list, finds the number’s binary value and appends it to the binary_ord list. As you might have guessed we can use bin() function to find the number’s binary value:

def pre_process(string):
    string_ord = []
    binary_ord = []
    for c in string:
        string_ord.append(ord(c))
    for i in string_ord:
        binary_ord.append(bin(i))

Finally, return the list of binary numbers:

def pre_process(string):
    string_ord = []
    binary_ord = []
    for c in string:
        string_ord.append(ord(c))
    for i in string_ord:
        binary_ord.append(bin(i))
    return binary_ord

Below the previous function, create a function that preprocesses the encryption key. Create a function called preprocess_key that takes in the key as a parameter:

def preprocess_key(string_key):

When we make a GUI the key will be a number, but in the form of a string so we must convert it to an integer first:

def preprocess_key(string_key):
    int_key = int(string_key)

next convert the int_key into a binary number using the bin() function:

def preprocess_key(string_key):
    int_key = int(string_key)
    binary_key = bin(int_key)

Lastly, return the binary_key variable:

def preprocess_key(string_key):
    int_key = int(string_key)
    binary_key = bin(int_key)
    return binary_key

Running the XOR Encryption:

Now that we’ve created two functions for preprocessing the data we can run the data through the XOR operator. Create a function below the previous functions, that takes the preprocessed key and unencrypted password binary as parameters:

def xor_cipher(password_binary, binary_key):

create an empty list variable called cipher_list that will contain our encrypted binary values:

def xor_cipher(password_binary, binary_key):
    cipher_list = []

Since we need to have individual values from the password_binary being run through the XOR operation with the binary_key we need to have a “counter” to keep track of the index values. Create a variable called counter set to 0, this way when the first password_binary is run through it will start at index value 0:

def xor_cipher(password_binary, binary_key):
    cipher_list = []
    counter = 0    

Next create a for loop that runs for the number of integers inside the password_binary:

def xor_cipher(password_binary, binary_key):
    cipher_list = []
    counter = 0    
    for b in password_binary:

Now the interesting thing about the bin() function is that it gives us the binary, but in the form of a string. So inside the for loop create two variables that holds the value of the password_binary and binary_key as integers with a base of 2. We can do this by using the second optional parameter in the int() function which specifies the number base, the default is 10, but lets change it to 2:

def xor_cipher(password_binary, binary_key):
    cipher_list = []
    counter = 0    
    for b in password_binary:
        x = int(password_binary[counter], 2)
        y = int(binary_key, 2)

As you can see, inside the x variable value we’re utilizing the counter variable to serve as an index value, but we must make sure it increases each time the for loop is run so add 1 to the counter variable in to for loop:

def xor_cipher(password_binary, binary_key):
    cipher_list = []
    counter = 0    
    for b in password_binary:
        x = int(password_binary[counter], 2)
        y = int(binary_key, 2)
        counter +=1

And finally run the XOR operation on the x and y variables and append the output to the cipher_list variable:

def xor_cipher(password_binary, binary_key):
    cipher_list = []
    counter = 0    
    for b in password_binary:
        x = int(password_binary[counter], 2)
        y = int(binary_key, 2)
        counter +=1
        cipher_list.append(x ^ y)

Lastly return the cipher_list:

def xor_cipher(password_binary, binary_key):
    cipher_list = []
    counter = 0    
    for b in password_binary:
        x = int(password_binary[counter], 2)
        y = int(binary_key, 2)
        counter +=1
        cipher_list.append(x ^ y)
    return cipher_list

converting the encrypted binary into a readable encrypted password:

Now that we have the encrypted version of the password binary it’s time to convert the encrypted binary into characters, luckily this is pretty easy. Create a function called conversion that takes in the encrypted binary list as a parameter:

def conversion(binary_list):

Now create an empty list variable that will contain the encrypted characters:

def conversion(binary_list):
    string_list = []    

Next, create a for loop that will iterate through the binary_list:

def conversion(binary_list):
    string_list = []
    for i in binary_list:

Using the chr() function find the character of each integer in the binary list and append it to the string_list:

def conversion(binary_list):
    string_list = []
    for i in binary_list:
        string_list.append(chr(i))

Lastly, use the join() function to combine all the characters in the string_list into a single string variable and return it:

def conversion(binary_list):
    string_list = []
    for i in binary_list:
        string_list.append(chr(i))
    cipher_string = "".join(string_list)
    return cipher_string

Creating a Main function:

Lastly, we’ll make a main function that utilizes all the functions we just created to simplify the process. Have the function take in the key and password as an input:

def encrypt(key,password):
    cipher_object = conversion(xor_cipher(pre_process(password),preprocess_key(key)))
    return cipher_object

Now whenever you want a fully XOR encrypted password you can just call the encrypt() function we created. In part 2 I’ll show you how we can make a GUI for encrypting passwords with ease. The full gist file for part 1 can be viewed –> here <—