One example of a transposition cipher is columnar transposition. The cipher works like this:
- Pick a keyword
- Lay your message out character by character in rows the same length of your key
- Extra room can be filled by random characters
- Arrange the columns according to the value of the character in the key (eg. socket = 541326)
- Arrange the columns into rows
Here is an example using the key "socket" and the message "canyouprogramthis":
s o c k e t c a n y o u p r o g r a m t h i s t noh ors ygi art cpm uat
"noh" corresponds to the 3 characters below the letter "c" in "socket". Alphabetically speaking, "c" comes before the other letters in the word "socket" so the letters underneath it go first in the encrypted message.
Today's goal is to create a function that takes in a key (socket) and an encrypted message (noh ors ygi art cpm uat) and outputs the unencrypted message (canyouprogramthis).
As a bonus problem see if you can figure out the key for this message:
heeilsef twkdaohe eensmtry
Comments:
Daniel - 10 years, 8 months ago
The fun thing about this is that the key just needs to be the same length as the number of 'words'. For example, the key for "heeilsef twkdaohe eensmtry" can be any 3 letter word where the second letter comes earlier in the alphabet than the first, and the last letter comes later than the previous two letters in the alphabet. The words 'sky' and 'try' both work as a key, and even the word 'key' does. I'll write up the code for this later on today.
reply permalink
Max Burstein - 10 years, 8 months ago
haha yes indeed. As you can see it's not a very secure means of encryption.
reply permalink
Anonymous - 10 years, 8 months ago
Fun problem. In J:
reply permalink
Max Burstein - 10 years, 8 months ago
Any chance you could explain how that works? J seems like a magical language
reply permalink
Anonymous - 10 years, 8 months ago
Lol sure. decrypt is a function. Inside the function body, x and y represent the left and right argument passed to the function. J is read from right to left:
The parenthesised expression on the left is
which yields 4 3 0 2 1 5, the ordered column indices that will "decrypt" the rows.
Then, {"1 uses this "grade" to put every row of the matrix into the order 4 3 0 2 1 5. Finally, the comma strings the rows of the matrix together to yield the decrypted message.
I'm just learning a little J for the giggles, sure bends the mind in a good way though :)
reply permalink
Max Burstein - 10 years, 8 months ago
Thanks for the explanation. It definitely is mind bending
reply permalink
toph - 10 years, 8 months ago
I'm just learning perl, but here we go. Also noticed that this only works if the key has unique letters, as "sockets" or "bookkeeper" you wouldn't be able to tell which of the same letters came first.
reply permalink
Hueho - 10 years, 8 months ago
reply permalink
Hueho - 10 years, 8 months ago
Oops, last line on alt_cipher should be:
m.map(&:join).join(' ')
reply permalink
Hueho - 10 years, 8 months ago
...r.map
My bad.
reply permalink
Adam - 10 years, 8 months ago
A Haskell solution:
reply permalink
Isi - 10 years, 8 months ago
I made an encrypt script for this in python, which was really fun, but there was really no need to code anything to solve for "heeilsef....", since all the possible alternations of they key can be guessed by looking at the number of groups of N letters.
reply permalink
gulliver - 10 years, 8 months ago
A little late (the weekend is not almost here anymore) but here's my effort:
reply permalink
Byron - 10 years, 7 months ago
I think its a little messy but it gets the job done :) Python!
reply permalink