Categories:

Encryption and Decryption Task, via Python

For a simple cybersecurity project to practice the key concepts of encryption and decryption, one can create a simple program (using Python) to achieve this. For this project, we will need to utilize the Pandas library and a character list (which will be provided as a .csv).

Download: encryption/decryption key

Setup

For this, you can feed the input value or you can setup the code where it can collect a user’s input instead. For this project, we will just feed it the message to encrypt.

# import Pandas
import pandas as pd

# import encryption key via Pandas
# 'r' = read
encryptionkey = pd.read_csv(r"<insert .csv path>",
                            sep=',', names=['Character', 'Byte'], header=None, skiprows=[0])

# create a dataframe 
df = pd.DataFrame(data=encryptionkey)

# take the columns and convert values to string
df['Character'] = df['Character'].astype(str)
df['Byte'] = df['Byte'].astype(str)

# split original message
# for example: ['g', 'r', 'e', 'e', 't', 'i', 'n', 'g', 's']
def split(message):
    return [char for char in message]

# message for encryption
message = "The real test is not whether you avoid this failure, because you won't." \
"  It's whether you let it harden or shame you into inaction, or whether you learn from it; whether you choose to persevere."

split_message = split(message)

print(split_message)

Encryption

# declare function called 'code_message()'
def code_message():
    coded_message = ""

    # create for loop
    # loop will be used to iterate a given number of times a block of code
    # iterations are dependent on value provided
    for x in range(len(split_message)):
        y = split_message[x]
        # establish error handling via try-except
        try:
            char_coded = encryptionkey.loc[encryptionkey['Character'] == y, 'Byte'].iloc[0]
            print(type(char_coded))

        # To handle if character is not in our decryption list
        except:
            print('unrecognized character')
            # this is a placeholder value, hence @@@
            char_coded = '@@@'

        # concatenate coded_message + char_coded
        coded_message = coded_message + char_coded
    return coded_message

coded_message = code_message()

# display encrypted message
print('Your coded message:', code_message(), '\n')

Decryption

####### Decryption #######

def decode_message(message):
    new_word = ''
    message_decode = []

    # similar to encryption setup, except we will use +2
    # increment will be +2 instead of +1
    for x in range(0, len(message), 2):
        # instantiate var as slice of message
        y = message[x:x + 2]
        index_nb = df[df.eq(y).any(1)]

        df2 = index_nb['Character'].tolist()

        # transform data type to string
        a = [str(a) for a in df2]

        # concatenate message_decode + a
        message_decode = message_decode + a

    new_word = ''.join(message_decode)

    return new_word

coded_message_str = str(coded_message)
print('Your decoded message:', decode_message(coded_message_str))

Full Code

# import Pandas
import pandas as pd

# import encryption key via Pandas
# 'r' = read
encryptionkey = pd.read_csv(r"<insert .csv path>",
                            sep=',', names=['Character', 'Byte'], header=None, skiprows=[0])

# create a dataframe 
df = pd.DataFrame(data=encryptionkey)

# take the columns and convert values to string
df['Character'] = df['Character'].astype(str)
df['Byte'] = df['Byte'].astype(str)

# split original message
# for example: ['g', 'r', 'e', 'e', 't', 'i', 'n', 'g', 's']
def split(message):
    return [char for char in message]

# message for encryption
message = "The real test is not whether you avoid this failure, because you won't." \
"  It's whether you let it harden or shame you into inaction, or whether you learn from it; whether you choose to persevere."

split_message = split(message)

print(split_message)

####### Encryption #######

# declare function called 'code_message()'
def code_message():
    coded_message = ""

    # create for loop
    # loop will be used to iterate a given number of times a block of code
    # iterations are dependent on value provided
    for x in range(len(split_message)):
        y = split_message[x]
        # establish error handling via try-except
        try:
            char_coded = encryptionkey.loc[encryptionkey['Character'] == y, 'Byte'].iloc[0]
            print(type(char_coded))

        # To handle if character is not in our decryption list
        except:
            print('unrecognized character')
            # this is a placeholder value, hence @@@
            char_coded = '@@@'

        # concatenate coded_message + char_coded
        coded_message = coded_message + char_coded
    return coded_message

coded_message = code_message()

# display encrypted message
print('Your coded message:', code_message(), '\n')

####### Decryption #######

def decode_message(message):
    new_word = ''
    message_decode = []

    # similar to encryption setup, except we will use +2
    # increment will be +2 instead of +1
    for x in range(0, len(message), 2):
        # instantiate var as slice of message
        y = message[x:x + 2]
        index_nb = df[df.eq(y).any(1)]

        df2 = index_nb['Character'].tolist()

        # transform data type to string
        a = [str(a) for a in df2]

        # concatenate message_decode + a
        message_decode = message_decode + a

    new_word = ''.join(message_decode)

    return new_word

coded_message_str = str(coded_message)
print('Your decoded message:', decode_message(coded_message_str))

Output

['T', 'h', 'e', ' ', 'r', 'e', 'a', 'l', ' ', 't', 'e', 's', 't', ' ', 'i', 's', ' ', 'n', 'o', 't', ' ', 'w', 'h', 'e', 't', 'h', 'e', 'r', ' ', 'y', 'o', 
'u', ' ', 'a', 'v', 'o', 'i', 'd', ' ', 't', 'h', 'i', 's', ' ', 'f', 'a', 'i', 'l', 'u', 'r', 'e', ',', ' ', 'b', 'e', 'c', 'a', 'u', 's', 'e', ' ', 'y', 'o', 'u', ' ', 'w', 'o', 'n', "'", 't', '.', ' ', ' ', 'I', 't', "'", 's', ' ', 'w', 'h', 'e', 't', 'h', 'e', 'r', ' ', 'y', 'o', 'u', ' ', 'l', 'e', 't', ' 
', 'i', 't', ' ', 'h', 'a', 'r', 'd', 'e', 'n', ' ', 'o', 'r', ' ', 's', 'h', 'a', 'm', 'e', ' ', 'y', 'o', 'u', ' ', 'i', 'n', 't', 'o', ' ', 'i', 'n', 'a', 'c', 't', 'i', 'o', 'n', ',', ' ', 'o', 'r', ' ', 'w', 'h', 'e', 't', 'h', 'e', 'r', ' ', 'y', 'o', 'u', ' ', 'l', 'e', 'a', 'r', 'n', ' ', 'f', 'r', 'o', 'm', ' ', 'i', 't', ';', ' ', 'w', 'h', 'e', 't', 'h', 'e', 'r', ' ', 'y', 'o', 'u', ' ', 'c', 'h', 'o', 'o', 's', 'e', ' ', 't', 'o', ' ', 'p', 'e', 'r', 
's', 'e', 'v', 'e', 'r', 'e', '.']
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
74207768657468657220796f752061766f69642074686973206661696c7572652c206265636175736520796f7520776f6e27742e202049742773207768657468657220796f75206c65742069742068617264656e206f72207368616d6520796f7520696e746f20696e616374696f6e2c206f72207768657468657220796f75206c6561726e2066726f6d2069743b207768657468657220796f752063686f6f736520746f207065727365766572652e

Your decoded message: The real test is not whether you avoid this failure, because you won't.  It's whether you let it harden or shame you into inaction, or whether you learn from it; whether you choose to persevere.