01
Desription
As a lover of binary numbers and cryptography, I decided to work on a python script that could convert text into a binary equivalent and re-convert the output back into text so that you're able to send encrypted messages to an individual with the same script without anyone who could potentially intercept the message understanding what is written.
This is where the dictionaries holding the letter to binary translation and vice versa are held. I decided to use “string.printable” values in order to ensure that all characters that are printable can be accounted for. The trick was how do I go about converting these!
Every character has its own asci number. I thought this was ideal a it would be easier to convert a number into binary than it would be to calculate a letter! To get this number I used the built in ord() function which returns the value. There is also a built in bin() function that converts numbers into binary! Perfect! Not so perfect. The format that this function presents is “0b100101”. Unfortunately, from a cryptography point a view, if every letter was identifiable with a 0b, it wouldn’t be very hard to separate each of the letters and identify a pattern and decrypt it.
So I had to make sure that the “0b” was removed and the code was able to break down a constant series of 1s and 0s. In other words, it would be hard to say whether “10001010” is “1000” and “1010” or “100”, “010” and “10” etc.
Every character has its own asci number. I thought this was ideal a it would be easier to convert a number into binary than it would be to calculate a letter! To get this number I used the built in ord() function which returns the value. There is also a built in bin() function that converts numbers into binary! Perfect! Not so perfect. The format that this function presents is “0b100101”. Unfortunately, from a cryptography point a view, if every letter was identifiable with a 0b, it wouldn’t be very hard to separate each of the letters and identify a pattern and decrypt it.
So I had to make sure that the “0b” was removed and the code was able to break down a constant series of 1s and 0s. In other words, it would be hard to say whether “10001010” is “1000” and “1010” or “100”, “010” and “10” etc.
So I needed to make sure that each of the words were converted to a series of 1s and 0s that could be of equal length to make it easier for the script. Seeing as the asci numbers vary from 9 to 126 in the printable method of string. This would vary the length of each of the binary conversions. Therefore, I evened them all out by adding 256. This ensure that the binary conversions were all 9 digits long because the 9th digit in a binary number represents 256. For there to be a 10th digit, there would have to be 255 different characters. In string.printable, there are only 100 different values with the lowest value being 9 and the highest being 126 which means the lowest value in our assignments will be 265 and 382, which is between 256 and 512 meaning the characters will all be of the same length. Perfect! Now we are able to print a string of 0s and 1s which nobody but the script holder would be able to understand!
This method is to provide the used with the dictionaries that are used to convert the text and the binary outputs.
This method is where the message input by the user is converted into our binary output using the same methodology explained before. It breaks down the message into a list of individual characters that make up that message. For each letter in the message, it uses the letter as a key to the dictionary holding the binary equivalent and returns the binary value into a list. Once every letter is converted, the method returns the elements of the list joined into one string.
This method decrypts the output of the previous method. It takes the string of 1s and 0s and cuts it down knowing that the length of each converted element is 9 characters long. Each 9-character element is then used as a key to access the dictionary with the letter equivalents and returns the value. This value is appended to a list. The method then returns the elements of the list joined into one string to reveal you hidden message.
Code Editor
This method is used to provide a user-friendly interface to interact with the methods within the class. This allows the user to dictate how they wish to use the script without having to access the methods directly reducing the number of key strokes required.
Improvements and Ideas
The script can be improved by allowing the user to enter a key against which a random algorithm can be run off of, making it harder to decrypt the message. This could be constructed to add a number between 256 and 385. This would make it harder to guess the dictionary contents as it would be subject to the user input however, it would still remain 9 characters long.