Creating CUSTOM Encrypting(Encoding) and Decrypting program in Python

Creating CUSTOM Encrypting(Encoding) and Decrypting program in Python

There is no better security protocol than encrypting the data in today’s world.

Data security is always a hot topic. Recently I came across a challenge where I need to deal with sensitive data in RPA. The data to be entered will be maintained by certain users, but the file containing the data will be visible across all the employees in the team. To prevent these unauthorized users accessing the valuable sensitive information, I thought of encrypting and decrypting the data using cryptography library in Python so the data will be visible and maintained by the BOT.

But, how about creating an encrypting algorithm of our own? It's fun right? I understand to invent a brand new cryptographic algorithm one needs a very strong mathematical and statistical background with a strong knowledge of a bit-operation capable language. Sometimes there’s nothing like a good challenge, just for fun. Please note that the functions we create in this tutorial/article and our implementation could be considered as cryptographically “weak”. But we are gonna define encrypted values for each characters of our own which makes it secure to some extent. With this, lets jump in.

Introduction

Lets first understand the difference between encoding and encryption. While encryption does involve encoding data, the two are not interchangeable terms, encryption is always used when referring to data that has been securely encoded. Encoding data is used only when talking about data that is not securely encoded.

In simple terms, Encryption is the process to encode data securely such that only the authorized user who knows the key or password is able to retrieve the original data, for everyone else it is just garbage. It is used to maintain data confidentiality. Some of the popular algorithms are - AES, RSA, and Blowfish. Encoding is the process of transforming data into such a format that it can be by a different type of system using publicly available algorithms. It is used to maintain data usability. Some of the popular algorithms are -ASCII, UNICODE, URL encoding, Base64.

There are two basic types of encryption: Symmetric-key and Asymmetric-key

In symmetric-key encryption, the data is encoded and decoded with the same key, like a password. This is the easiest way of encryption. In Asymmetric-key encryption, one key is used to encrypt data (public key) and a different key is used to decrypt the data (private-key).

In this tutorial, we gonna create a custom program to encrypt and decrypt data using simple symmetric-key i.e. same key (data table) will be used for both encrypting and decrypting the data.

Getting Started

Let's understand the functions that we are gonna create in this program. We require one function to read the keys, one function to encrypt the text and another to decrypt the text, that's it!

Reading the keys

Let's create encrypted values for each characters. For understanding, below CSV table contains encrypted values for numeric characters only. You can define your own values to each characters. Here, to make the algorithm a little complex, I have defined two encrypted keys for each character which will be used in combination.

Sample keys for each numbers

Let's create the function which is the core algorithm of this program that actually modifies the above keys instead of using the keys as-is.

No alt text provided for this image

The above code snippet contains the logic of combining alternate values of the encrypted values from columns key to internally generate a new key.

Line 1 — Importing pandas package to read the CSV table.

Line 2 — Creating a function that takes one parameter i.e. the file in which the encrypted keys are stored (above CSV file).

Line 3 — Defining an empty dictionary to store the data at later stage.

Line 4 — Reading the CSV table.

Line 5–8 — Converting Columns ‘Key1’ and ‘Key2’ as string and reversing Column ‘Key2’ 

Line 9–14 — Creating a new key associated with each character as an encryption key by combining two key columns.

Line 15 — Converting the Characters and Key columns into dictionary and returning it in the line 16.

The above algorithm can also be written with different logics in a way how you need to encrypt the data. One can also use mathematical equations to encrypt each character. Here I am taking the alternate encrypted characters from two columns.

Encrypting the string

This is the second function upon calling should generate the encrypted text.

No alt text provided for this image

In the above code snippet, the function encrypt_string takes two parameters -

Line 1 — Defining function encrypt_string that takes two parameters:

keyFile — The file which contains the encrypted key for each character. This is used to generate the keys by calling the function generate_keys which we discussed in the above section.

stringToEncrypt — The text message that has to be encrypted.

Line 3 — Creating the encryption keys and storing it in dictionary named ‘keys’ by calling the function generate_keys.

Line 4–6 — Taking each character in the input string and fetching associated ‘key’ from the dictionary by looking at its ‘value’ in the dictionary. 

Line 7 — The final encrypted output is returned.

Let's see how the output would look like by passing the string '560100' to encrypt as second argument and the key file which we built in the first step as first argument.

In:

print(encrypt_string('EncodedKey.csv','560100'))
        

Out:

CfQ3=2=JC+A7=y=gBZA+=7=9DoQb=+=ABZA+=7=9BZA+=7=9        


Decrypting the string

This function decrypts the encrypted value by referring to the same keys. Let's understand the function-

No alt text provided for this image

This is a little bit tricky function. In the above code snippet-

Line 1 — Creating a function decrypt_string that takes two parameters:

keyFile — The file which contains the encrypted key for each character. This is used to generate the keys by calling the function generate_keys which we discussed in the above section.

stringToDecrypt — The encrypted message that has to be decrypted.

Line 3 — Creating the encryption keys and storing it in dictionary named ‘keys’ by calling the function generate_keys.

Line 4- 6 — If you remember while building the CSV file, we had defined total eight characters from the two key columns. Hence, here we are taking 8 characters from the encrypted values at a time and looking for the appropriate values from the dictionary.

Line 7 — The final decrypted output is returned.

Let's see how the output would look like by passing the encrypted string that we got in the above section 'CfQ3=2=JC+A7=y=gBZA+=7=9DoQb=+=ABZA+=7=9BZA+=7=9' to decrypt as second argument and the key file which we built in first step as first argument.

In:

print(decrypt_string('EncodedKey.csv',
'CfQ3=2=JC+A7=y=gBZA+=7=9DoQb=+=ABZA+=7=9BZA+=7=9'))
        

Out:

560100        

Wrapping up

Thus, in this tutorial we saw how we can create a custom symmetric-key encryption program. Here we have used encoded values in two columns referring as Key1 and Key2. One can modify the number of columns and in fact tweak the algorithm to generate complex keys by combining multiple columns. Also using mathematical equations or altering the logic of combining the multiple columns, complex algorithm can be created.

Thanks for reading ❤️ keep sharing 📚

#python #encryption #decryption #roboticprocessautomation #rpa #algorithm #custom #automationanywhere #netapp #netappcloud #encoding #decode #dictionary #pandas #cypher2022 #cypher #symmetry





To view or add a comment, sign in

More articles by Nagaraj Vaidya

  • RPA way forward - RPA 2.0 / RPA 3.0

    Hello connections, Its quite a high time, many of the RPA CoEs are planning to scale up there Centre of Excellence. If…

    2 Comments
  • Open AI GPT-3

    GPT-3 is the largest language model ever created and is capable of generating text that is indistinguishable from human…

    1 Comment
  • A2019 Enterprise and Automation Anywhere v11.x (AAE v11)

    Automation Anywhere 10.x Enterprise Software was pushed to history on March 31st 2020, this was the date on which…

  • Attended vs. Unattended Automation

    Hello traveler, are you confused about what kind of automation suits your RPA CoE, do you want to know what kind of…

    3 Comments

Insights from the community

Others also viewed

Explore topics