Pages

Showing posts with label cryptography. Show all posts
Showing posts with label cryptography. Show all posts

Tuesday, January 12, 2016

Accessing Python Functions in Quicksilver

Over the last two years, I have been programming a lot with Python.  I have developed a lot of functions to do various mathematical tasks. For instance, I have a function that takes a number and spits out a list of its factors:
factors(30) = [1, 2, 3, 5, 6, 10, 15, 30]

And I have another that gives me the prime factorization of a number:
primefactorization(400) = [2, 2, 2, 2, 5, 5]

These have been very helpful as I have created other programs, and as I have participated in the EulerProject -- a set of mathematical challenges that typically require the user to create a program that helps solve them.  As I try to complete more and more challenges, my collection of useful functions grows and grows.

Recently, I have tried to find a way to access these functions more quickly.  Previously, I would have to find them in the correct program file, open it up, tweak it a little bit to call the function with the specifics of what I was looking for, and then run the program.  Annoyingly many steps, in my opinion, and usually hard enough to deter me from using my own work and find an answer another way, or give up entirely.

However, I found a way to use Quicksilver to access these functions, inspired by a friends QuicksilverPythonTodoList program. He uses Quicksilver, as I do, to easily find and open programs and files on the mac, to move and copy files, to create qr codes, to search google, and many other things.  And he uses it to call up a python program and add items to a todo list by a simple keyboard command.  By pressing Cmd-Space, period, typing his todo item, pressing tab, and enter, quicksilver automatically opens up the correct python program, enters in the correct input, the python script runs which updates a text file, which is automatically saved and displayed on his background via GeekTool. All in the background, instantly (practically), at the press of a few simple buttons.  I wanted that too, for my mathematical functions.

So I gathered all my functions into one python module which I called euler.py and reading Brian's post on adding custom actions to quicksilver I set out to enable this for myself.  It took three steps, which I'll outline for you if you're interested yourselves:

Step 0: Install quicksilver and learn how to use the period to enter text entry mode. 
  1. Create a python module that has any desired functions all in one place.  Take note of the file name and path as you will need it later. Mine was ~/Documents/script/euler.py
  2. Use terminal to access that file and call up a function.  I had to use a -c switch with python to access my functions.  Something like:
        python -c "import euler; print euler.factors(30)"
    This is essentially a two-line program that imports my list of functions, and calls one of them specifically.  
  3. Write an applescript file that calls this terminal command.  In order for quicksilver to recognize it, you'll need to save this applescript file in ~/Library/Application Support/QuickSilver/Actions and then restart QS.  I created an applescript named DoMath.
    using terms from application 'Quicksilver' on process text theText set results to do shel script 'cd ~/Documents/script; python -c "import euler; print euler." & theText & "'" return results end process text end using terms from
Now I can access my files by the following keystrokes:
cmd space, period, type my functions name, tab, type domath, enter.


Having a heavy interest in cryptography, I also took many of the functions I wrote to encipher and decipher messages and set it up to be done the same way.  So now:
cmd space, period, CaesarShift("hello world") tab encoder instantly produces Khoor zruog.  

I have one improvement I'd like to do -- but not sure how to quite yet.  I'd like to be able to have it save the results to the clipboard automatically.  I'm sure that's possible with an additional tweak of the DoMath and Encoder applescripts -- but that's a learning project for another day.

Saturday, December 15, 2012

Matrix Multiplication Cryptography

This post is a part of a series of guest-posts on the applications of matrix multiplication. These posts were written by my pre-calc students:

Matrix Cryptography
by David Stanley

Cryptography, put simply, is the art of encoding messages.  It serves to answer the simple question of how you get a message to a friend without your enemy being able to read it.  Cryptography has been used for centuries by militaries and intelligence agencies to send important messages, while insuring that the information the messages contain does not fall into enemy hands. 
Although there are many forms of cryptography, one of the simplest yet most effective forms of encryption still utilizes the simple matrix.The message is placed in matrix form, and then multiplied by a random square matrix or encoding matrix. 

 The first step is to write down the message that you wish to send.  I will use this completely true and totally non-brown nosing message as an example:

Mr Roer is the best math teacher ever in the history of humanity.

Secondly, you must create an encoding matrix.  This matrix must be a square matrix. An example of this would be:

In this kind of encryption, letters are assigned numbers for their place in the alphabet.  A would be 1, B would be 2, C would be 3 and so on.  Spaces are assigned the number 27, as their are only 26 letters in the alphabet.  So the message in matrix form would be:

Notice how my encoding matrix has the same amount of columns as the message matrix does rows.  This is required or else they cannot be multiplied.  All that is left to do is to multiply the encryption matrix by the message matrix.  This gives you:
Now if you received this in the mail, you would have no idea at all what it said.  In order to figure this out in a timely manner, you would have to have the decoding matrix.  The decoding matrix is the inverse of the encoding matrix.  This can easily be found on your calculator.  The decoding matrix for this problem is quite long, so I will round to four decimal places. 

The exact elements in the decoding matrix have more digits and would give the exact numbers as the original message. Using this rounded decryption matrix gives numbers that can all be rounded to the original message, though occasionally a letter might be slightly off.  

In summary, the encoding process written in calculator language when 
           [A] is the encoding matrix,
           [B] is the original message,  
    and [C] is the encoded message is [A]x[B]=[C]. 
The process of decoding the message is ([A]^-1)x[C]=[B]. 

And there you have it.  That is matrix cryptography in a nutshell. 
Related Posts Plugin for WordPress, Blogger...